diff options
Diffstat (limited to 'textbuf.c')
-rw-r--r-- | textbuf.c | 32 |
1 files changed, 32 insertions, 0 deletions
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 <stdlib.h> +#include <string.h> + +#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); +} |