summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-10-25 10:37:32 +0100
committerLars Wirzenius <liw@liw.fi>2010-10-25 10:37:32 +0100
commited77916173766327248d84453564f1ae05b05535 (patch)
treeadb0e4c58330deae014897fca913fd51b869833b
parent20da8374e9da7009da7fe7cfbf81387f6cc94dc6 (diff)
downloaddynstr-ed77916173766327248d84453564f1ae05b05535.tar.gz
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.
-rw-r--r--dynstr.c7
-rw-r--r--dynstr.h9
-rw-r--r--unittests.c41
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 <stdbool.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#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 <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#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]);