From 5c5fd62add1244586afa77d51e6c30f354138030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sat, 19 Sep 2015 16:11:27 +0200 Subject: buffer: add functions to prepend data to an existing buffer --- buffer.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'buffer.c') diff --git a/buffer.c b/buffer.c index 7784785..3cdf65a 100644 --- a/buffer.c +++ b/buffer.c @@ -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)); +} -- cgit v1.2.3