diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-09-19 16:11:27 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-10-05 16:29:52 +0200 |
| commit | 5c5fd62add1244586afa77d51e6c30f354138030 (patch) | |
| tree | e48eedbff4062158c7dc9da310e0f2b29d0141d6 | |
| parent | 0c17a55a29e4b1c49c0675513a9e991e67d0f6c3 (diff) | |
| download | vis-5c5fd62add1244586afa77d51e6c30f354138030.tar.gz vis-5c5fd62add1244586afa77d51e6c30f354138030.tar.xz | |
buffer: add functions to prepend data to an existing buffer
| -rw-r--r-- | buffer.c | 13 | ||||
| -rw-r--r-- | buffer.h | 3 |
2 files changed, 16 insertions, 0 deletions
@@ -62,3 +62,16 @@ bool buffer_append0(Buffer *buf, const char *data) { buf->len--; return buffer_append(buf, data, strlen(data)) && buffer_append(buf, "\0", 1); } + +bool buffer_prepend(Buffer *buf, const void *data, size_t len) { + if (!buffer_grow(buf, buf->len + len)) + return false; + memmove(buf->data + len, buf->data, buf->len); + memcpy(buf->data, data, len); + buf->len += len; + return true; +} + +bool buffer_prepend0(Buffer *buf, const char *data) { + return buffer_prepend(buf, data, strlen(data) + (buf->len == 0)); +} @@ -28,4 +28,7 @@ bool buffer_append(Buffer*, const void *data, size_t len); /* append NUl-terminated data */ bool buffer_append0(Buffer*, const char *data); +bool buffer_prepend(Buffer*, const void *data, size_t len); +bool buffer_prepend0(Buffer*, const char *data); + #endif |
