3 * $Id: dstr.h,v 1.11 2000/06/17 10:37:39 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.11 2000/06/17 10:37:39 mdw
34 * Add support for arena management.
36 * Revision 1.10 1999/12/22 15:39:51 mdw
37 * Fix argument reuse in DPUTS.
39 * Revision 1.9 1999/12/10 23:42:04 mdw
40 * Change header file guard names.
42 * Revision 1.8 1999/07/14 19:45:24 mdw
43 * Prevent some macros from re-evaluating their arguments.
45 * Revision 1.7 1999/05/21 22:12:12 mdw
46 * Fix the bugs in the new macros. (Whoops.)
48 * Revision 1.6 1999/05/21 08:38:14 mdw
49 * Add some more macros, particularly for creation and destruction.
51 * Revision 1.5 1999/05/13 22:47:57 mdw
52 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
54 * Revision 1.4 1999/05/06 19:51:35 mdw
55 * Reformatted the LGPL notice a little bit.
57 * Revision 1.3 1999/05/05 18:50:31 mdw
58 * Change licensing conditions to LGPL.
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.
64 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
65 * Initial version of mLib
76 /*----- Rationale ---------------------------------------------------------*
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.
86 /*----- Header files ------------------------------------------------------*/
100 /*----- Data structures ---------------------------------------------------*/
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 */
106 arena
*a
; /* Pointer to arena */
109 #define DSTR_INIT { 0, 0, 0, &arena_stdlib } /* How to initialize one */
111 /*----- Functions provided ------------------------------------------------*/
113 /* --- @dstr_create@ --- *
115 * Arguments: @dstr *d@ = pointer to a dynamic string block
119 * Use: Initializes a dynamic string.
122 extern void dstr_create(dstr */
*d*/
);
124 #define DCREATE(d) do { \
129 _dd->a = &arena_stdlib; \
132 /* --- @dstr_destroy@ --- *
134 * Arguments: @dstr *d@ = pointer to a dynamic string block
138 * Use: Reclaims the space used by a dynamic string.
141 extern void dstr_destroy(dstr */
*d*/
);
143 #define DDESTROY(d) do { \
146 x_free(_d->a, _d->buf); \
150 /* --- @dstr_reset@ --- *
152 * Arguments: @dstr *d@ = pointer to a dynamic string block
156 * Use: Resets a string so that new data gets put at the beginning.
159 extern void dstr_reset(dstr */
*d*/
);
161 #define DRESET(d) ((d)->len = 0)
163 /* --- @dstr_ensure@ --- *
165 * Arguments: @dstr *d@ = pointer to a dynamic string block
166 * @size_t sz@ = amount of free space to ensure
170 * Use: Ensures that at least @sz@ bytes are available in the
174 extern void dstr_ensure(dstr */
*d*/
, size_t /*sz*/);
176 #define DENSURE(d, rq) do { \
179 if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq); \
182 /* --- @dstr_putc@ --- *
184 * Arguments: @dstr *d@ = pointer to a dynamic string block
185 * @char ch@ = character to append
189 * Use: Appends a character to a string.
192 extern void dstr_putc(dstr */
*d*/
, char /*ch*/);
194 #define DPUTC(d, ch) do { \
197 _d->buf[_d->len++] = (ch); \
200 /* --- @dstr_putz@ --- *
202 * Arguments: @dstr *d@ = pointer to a dynamic string block
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.
211 extern void dstr_putz(dstr */
*d*/
);
213 #define DPUTZ(d) do { \
216 _d->buf[_d->len] = 0; \
219 /* --- @dstr_puts@ --- *
221 * Arguments: @dstr *d@ = pointer to a dynamic string block
222 * @const char *s@ = pointer to string to append
226 * Use: Appends a character string to a string. A trailing null
227 * byte is added, as for @dstr_putz@.
230 extern void dstr_puts(dstr */
*d*/
, const char */
*s*/
);
232 #define DPUTS(d, s) do { \
234 const char *_s = (s); \
235 size_t _sz = strlen(_s); \
236 DENSURE(_d, _sz + 1); \
237 memcpy(_d->buf + _d->len, _s, _sz + 1); \
241 /* --- @dstr_vputf@ --- *
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
247 * Returns: The number of characters written to the string.
249 * Use: As for @dstr_putf@, but may be used as a back-end to user-
250 * supplied functions with @printf@-style interfaces.
253 extern int dstr_vputf(dstr */
*d*/
, const char */
*p*/
, va_list /*ap*/);
255 /* --- @dstr_putf@ --- *
257 * Arguments: @dstr *d@ = pointer to a dynamic string block
258 * @const char *p@ = pointer to @printf@-style format string
259 * @...@ = argument handle
261 * Returns: The number of characters written to the string.
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.
269 extern int dstr_putf(dstr */
*d*/
, const char */
*p*/
, ...);
271 /* --- @dstr_putd@ --- *
273 * Arguments: @dstr *d@ = pointer to a dynamic string block
274 * @const dstr *s@ = pointer to a dynamic string to append
278 * Use: Appends a dynamic string to a string. A trailing null
279 * byte is added, as for @dstr_putz@.
282 extern void dstr_putd(dstr */
*d*/
, const dstr */
*s*/
);
284 #define DPUTD(d, s) do { \
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; \
293 /* --- @dstr_putm@ --- *
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
299 * Returns: Appends an arbitrary data block to a string. No trailing
303 extern void dstr_putm(dstr */
*d*/
, const void */
*p*/
, size_t /*sz*/);
305 #define DPUTM(d, p, sz) do { \
309 memcpy(_d->buf + _d->len, (p), _sz); \
313 /* --- @dstr_tidy@ --- *
315 * Arguments: @dstr *d@ = pointer to a dynamic string block
319 * Use: Reduces the amount of memory used by a string. A trailing
320 * null byte is added, as for @dstr_putz@.
323 extern void dstr_tidy(dstr */
*d*/
);
325 /* --- @dstr_putline@ --- *
327 * Arguments: @dstr *d@ = pointer to a dynamic string block
328 * @FILE *fp@ = a stream to read from
330 * Returns: The number of characters read into the buffer, or @EOF@ if
331 * end-of-file was reached before any characters were read.
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@.
338 extern int dstr_putline(dstr */
*d*/
, FILE */
*fp*/
);
340 /* --- @dstr_write@ --- *
342 * Arguments: @dstr *d@ = pointer to a dynamic string block
343 * @FILE *fp@ = a stream to write on
345 * Returns: The number of bytes written (as for @fwrite@).
347 * Use: Writes a dynamic string to a file.
350 extern size_t dstr_write(const dstr */
*d*/
, FILE */
*fp*/
);
352 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
354 /*----- That's all, folks -------------------------------------------------*/