From: Sebastian Harl Date: Tue, 29 Oct 2013 09:06:42 +0000 (+0100) Subject: utils strbuf: Added sdb_strbuf_read(). X-Git-Tag: sysdb-0.1.0~336^2~24 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=621126eb1c628c3328e0db0bb8baa18f8b88e2bb;p=sysdb.git utils strbuf: Added sdb_strbuf_read(). This function may be used to read from an open file into the buffer. --- diff --git a/src/include/utils/strbuf.h b/src/include/utils/strbuf.h index 6ad9e1e..ebbd9e3 100644 --- a/src/include/utils/strbuf.h +++ b/src/include/utils/strbuf.h @@ -111,6 +111,19 @@ sdb_strbuf_memcpy(sdb_strbuf_t *strbuf, const void *data, size_t n); 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 993b086..bffcfc6 100644 --- a/src/utils/strbuf.c +++ b/src/utils/strbuf.c @@ -34,6 +34,8 @@ #include #include +#include + /* * private data structures */ @@ -233,6 +235,18 @@ sdb_strbuf_memcpy(sdb_strbuf_t *strbuf, const void *data, size_t n) 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) {