3 * $Id: dstr.h,v 1.9 1999/12/10 23:42:04 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.9 1999/12/10 23:42:04 mdw
34 * Change header file guard names.
36 * Revision 1.8 1999/07/14 19:45:24 mdw
37 * Prevent some macros from re-evaluating their arguments.
39 * Revision 1.7 1999/05/21 22:12:12 mdw
40 * Fix the bugs in the new macros. (Whoops.)
42 * Revision 1.6 1999/05/21 08:38:14 mdw
43 * Add some more macros, particularly for creation and destruction.
45 * Revision 1.5 1999/05/13 22:47:57 mdw
46 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
48 * Revision 1.4 1999/05/06 19:51:35 mdw
49 * Reformatted the LGPL notice a little bit.
51 * Revision 1.3 1999/05/05 18:50:31 mdw
52 * Change licensing conditions to LGPL.
54 * Revision 1.2 1998/12/15 23:53:23 mdw
55 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
56 * formatting in a safe way.
58 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
59 * Initial version of mLib
70 /*----- Rationale ---------------------------------------------------------*
72 * This file declares what is hopefully a fairly useful collection of
73 * primitive string handling functions. The idea is that the strings
74 * allocate memory for themselves as required. The @dstr@ routines don't
75 * assume any sort of terminator character, so arbitrary binary data can
76 * be stored in a dynamic string. With luck, this should put a stop to
77 * any buffer overflow problems.
80 /*----- Header files ------------------------------------------------------*/
86 /*----- Data structures ---------------------------------------------------*/
89 char *buf; /* Pointer to string buffer */
90 size_t sz; /* Size of the buffer */
91 size_t len; /* Length of the string */
94 #define DSTR_INIT { 0, 0, 0 } /* How to initialize one */
96 /*----- Functions provided ------------------------------------------------*/
98 /* --- @dstr_create@ --- *
100 * Arguments: @dstr *d@ = pointer to a dynamic string block
104 * Use: Initializes a dynamic string.
107 extern void dstr_create(dstr */*d*/);
109 #define DCREATE(d) do { \
116 /* --- @dstr_destroy@ --- *
118 * Arguments: @dstr *d@ = pointer to a dynamic string block
122 * Use: Reclaims the space used by a dynamic string.
125 extern void dstr_destroy(dstr */*d*/);
127 #define DDESTROY(d) do { \
134 /* --- @dstr_reset@ --- *
136 * Arguments: @dstr *d@ = pointer to a dynamic string block
140 * Use: Resets a string so that new data gets put at the beginning.
143 extern void dstr_reset(dstr */*d*/);
145 #define DRESET(d) ((d)->len = 0)
147 /* --- @dstr_ensure@ --- *
149 * Arguments: @dstr *d@ = pointer to a dynamic string block
150 * @size_t sz@ = amount of free space to ensure
154 * Use: Ensures that at least @sz@ bytes are available in the
158 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
160 #define DENSURE(d, rq) do { \
163 if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq); \
166 /* --- @dstr_putc@ --- *
168 * Arguments: @dstr *d@ = pointer to a dynamic string block
169 * @char ch@ = character to append
173 * Use: Appends a character to a string.
176 extern void dstr_putc(dstr */*d*/, char /*ch*/);
178 #define DPUTC(d, ch) do { \
181 _d->buf[_d->len++] = (ch); \
184 /* --- @dstr_putz@ --- *
186 * Arguments: @dstr *d@ = pointer to a dynamic string block
190 * Use: Appends a null byte to a string. The null byte does not
191 * contribute to the string's length, and will be overwritten
192 * by subsequent `put' operations.
195 extern void dstr_putz(dstr */*d*/);
197 #define DPUTZ(d) do { \
200 _d->buf[_d->len] = 0; \
203 /* --- @dstr_puts@ --- *
205 * Arguments: @dstr *d@ = pointer to a dynamic string block
206 * @const char *s@ = pointer to string to append
210 * Use: Appends a character string to a string. A trailing null
211 * byte is added, as for @dstr_putz@.
214 extern void dstr_puts(dstr */*d*/, const char */*s*/);
216 #define DPUTS(d, s) do { \
218 const char *_s = (s); \
219 size_t _sz = strlen(s); \
220 DENSURE(_d, _sz + 1); \
221 memcpy(_d->buf + _d->len, _s, _sz + 1); \
225 /* --- @dstr_vputf@ --- *
227 * Arguments: @dstr *d@ = pointer to a dynamic string block
228 * @const char *p@ = pointer to @printf@-style format string
229 * @va_list ap@ = argument handle
231 * Returns: The number of characters written to the string.
233 * Use: As for @dstr_putf@, but may be used as a back-end to user-
234 * supplied functions with @printf@-style interfaces.
237 extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list /*ap*/);
239 /* --- @dstr_putf@ --- *
241 * Arguments: @dstr *d@ = pointer to a dynamic string block
242 * @const char *p@ = pointer to @printf@-style format string
243 * @...@ = argument handle
245 * Returns: The number of characters written to the string.
247 * Use: Writes a piece of text to a dynamic string, doing @printf@-
248 * style substitutions as it goes. Intended to be robust if
249 * faced with malicious arguments, but not if the format string
250 * itself is malicious.
253 extern int dstr_putf(dstr */*d*/, const char */*p*/, ...);
255 /* --- @dstr_putd@ --- *
257 * Arguments: @dstr *d@ = pointer to a dynamic string block
258 * @const dstr *s@ = pointer to a dynamic string to append
262 * Use: Appends a dynamic string to a string. A trailing null
263 * byte is added, as for @dstr_putz@.
266 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
268 #define DPUTD(d, s) do { \
270 const dstr *_s = (s); \
271 DENSURE(_d, _s->len + 1); \
272 memcpy(_d->buf + _d->len, _s->buf, _s->len); \
273 _d->len += _s->len; \
274 _d->buf[_d->len] = 0; \
277 /* --- @dstr_putm@ --- *
279 * Arguments: @dstr *d@ = pointer to a dynamic string block
280 * @const void *p@ = pointer to a block to append
281 * @size_t sz@ = size of the block
283 * Returns: Appends an arbitrary data block to a string. No trailing
287 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
289 #define DPUTM(d, p, sz) do { \
293 memcpy(_d->buf + _d->len, (p), _sz); \
297 /* --- @dstr_tidy@ --- *
299 * Arguments: @dstr *d@ = pointer to a dynamic string block
303 * Use: Reduces the amount of memory used by a string. A trailing
304 * null byte is added, as for @dstr_putz@.
307 extern void dstr_tidy(dstr */*d*/);
309 /* --- @dstr_putline@ --- *
311 * Arguments: @dstr *d@ = pointer to a dynamic string block
312 * @FILE *fp@ = a stream to read from
314 * Returns: The number of characters read into the buffer, or @EOF@ if
315 * end-of-file was reached before any characters were read.
317 * Use: Appends the next line from the given input stream to the
318 * string. A trailing newline is not added; a trailing null
319 * byte is appended, as for @dstr_putz@.
322 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
324 /* --- @dstr_write@ --- *
326 * Arguments: @dstr *d@ = pointer to a dynamic string block
327 * @FILE *fp@ = a stream to write on
329 * Returns: The number of bytes written (as for @fwrite@).
331 * Use: Writes a dynamic string to a file.
334 extern size_t dstr_write(const dstr */*d*/, FILE */*fp*/);
336 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
338 /*----- That's all, folks -------------------------------------------------*/