blob: 6bbb74ccb8a2658c58e3700d6241f0752d5eb018 (
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
33
34
35
36
37
38
39
40
41
42
43
44
|
#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_delete(struct textbuf *textbuf)
{
textbuf->buffer[textbuf->length] = '\0';
textbuf->length--;
}
void textbuf_clear(struct textbuf *textbuf)
{
textbuf_free(textbuf);
*textbuf = textbuf_init();
}
void textbuf_free(struct textbuf *textbuf)
{
free(textbuf->buffer);
}
|