summaryrefslogtreecommitdiff
path: root/textbuf.c
blob: e68ddbd94b50883e035baf0cc78cec3ac2ea5114 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
}