3 * $Id: dstr.h,v 1.10 1999/12/22 15:39:51 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.10 1999/12/22 15:39:51 mdw
34 * Fix argument reuse in DPUTS.
36 * Revision 1.9 1999/12/10 23:42:04 mdw
37 * Change header file guard names.
39 * Revision 1.8 1999/07/14 19:45:24 mdw
40 * Prevent some macros from re-evaluating their arguments.
42 * Revision 1.7 1999/05/21 22:12:12 mdw
43 * Fix the bugs in the new macros. (Whoops.)
45 * Revision 1.6 1999/05/21 08:38:14 mdw
46 * Add some more macros, particularly for creation and destruction.
48 * Revision 1.5 1999/05/13 22:47:57 mdw
49 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
51 * Revision 1.4 1999/05/06 19:51:35 mdw
52 * Reformatted the LGPL notice a little bit.
54 * Revision 1.3 1999/05/05 18:50:31 mdw
55 * Change licensing conditions to LGPL.
57 * Revision 1.2 1998/12/15 23:53:23 mdw
58 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
59 * formatting in a safe way.
61 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
62 * Initial version of mLib
73 /*----- Rationale ---------------------------------------------------------*
75 * This file declares what is hopefully a fairly useful collection of
76 * primitive string handling functions. The idea is that the strings
77 * allocate memory for themselves as required. The @dstr@ routines don't
78 * assume any sort of terminator character, so arbitrary binary data can
79 * be stored in a dynamic string. With luck, this should put a stop to
80 * any buffer overflow problems.
83 /*----- Header files ------------------------------------------------------*/
89 /*----- Data structures ---------------------------------------------------*/
92 char *buf; /* Pointer to string buffer */
93 size_t sz; /* Size of the buffer */
94 size_t len; /* Length of the string */
97 #define DSTR_INIT { 0, 0, 0 } /* How to initialize one */
99 /*----- Functions provided ------------------------------------------------*/
101 /* --- @dstr_create@ --- *
103 * Arguments: @dstr *d@ = pointer to a dynamic string block
107 * Use: Initializes a dynamic string.
110 extern void dstr_create(dstr */*d*/);
112 #define DCREATE(d) do { \
119 /* --- @dstr_destroy@ --- *
121 * Arguments: @dstr *d@ = pointer to a dynamic string block
125 * Use: Reclaims the space used by a dynamic string.
128 extern void dstr_destroy(dstr */*d*/);
130 #define DDESTROY(d) do { \
137 /* --- @dstr_reset@ --- *
139 * Arguments: @dstr *d@ = pointer to a dynamic string block
143 * Use: Resets a string so that new data gets put at the beginning.
146 extern void dstr_reset(dstr */*d*/);
148 #define DRESET(d) ((d)->len = 0)
150 /* --- @dstr_ensure@ --- *
152 * Arguments: @dstr *d@ = pointer to a dynamic string block
153 * @size_t sz@ = amount of free space to ensure
157 * Use: Ensures that at least @sz@ bytes are available in the
161 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
163 #define DENSURE(d, rq) do { \
166 if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq); \
169 /* --- @dstr_putc@ --- *
171 * Arguments: @dstr *d@ = pointer to a dynamic string block
172 * @char ch@ = character to append
176 * Use: Appends a character to a string.
179 extern void dstr_putc(dstr */*d*/, char /*ch*/);
181 #define DPUTC(d, ch) do { \
184 _d->buf[_d->len++] = (ch); \
187 /* --- @dstr_putz@ --- *
189 * Arguments: @dstr *d@ = pointer to a dynamic string block
193 * Use: Appends a null byte to a string. The null byte does not
194 * contribute to the string's length, and will be overwritten
195 * by subsequent `put' operations.
198 extern void dstr_putz(dstr */*d*/);
200 #define DPUTZ(d) do { \
203 _d->buf[_d->len] = 0; \
206 /* --- @dstr_puts@ --- *
208 * Arguments: @dstr *d@ = pointer to a dynamic string block
209 * @const char *s@ = pointer to string to append
213 * Use: Appends a character string to a string. A trailing null
214 * byte is added, as for @dstr_putz@.
217 extern void dstr_puts(dstr */*d*/, const char */*s*/);
219 #define DPUTS(d, s) do { \
221 const char *_s = (s); \
222 size_t _sz = strlen(_s); \
223 DENSURE(_d, _sz + 1); \
224 memcpy(_d->buf + _d->len, _s, _sz + 1); \
228 /* --- @dstr_vputf@ --- *
230 * Arguments: @dstr *d@ = pointer to a dynamic string block
231 * @const char *p@ = pointer to @printf@-style format string
232 * @va_list ap@ = argument handle
234 * Returns: The number of characters written to the string.
236 * Use: As for @dstr_putf@, but may be used as a back-end to user-
237 * supplied functions with @printf@-style interfaces.
240 extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list /*ap*/);
242 /* --- @dstr_putf@ --- *
244 * Arguments: @dstr *d@ = pointer to a dynamic string block
245 * @const char *p@ = pointer to @printf@-style format string
246 * @...@ = argument handle
248 * Returns: The number of characters written to the string.
250 * Use: Writes a piece of text to a dynamic string, doing @printf@-
251 * style substitutions as it goes. Intended to be robust if
252 * faced with malicious arguments, but not if the format string
253 * itself is malicious.
256 extern int dstr_putf(dstr */*d*/, const char */*p*/, ...);
258 /* --- @dstr_putd@ --- *
260 * Arguments: @dstr *d@ = pointer to a dynamic string block
261 * @const dstr *s@ = pointer to a dynamic string to append
265 * Use: Appends a dynamic string to a string. A trailing null
266 * byte is added, as for @dstr_putz@.
269 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
271 #define DPUTD(d, s) do { \
273 const dstr *_s = (s); \
274 DENSURE(_d, _s->len + 1); \
275 memcpy(_d->buf + _d->len, _s->buf, _s->len); \
276 _d->len += _s->len; \
277 _d->buf[_d->len] = 0; \
280 /* --- @dstr_putm@ --- *
282 * Arguments: @dstr *d@ = pointer to a dynamic string block
283 * @const void *p@ = pointer to a block to append
284 * @size_t sz@ = size of the block
286 * Returns: Appends an arbitrary data block to a string. No trailing
290 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
292 #define DPUTM(d, p, sz) do { \
296 memcpy(_d->buf + _d->len, (p), _sz); \
300 /* --- @dstr_tidy@ --- *
302 * Arguments: @dstr *d@ = pointer to a dynamic string block
306 * Use: Reduces the amount of memory used by a string. A trailing
307 * null byte is added, as for @dstr_putz@.
310 extern void dstr_tidy(dstr */*d*/);
312 /* --- @dstr_putline@ --- *
314 * Arguments: @dstr *d@ = pointer to a dynamic string block
315 * @FILE *fp@ = a stream to read from
317 * Returns: The number of characters read into the buffer, or @EOF@ if
318 * end-of-file was reached before any characters were read.
320 * Use: Appends the next line from the given input stream to the
321 * string. A trailing newline is not added; a trailing null
322 * byte is appended, as for @dstr_putz@.
325 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
327 /* --- @dstr_write@ --- *
329 * Arguments: @dstr *d@ = pointer to a dynamic string block
330 * @FILE *fp@ = a stream to write on
332 * Returns: The number of bytes written (as for @fwrite@).
334 * Use: Writes a dynamic string to a file.
337 extern size_t dstr_write(const dstr */*d*/, FILE */*fp*/);
339 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
341 /*----- That's all, folks -------------------------------------------------*/