summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: db10803)
raw | patch | inline | side by side (parent: db10803)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 29 Oct 2013 09:06:42 +0000 (10:06 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 29 Oct 2013 09:06:42 +0000 (10:06 +0100) |
This function may be used to read from an open file into the buffer.
src/include/utils/strbuf.h | patch | blob | history | |
src/utils/strbuf.c | patch | blob | history |
index 6ad9e1e481fa84d7e1c0dff4f26b571159859118..ebbd9e3afff3dcc4bf143afa6019114c7cb4038f 100644 (file)
ssize_t
sdb_strbuf_memappend(sdb_strbuf_t *strbuf, const void *data, size_t n);
+/*
+ * sdb_strbuf_read:
+ * Read from an open file-descriptor and append the data to the buffer. The
+ * function does not handle *any* read errors. This allows for more
+ * flexibility for the caller regarding the handling of EAGAIN or EWOULDBLOCK.
+ *
+ * Returns:
+ * - the number of bytes read (zero on EOF)
+ * - a negative value on error
+ */
+ssize_t
+sdb_strbuf_read(sdb_strbuf_t *strbuf, int fd, size_t n);
+
/*
* sdb_strbuf_chomp:
* Remove all consecutive newline characters from the end of the string buffer
diff --git a/src/utils/strbuf.c b/src/utils/strbuf.c
index 993b086d0ef0adbcb060faf046cd7390f548e4bd..bffcfc633c1b6669d391627ad67526d01e61c28f 100644 (file)
--- a/src/utils/strbuf.c
+++ b/src/utils/strbuf.c
#include <stdarg.h>
#include <string.h>
+#include <unistd.h>
+
/*
* private data structures
*/
return sdb_strbuf_memappend(strbuf, data, n);
} /* sdb_strbuf_memcpy */
+ssize_t
+sdb_strbuf_read(sdb_strbuf_t *strbuf, int fd, size_t n)
+{
+ if (! strbuf)
+ return -1;
+
+ if (strbuf_resize(strbuf, strbuf->pos + n + 1))
+ return -1;
+
+ return read(fd, strbuf->string + strbuf->pos, n);
+} /* sdb_strbuf_read */
+
ssize_t
sdb_strbuf_chomp(sdb_strbuf_t *strbuf)
{