aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-05-11 20:34:23 +0200
committerMarc André Tanner <mat@brain-dump.org>2016-05-18 15:07:02 +0200
commit39b306f235f3faebb047c7cda82746781b9ea2f2 (patch)
treeee32dadea6657c5313febe086bed1e7ae630d421
parent2c72e793c580d3ab149e7724a75b4be164c67aed (diff)
downloadvis-39b306f235f3faebb047c7cda82746781b9ea2f2.tar.gz
vis-39b306f235f3faebb047c7cda82746781b9ea2f2.tar.xz
buffer: add utility function to NUL terminate buffer
-rw-r--r--buffer.c5
-rw-r--r--buffer.h2
2 files changed, 7 insertions, 0 deletions
diff --git a/buffer.c b/buffer.c
index d8b01f7..410a059 100644
--- a/buffer.c
+++ b/buffer.c
@@ -29,6 +29,11 @@ void buffer_truncate(Buffer *buf) {
buf->len = 0;
}
+bool buffer_terminate(Buffer *buf) {
+ return !buf->data || buf->len == 0 || buf->data[buf->len-1] == '\0' ||
+ buffer_append(buf, "\0", 1);
+}
+
void buffer_release(Buffer *buf) {
if (!buf)
return;
diff --git a/buffer.h b/buffer.h
index 1c9f684..6b44a53 100644
--- a/buffer.h
+++ b/buffer.h
@@ -22,6 +22,8 @@ void buffer_clear(Buffer*);
bool buffer_grow(Buffer*, size_t size);
/* truncate buffer, but keep associated memory region for further data */
void buffer_truncate(Buffer*);
+/* if buffer is not empty, make sure it is NUL terminated */
+bool buffer_terminate(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 */