summaryrefslogtreecommitdiff
path: root/textbuf.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-25 11:28:09 -0700
committercflip <cflip@cflip.net>2023-01-25 18:32:43 -0700
commit723876da5741892530cd74112ec1510124e95cf9 (patch)
tree2017e9e488a073b274c485fbd192ba05724c01c7 /textbuf.c
parent69e2be81c732353f5f89389fec3f9bb768b0766a (diff)
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.
Diffstat (limited to 'textbuf.c')
-rw-r--r--textbuf.c32
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);
+}