From 723876da5741892530cd74112ec1510124e95cf9 Mon Sep 17 00:00:00 2001 From: cflip Date: Wed, 25 Jan 2023 11:28:09 -0700 Subject: Rename `append_buffer` to textbuf This name is a little bit better I think, and it will be nice to have a distinction between this utility and the 'file' kind of buffer. --- textbuf.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 textbuf.c (limited to 'textbuf.c') diff --git a/textbuf.c b/textbuf.c new file mode 100644 index 0000000..e68ddbd --- /dev/null +++ b/textbuf.c @@ -0,0 +1,32 @@ +#include "textbuf.h" + +#include +#include + +#include "error.h" + +struct textbuf textbuf_init() +{ + struct textbuf result; + result.buffer = NULL; + result.length = 0; + return result; +} + +void textbuf_append(struct textbuf *textbuf, const char *str, int len) +{ + char *new = realloc(textbuf->buffer, textbuf->length + len); + if (new == NULL) { + fatal_error("Failed to reallocate textbuf!"); + return; + } + + memcpy(&new[textbuf->length], str, len); + textbuf->buffer = new; + textbuf->length += len; +} + +void textbuf_free(struct textbuf *textbuf) +{ + free(textbuf->buffer); +} -- cgit v1.2.3