vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 555720ed0c1403a6eccfba0a7c47834da35e992a parent be91766e450ea782113a2968e2c266c12f57768d Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 11 May 2016 20:34:23 +0200 buffer: add utility function to NUL terminate buffer Diffstat:
| M | buffer.c | | | 5 | +++++ |
| M | buffer.h | | | 2 | ++ |
2 files changed, 7 insertions(+), 0 deletions(-)
diff --git 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 @@ -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 */