295
|
1 /*
|
|
2 lwcc/strbuf.c
|
|
3
|
|
4 Copyright © 2013 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <stdlib.h>
|
|
23
|
|
24 #include <lw_alloc.h>
|
|
25
|
|
26 #include "strbuf.h"
|
|
27
|
|
28 struct strbuf *strbuf_new(void)
|
|
29 {
|
|
30 struct strbuf *strbuf;
|
|
31
|
|
32 strbuf = lw_alloc(sizeof(struct strbuf));
|
|
33 strbuf -> str = NULL;
|
|
34 strbuf -> bo = 0;
|
|
35 strbuf -> bl = 0;
|
|
36 return strbuf;
|
|
37 }
|
|
38
|
|
39 void strbuf_add(struct strbuf *strbuf, int c)
|
|
40 {
|
|
41 if (strbuf -> bo >= strbuf -> bl)
|
|
42 {
|
|
43 strbuf -> bl += 100;
|
|
44 strbuf -> str = lw_realloc(strbuf -> str, strbuf -> bl);
|
|
45 }
|
|
46 strbuf -> str[strbuf -> bo++] = c;
|
|
47 }
|
|
48
|
|
49 char *strbuf_end(struct strbuf *strbuf)
|
|
50 {
|
|
51 char *rv;
|
|
52
|
|
53 strbuf_add(strbuf, 0);
|
|
54 rv = strbuf -> str;
|
|
55 lw_free(strbuf);
|
|
56 return rv;
|
|
57 }
|