Mercurial > ~dholland > hg > ag > index.cgi
comparison help2html/buffer.c @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 #include <stdlib.h> | |
2 #include <string.h> | |
3 #include <assert.h> | |
4 | |
5 #include "must.h" | |
6 #include "buffer.h" | |
7 | |
8 static void buffer_provide(struct buffer *b, size_t needed) { | |
9 if (needed <= b->allocsize) { | |
10 return; | |
11 } | |
12 size_t newalloc = b->allocsize*2; | |
13 if (newalloc < 16) { | |
14 newalloc = 16; | |
15 } | |
16 while (newalloc < needed) { | |
17 newalloc *= 2; | |
18 } | |
19 | |
20 b->text = must_realloc(b->text, newalloc); | |
21 b->allocsize = newalloc; | |
22 } | |
23 | |
24 void buffer_clear(struct buffer *b) { | |
25 b->len = 0; | |
26 b->text[b->len] = 0; | |
27 } | |
28 | |
29 void buffer_init(struct buffer *b) { | |
30 b->allocsize = 16; | |
31 b->text = must_malloc(b->allocsize); | |
32 buffer_clear(b); | |
33 } | |
34 | |
35 void buffer_cleanup(struct buffer *b) { | |
36 free(b->text); | |
37 b->text = NULL; | |
38 b->len = 0; | |
39 b->allocsize = 0; | |
40 } | |
41 | |
42 void buffer_append(struct buffer *b, const char *t) { | |
43 size_t tlen = strlen(t); | |
44 buffer_provide(b, b->len + tlen + 1); | |
45 strcpy(b->text + b->len, t); | |
46 b->len += tlen; | |
47 } | |
48 | |
49 void buffer_add(struct buffer *b, int ch) { | |
50 buffer_provide(b, b->len + 2); | |
51 b->text[b->len++] = ch; | |
52 b->text[b->len] = 0; | |
53 } | |
54 | |
55 void buffer_start(struct buffer *b, int ch) { | |
56 buffer_clear(b); | |
57 buffer_add(b, ch); | |
58 } |