aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-09-14 21:14:53 +0200
committerMarc André Tanner <mat@brain-dump.org>2015-09-15 09:09:15 +0200
commite57595741285eebf1aac2f2ceac19cae5d949612 (patch)
treed705dc5812a27e39730917b8cdea0ec1a2458688
parentf20ea432e67d3b0223129b418613c74b3beae450 (diff)
downloadvis-e57595741285eebf1aac2f2ceac19cae5d949612.tar.gz
vis-e57595741285eebf1aac2f2ceac19cae5d949612.tar.xz
buffer: add buffer_put0 to store a NUL terminated string
-rw-r--r--buffer.c6
-rw-r--r--buffer.h2
2 files changed, 7 insertions, 1 deletions
diff --git a/buffer.c b/buffer.c
index 05e8177..7784785 100644
--- a/buffer.c
+++ b/buffer.c
@@ -39,11 +39,15 @@ void buffer_release(Buffer *buf) {
bool buffer_put(Buffer *buf, const void *data, size_t len) {
if (!buffer_grow(buf, len))
return false;
- memcpy(buf->data, data, len);
+ memmove(buf->data, data, len);
buf->len = len;
return true;
}
+bool buffer_put0(Buffer *buf, const char *data) {
+ return buffer_put(buf, data, strlen(data)+1);
+}
+
bool buffer_append(Buffer *buf, const void *data, size_t len) {
size_t rem = buf->size - buf->len;
if (len > rem && !buffer_grow(buf, buf->size + len - rem))
diff --git a/buffer.h b/buffer.h
index 23a92ba..088248b 100644
--- a/buffer.h
+++ b/buffer.h
@@ -21,6 +21,8 @@ bool buffer_grow(Buffer*, size_t size);
void buffer_truncate(Buffer*);
/* replace buffer content with given data, growing the buffer if needed */
bool buffer_put(Buffer*, const void *data, size_t len);
+/* same but with NUL-terminated data */
+bool buffer_put0(Buffer*, const char *data);
/* append futher content to the end of the buffer data */
bool buffer_append(Buffer*, const void *data, size_t len);
/* append NUl-terminated data */