From ed77916173766327248d84453564f1ae05b05535 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 25 Oct 2010 10:37:32 +0100 Subject: Implement dynstr_write. Changed the function signature since write(2) is so different from fwrite(3), and there's no point in trying to shoehorn the two into the same mold. --- dynstr.c | 7 +++++++ dynstr.h | 9 ++++++--- unittests.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/dynstr.c b/dynstr.c index 4805b45..99ef2ab 100644 --- a/dynstr.c +++ b/dynstr.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "dynstr.h" @@ -294,3 +295,9 @@ size_t dynstr_fwrite(FILE *file, Dynstr *dynstr) return fwrite(dynstr->mem, 1, dynstr->size, file); } + +ssize_t dynstr_write(int fd, Dynstr *dynstr) +{ + return write(fd, dynstr->mem, dynstr->size); +} + diff --git a/dynstr.h b/dynstr.h index d0ac247..95f7f4e 100644 --- a/dynstr.h +++ b/dynstr.h @@ -136,12 +136,15 @@ size_t dynstr_last_string(Dynstr *dynstr, size_t offset, Dynstr *pattern); * Comparisons are done byte-by-byte using unsigned values. */ int dynstr_cmp(Dynstr *dynstr1, Dynstr *dynstr2); -/* Write a dynamic string into an open file, either FILE or a Unix file - * handle. Return value is number of bytes written, just like for fwrite(3). +/* Write a dynamic string into an open file (using stdio). Return value is + * number of bytes written, just like for fwrite(3). * If an error occurs, the size is less than the length of the string, * and errno has been set by fwrite(3). */ size_t dynstr_fwrite(FILE *file, Dynstr *dynstr); -size_t dynstr_write(int file, Dynstr *dynstr); + +/* Write a dynamic string into an open Unix file handle. Return value is + * number of bytes written, or -1. */ +ssize_t dynstr_write(int file, Dynstr *dynstr); /* Read a number of bytes from an open file, either FILE or a Unix file * handle. Return value is number of bytes read, just like for fread(3). diff --git a/unittests.c b/unittests.c index aea00e5..38cbc5d 100644 --- a/unittests.c +++ b/unittests.c @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include #include "dynstr.h" @@ -996,6 +999,43 @@ static int test_fwrite_writes_string(void) } +static int test_write_writes_string(void) +{ + char tempname[] = "unittest.XXXXXX"; + int fd; + int fd2; + size_t num_bytes; + Dynstr *dynstr; + Dynstr *dynstr2; + char buf[1024]; + int read_bytes; + + dynstr = new_from_cstring("life is too short for str* and mem* in apps"); + + fd = mkstemp(tempname); + if (fd == -1) + abort(); + + fd2 = open(tempname, O_WRONLY, 0); + num_bytes = dynstr_write(fd2, dynstr); + close(fd2); + + FAIL_UNLESS_EQUAL(num_bytes, dynstr_len(dynstr)); + + /* FIXME: should compare contents of strings, too */ + read_bytes = read(fd, buf, sizeof(buf)); + if (read_bytes == -1) + abort(); + close(fd); + dynstr2 = dynstr_new_from_memory(buf, read_bytes); + FAIL_UNLESS_EQUAL(read_bytes, dynstr_len(dynstr)); + FAIL_UNLESS_EQUAL(dynstr_cmp(dynstr, dynstr2), 0); + dynstr_free(dynstr2); + + return true; +} + + static void setup(void) { dynstr_init(); @@ -1083,6 +1123,7 @@ static const struct test tests[] = { TEST(test_cmp_returns_negative_if_first_string_comes_first), TEST(test_cmp_returns_positive_if_second_string_comes_first), TEST(test_fwrite_writes_string), + TEST(test_write_writes_string), }; static const int num_tests = sizeof(tests) / sizeof(tests[0]); -- cgit v1.2.1