aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 */