Commit | Line | Data |
---|---|---|
0875b58f | 1 | /* -*-c-*- |
2 | * | |
0875b58f | 3 | * Handle dynamically growing strings |
4 | * | |
5 | * (c) 1998 Straylight/Edgeware | |
6 | */ | |
7 | ||
d4efbcd9 | 8 | /*----- Licensing notice --------------------------------------------------* |
0875b58f | 9 | * |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
12 | * mLib is free software; you can redistribute it and/or modify | |
c846879c | 13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
d4efbcd9 | 16 | * |
0875b58f | 17 | * mLib is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
c846879c | 20 | * GNU Library General Public License for more details. |
d4efbcd9 | 21 | * |
c846879c | 22 | * You should have received a copy of the GNU Library General Public |
0bd98442 | 23 | * License along with mLib; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
0875b58f | 26 | */ |
27 | ||
c6e0eaf0 | 28 | #ifndef MLIB_DSTR_H |
29 | #define MLIB_DSTR_H | |
0875b58f | 30 | |
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
35 | /*----- Rationale ---------------------------------------------------------* | |
36 | * | |
37 | * This file declares what is hopefully a fairly useful collection of | |
38 | * primitive string handling functions. The idea is that the strings | |
39 | * allocate memory for themselves as required. The @dstr@ routines don't | |
40 | * assume any sort of terminator character, so arbitrary binary data can | |
41 | * be stored in a dynamic string. With luck, this should put a stop to | |
42 | * any buffer overflow problems. | |
43 | */ | |
44 | ||
45 | /*----- Header files ------------------------------------------------------*/ | |
46 | ||
00c7638b | 47 | #include <stdarg.h> |
0875b58f | 48 | #include <stdio.h> |
c44b6bde | 49 | #include <stdlib.h> |
0875b58f | 50 | |
20eb516f | 51 | #ifndef MLIB_ALLOC_H |
52 | # include "alloc.h" | |
53 | #endif | |
54 | ||
55 | #ifndef MLIB_ARENA_H | |
56 | # include "arena.h" | |
57 | #endif | |
58 | ||
3832000d MW |
59 | #ifndef MLIB_MACROS_H |
60 | # include "macros.h" | |
61 | #endif | |
62 | ||
0875b58f | 63 | /*----- Data structures ---------------------------------------------------*/ |
64 | ||
65 | typedef struct dstr { | |
66 | char *buf; /* Pointer to string buffer */ | |
67 | size_t sz; /* Size of the buffer */ | |
68 | size_t len; /* Length of the string */ | |
20eb516f | 69 | arena *a; /* Pointer to arena */ |
0875b58f | 70 | } dstr; |
71 | ||
20eb516f | 72 | #define DSTR_INIT { 0, 0, 0, &arena_stdlib } /* How to initialize one */ |
c44b6bde | 73 | |
0875b58f | 74 | /*----- Functions provided ------------------------------------------------*/ |
75 | ||
76 | /* --- @dstr_create@ --- * | |
77 | * | |
78 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
79 | * | |
80 | * Returns: --- | |
81 | * | |
96c5fe33 | 82 | * Use: Initializes a dynamic string. |
0875b58f | 83 | */ |
84 | ||
85 | extern void dstr_create(dstr */*d*/); | |
86 | ||
c44b6bde | 87 | #define DCREATE(d) do { \ |
077c6597 | 88 | dstr *_dd = (d); \ |
89 | _dd->buf = 0; \ | |
90 | _dd->sz = 0; \ | |
91 | _dd->len = 0; \ | |
20eb516f | 92 | _dd->a = &arena_stdlib; \ |
c44b6bde | 93 | } while (0) |
94 | ||
0875b58f | 95 | /* --- @dstr_destroy@ --- * |
96 | * | |
97 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
98 | * | |
99 | * Returns: --- | |
100 | * | |
101 | * Use: Reclaims the space used by a dynamic string. | |
102 | */ | |
103 | ||
104 | extern void dstr_destroy(dstr */*d*/); | |
105 | ||
c44b6bde | 106 | #define DDESTROY(d) do { \ |
077c6597 | 107 | dstr *_d = (d); \ |
108 | if (_d->buf) \ | |
20eb516f | 109 | x_free(_d->a, _d->buf); \ |
077c6597 | 110 | DCREATE(_d); \ |
c44b6bde | 111 | } while (0) |
112 | ||
0875b58f | 113 | /* --- @dstr_reset@ --- * |
114 | * | |
c6e0eaf0 | 115 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
0875b58f | 116 | * |
117 | * Returns: --- | |
118 | * | |
119 | * Use: Resets a string so that new data gets put at the beginning. | |
120 | */ | |
121 | ||
122 | extern void dstr_reset(dstr */*d*/); | |
123 | ||
bd3d316a | 124 | #define DRESET(d) ((d)->len = 0) |
c44b6bde | 125 | |
0875b58f | 126 | /* --- @dstr_ensure@ --- * |
127 | * | |
128 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
129 | * @size_t sz@ = amount of free space to ensure | |
130 | * | |
131 | * Returns: --- | |
132 | * | |
133 | * Use: Ensures that at least @sz@ bytes are available in the | |
134 | * given string. | |
135 | */ | |
136 | ||
137 | extern void dstr_ensure(dstr */*d*/, size_t /*sz*/); | |
138 | ||
139 | #define DENSURE(d, rq) do { \ | |
077c6597 | 140 | dstr *_dd = (d); \ |
141 | size_t _rq = (rq); \ | |
142 | if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq); \ | |
0875b58f | 143 | } while (0) |
144 | ||
145 | /* --- @dstr_putc@ --- * | |
146 | * | |
147 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
2be33c7c | 148 | * @int ch@ = character to append |
0875b58f | 149 | * |
150 | * Returns: --- | |
151 | * | |
152 | * Use: Appends a character to a string. | |
153 | */ | |
154 | ||
2be33c7c | 155 | extern void dstr_putc(dstr */*d*/, int /*ch*/); |
0875b58f | 156 | |
157 | #define DPUTC(d, ch) do { \ | |
077c6597 | 158 | dstr *_d = (d); \ |
159 | DENSURE(_d, 1); \ | |
2be33c7c | 160 | *((unsigned char *)_d->buf + _d->len++) = (ch); \ |
0875b58f | 161 | } while (0) |
162 | ||
163 | /* --- @dstr_putz@ --- * | |
164 | * | |
165 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
166 | * | |
167 | * Returns: --- | |
168 | * | |
169 | * Use: Appends a null byte to a string. The null byte does not | |
170 | * contribute to the string's length, and will be overwritten | |
171 | * by subsequent `put' operations. | |
172 | */ | |
173 | ||
174 | extern void dstr_putz(dstr */*d*/); | |
175 | ||
176 | #define DPUTZ(d) do { \ | |
077c6597 | 177 | dstr *_d = (d); \ |
178 | DENSURE(_d, 1); \ | |
179 | _d->buf[_d->len] = 0; \ | |
0875b58f | 180 | } while (0) |
181 | ||
182 | /* --- @dstr_puts@ --- * | |
183 | * | |
184 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
185 | * @const char *s@ = pointer to string to append | |
186 | * | |
187 | * Returns: --- | |
188 | * | |
189 | * Use: Appends a character string to a string. A trailing null | |
190 | * byte is added, as for @dstr_putz@. | |
191 | */ | |
192 | ||
193 | extern void dstr_puts(dstr */*d*/, const char */*s*/); | |
194 | ||
195 | #define DPUTS(d, s) do { \ | |
077c6597 | 196 | dstr *_d = (d); \ |
197 | const char *_s = (s); \ | |
c53c8c86 | 198 | size_t _sz = strlen(_s); \ |
077c6597 | 199 | DENSURE(_d, _sz + 1); \ |
200 | memcpy(_d->buf + _d->len, _s, _sz + 1); \ | |
201 | _d->len += _sz; \ | |
0875b58f | 202 | } while (0) |
203 | ||
00c7638b | 204 | /* --- @dstr_vputf@ --- * |
205 | * | |
206 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
207 | * @const char *p@ = pointer to @printf@-style format string | |
5a18a126 | 208 | * @va_list *ap@ = argument handle |
00c7638b | 209 | * |
96c5fe33 | 210 | * Returns: The number of characters written to the string. |
00c7638b | 211 | * |
212 | * Use: As for @dstr_putf@, but may be used as a back-end to user- | |
213 | * supplied functions with @printf@-style interfaces. | |
214 | */ | |
215 | ||
5a18a126 | 216 | extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list */*ap*/); |
00c7638b | 217 | |
218 | /* --- @dstr_putf@ --- * | |
219 | * | |
220 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
221 | * @const char *p@ = pointer to @printf@-style format string | |
222 | * @...@ = argument handle | |
223 | * | |
96c5fe33 | 224 | * Returns: The number of characters written to the string. |
00c7638b | 225 | * |
226 | * Use: Writes a piece of text to a dynamic string, doing @printf@- | |
227 | * style substitutions as it goes. Intended to be robust if | |
228 | * faced with malicious arguments, but not if the format string | |
229 | * itself is malicious. | |
230 | */ | |
231 | ||
3832000d MW |
232 | extern int PRINTF_LIKE(2, 3) |
233 | dstr_putf(dstr */*d*/, const char */*p*/, ...); | |
00c7638b | 234 | |
0875b58f | 235 | /* --- @dstr_putd@ --- * |
236 | * | |
237 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
238 | * @const dstr *s@ = pointer to a dynamic string to append | |
239 | * | |
240 | * Returns: --- | |
241 | * | |
242 | * Use: Appends a dynamic string to a string. A trailing null | |
243 | * byte is added, as for @dstr_putz@. | |
244 | */ | |
245 | ||
246 | extern void dstr_putd(dstr */*d*/, const dstr */*s*/); | |
247 | ||
248 | #define DPUTD(d, s) do { \ | |
077c6597 | 249 | dstr *_d = (d); \ |
250 | const dstr *_s = (s); \ | |
251 | DENSURE(_d, _s->len + 1); \ | |
252 | memcpy(_d->buf + _d->len, _s->buf, _s->len); \ | |
253 | _d->len += _s->len; \ | |
254 | _d->buf[_d->len] = 0; \ | |
0875b58f | 255 | } while (0) |
256 | ||
257 | /* --- @dstr_putm@ --- * | |
258 | * | |
259 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
260 | * @const void *p@ = pointer to a block to append | |
261 | * @size_t sz@ = size of the block | |
262 | * | |
263 | * Returns: Appends an arbitrary data block to a string. No trailing | |
264 | * null is appended. | |
265 | */ | |
266 | ||
267 | extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/); | |
268 | ||
269 | #define DPUTM(d, p, sz) do { \ | |
077c6597 | 270 | dstr *_d = (d); \ |
271 | size_t _sz = (sz); \ | |
272 | DENSURE(_d, _sz); \ | |
273 | memcpy(_d->buf + _d->len, (p), _sz); \ | |
274 | _d->len += _sz; \ | |
0875b58f | 275 | } while (0) |
276 | ||
277 | /* --- @dstr_tidy@ --- * | |
278 | * | |
279 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
280 | * | |
281 | * Returns: --- | |
282 | * | |
283 | * Use: Reduces the amount of memory used by a string. A trailing | |
284 | * null byte is added, as for @dstr_putz@. | |
285 | */ | |
286 | ||
287 | extern void dstr_tidy(dstr */*d*/); | |
288 | ||
289 | /* --- @dstr_putline@ --- * | |
290 | * | |
291 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
292 | * @FILE *fp@ = a stream to read from | |
293 | * | |
294 | * Returns: The number of characters read into the buffer, or @EOF@ if | |
295 | * end-of-file was reached before any characters were read. | |
296 | * | |
297 | * Use: Appends the next line from the given input stream to the | |
298 | * string. A trailing newline is not added; a trailing null | |
299 | * byte is appended, as for @dstr_putz@. | |
300 | */ | |
301 | ||
302 | extern int dstr_putline(dstr */*d*/, FILE */*fp*/); | |
303 | ||
304 | /* --- @dstr_write@ --- * | |
305 | * | |
306 | * Arguments: @dstr *d@ = pointer to a dynamic string block | |
307 | * @FILE *fp@ = a stream to write on | |
308 | * | |
309 | * Returns: The number of bytes written (as for @fwrite@). | |
310 | * | |
311 | * Use: Writes a dynamic string to a file. | |
312 | */ | |
313 | ||
96c5fe33 | 314 | extern size_t dstr_write(const dstr */*d*/, FILE */*fp*/); |
0875b58f | 315 | |
c44b6bde | 316 | #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp)) |
317 | ||
0875b58f | 318 | /*----- That's all, folks -------------------------------------------------*/ |
319 | ||
320 | #ifdef __cplusplus | |
321 | } | |
322 | #endif | |
323 | ||
324 | #endif |