3 * $Id: dstr.h,v 1.12 2002/01/13 13:30:48 mdw Exp $
5 * Handle dynamically growing strings
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
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.
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
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.12 2002/01/13 13:30:48 mdw
34 * Change interface for @dstr_vputf@.
36 * Revision 1.11 2000/06/17 10:37:39 mdw
37 * Add support for arena management.
39 * Revision 1.10 1999/12/22 15:39:51 mdw
40 * Fix argument reuse in DPUTS.
42 * Revision 1.9 1999/12/10 23:42:04 mdw
43 * Change header file guard names.
45 * Revision 1.8 1999/07/14 19:45:24 mdw
46 * Prevent some macros from re-evaluating their arguments.
48 * Revision 1.7 1999/05/21 22:12:12 mdw
49 * Fix the bugs in the new macros. (Whoops.)
51 * Revision 1.6 1999/05/21 08:38:14 mdw
52 * Add some more macros, particularly for creation and destruction.
54 * Revision 1.5 1999/05/13 22:47:57 mdw
55 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
57 * Revision 1.4 1999/05/06 19:51:35 mdw
58 * Reformatted the LGPL notice a little bit.
60 * Revision 1.3 1999/05/05 18:50:31 mdw
61 * Change licensing conditions to LGPL.
63 * Revision 1.2 1998/12/15 23:53:23 mdw
64 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
65 * formatting in a safe way.
67 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
68 * Initial version of mLib
79 /*----- Rationale ---------------------------------------------------------*
81 * This file declares what is hopefully a fairly useful collection of
82 * primitive string handling functions. The idea is that the strings
83 * allocate memory for themselves as required. The @dstr@ routines don't
84 * assume any sort of terminator character, so arbitrary binary data can
85 * be stored in a dynamic string. With luck, this should put a stop to
86 * any buffer overflow problems.
89 /*----- Header files ------------------------------------------------------*/
103 /*----- Data structures ---------------------------------------------------*/
105 typedef struct dstr {
106 char *buf; /* Pointer to string buffer */
107 size_t sz; /* Size of the buffer */
108 size_t len; /* Length of the string */
109 arena *a; /* Pointer to arena */
112 #define DSTR_INIT { 0, 0, 0, &arena_stdlib } /* How to initialize one */
114 /*----- Functions provided ------------------------------------------------*/
116 /* --- @dstr_create@ --- *
118 * Arguments: @dstr *d@ = pointer to a dynamic string block
122 * Use: Initializes a dynamic string.
125 extern void dstr_create(dstr */*d*/);
127 #define DCREATE(d) do { \
132 _dd->a = &arena_stdlib; \
135 /* --- @dstr_destroy@ --- *
137 * Arguments: @dstr *d@ = pointer to a dynamic string block
141 * Use: Reclaims the space used by a dynamic string.
144 extern void dstr_destroy(dstr */*d*/);
146 #define DDESTROY(d) do { \
149 x_free(_d->a, _d->buf); \
153 /* --- @dstr_reset@ --- *
155 * Arguments: @dstr *d@ = pointer to a dynamic string block
159 * Use: Resets a string so that new data gets put at the beginning.
162 extern void dstr_reset(dstr */*d*/);
164 #define DRESET(d) ((d)->len = 0)
166 /* --- @dstr_ensure@ --- *
168 * Arguments: @dstr *d@ = pointer to a dynamic string block
169 * @size_t sz@ = amount of free space to ensure
173 * Use: Ensures that at least @sz@ bytes are available in the
177 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
179 #define DENSURE(d, rq) do { \
182 if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq); \
185 /* --- @dstr_putc@ --- *
187 * Arguments: @dstr *d@ = pointer to a dynamic string block
188 * @char ch@ = character to append
192 * Use: Appends a character to a string.
195 extern void dstr_putc(dstr */*d*/, char /*ch*/);
197 #define DPUTC(d, ch) do { \
200 _d->buf[_d->len++] = (ch); \
203 /* --- @dstr_putz@ --- *
205 * Arguments: @dstr *d@ = pointer to a dynamic string block
209 * Use: Appends a null byte to a string. The null byte does not
210 * contribute to the string's length, and will be overwritten
211 * by subsequent `put' operations.
214 extern void dstr_putz(dstr */*d*/);
216 #define DPUTZ(d) do { \
219 _d->buf[_d->len] = 0; \
222 /* --- @dstr_puts@ --- *
224 * Arguments: @dstr *d@ = pointer to a dynamic string block
225 * @const char *s@ = pointer to string to append
229 * Use: Appends a character string to a string. A trailing null
230 * byte is added, as for @dstr_putz@.
233 extern void dstr_puts(dstr */*d*/, const char */*s*/);
235 #define DPUTS(d, s) do { \
237 const char *_s = (s); \
238 size_t _sz = strlen(_s); \
239 DENSURE(_d, _sz + 1); \
240 memcpy(_d->buf + _d->len, _s, _sz + 1); \
244 /* --- @dstr_vputf@ --- *
246 * Arguments: @dstr *d@ = pointer to a dynamic string block
247 * @const char *p@ = pointer to @printf@-style format string
248 * @va_list *ap@ = argument handle
250 * Returns: The number of characters written to the string.
252 * Use: As for @dstr_putf@, but may be used as a back-end to user-
253 * supplied functions with @printf@-style interfaces.
256 extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list */*ap*/);
258 /* --- @dstr_putf@ --- *
260 * Arguments: @dstr *d@ = pointer to a dynamic string block
261 * @const char *p@ = pointer to @printf@-style format string
262 * @...@ = argument handle
264 * Returns: The number of characters written to the string.
266 * Use: Writes a piece of text to a dynamic string, doing @printf@-
267 * style substitutions as it goes. Intended to be robust if
268 * faced with malicious arguments, but not if the format string
269 * itself is malicious.
272 extern int dstr_putf(dstr */*d*/, const char */*p*/, ...);
274 /* --- @dstr_putd@ --- *
276 * Arguments: @dstr *d@ = pointer to a dynamic string block
277 * @const dstr *s@ = pointer to a dynamic string to append
281 * Use: Appends a dynamic string to a string. A trailing null
282 * byte is added, as for @dstr_putz@.
285 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
287 #define DPUTD(d, s) do { \
289 const dstr *_s = (s); \
290 DENSURE(_d, _s->len + 1); \
291 memcpy(_d->buf + _d->len, _s->buf, _s->len); \
292 _d->len += _s->len; \
293 _d->buf[_d->len] = 0; \
296 /* --- @dstr_putm@ --- *
298 * Arguments: @dstr *d@ = pointer to a dynamic string block
299 * @const void *p@ = pointer to a block to append
300 * @size_t sz@ = size of the block
302 * Returns: Appends an arbitrary data block to a string. No trailing
306 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
308 #define DPUTM(d, p, sz) do { \
312 memcpy(_d->buf + _d->len, (p), _sz); \
316 /* --- @dstr_tidy@ --- *
318 * Arguments: @dstr *d@ = pointer to a dynamic string block
322 * Use: Reduces the amount of memory used by a string. A trailing
323 * null byte is added, as for @dstr_putz@.
326 extern void dstr_tidy(dstr */*d*/);
328 /* --- @dstr_putline@ --- *
330 * Arguments: @dstr *d@ = pointer to a dynamic string block
331 * @FILE *fp@ = a stream to read from
333 * Returns: The number of characters read into the buffer, or @EOF@ if
334 * end-of-file was reached before any characters were read.
336 * Use: Appends the next line from the given input stream to the
337 * string. A trailing newline is not added; a trailing null
338 * byte is appended, as for @dstr_putz@.
341 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
343 /* --- @dstr_write@ --- *
345 * Arguments: @dstr *d@ = pointer to a dynamic string block
346 * @FILE *fp@ = a stream to write on
348 * Returns: The number of bytes written (as for @fwrite@).
350 * Use: Writes a dynamic string to a file.
353 extern size_t dstr_write(const dstr */*d*/, FILE */*fp*/);
355 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
357 /*----- That's all, folks -------------------------------------------------*/