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