3 * $Id: dstr.h,v 1.8 1999/07/14 19:45:24 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.8 1999/07/14 19:45:24 mdw
34 * Prevent some macros from re-evaluating their arguments.
36 * Revision 1.7 1999/05/21 22:12:12 mdw
37 * Fix the bugs in the new macros. (Whoops.)
39 * Revision 1.6 1999/05/21 08:38:14 mdw
40 * Add some more macros, particularly for creation and destruction.
42 * Revision 1.5 1999/05/13 22:47:57 mdw
43 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
45 * Revision 1.4 1999/05/06 19:51:35 mdw
46 * Reformatted the LGPL notice a little bit.
48 * Revision 1.3 1999/05/05 18:50:31 mdw
49 * Change licensing conditions to LGPL.
51 * Revision 1.2 1998/12/15 23:53:23 mdw
52 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
53 * formatting in a safe way.
55 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
56 * Initial version of mLib
67 /*----- Rationale ---------------------------------------------------------*
69 * This file declares what is hopefully a fairly useful collection of
70 * primitive string handling functions. The idea is that the strings
71 * allocate memory for themselves as required. The @dstr@ routines don't
72 * assume any sort of terminator character, so arbitrary binary data can
73 * be stored in a dynamic string. With luck, this should put a stop to
74 * any buffer overflow problems.
77 /*----- Header files ------------------------------------------------------*/
83 /*----- Data structures ---------------------------------------------------*/
86 char *buf
; /* Pointer to string buffer */
87 size_t sz
; /* Size of the buffer */
88 size_t len
; /* Length of the string */
91 #define DSTR_INIT { 0, 0, 0 } /* How to initialize one */
93 /*----- Functions provided ------------------------------------------------*/
95 /* --- @dstr_create@ --- *
97 * Arguments: @dstr *d@ = pointer to a dynamic string block
101 * Use: Initializes a dynamic string.
104 extern void dstr_create(dstr */
*d*/
);
106 #define DCREATE(d) do { \
113 /* --- @dstr_destroy@ --- *
115 * Arguments: @dstr *d@ = pointer to a dynamic string block
119 * Use: Reclaims the space used by a dynamic string.
122 extern void dstr_destroy(dstr */
*d*/
);
124 #define DDESTROY(d) do { \
131 /* --- @dstr_reset@ --- *
133 * Arguments: @dstr *d@ = pointer to a dynaimc string block
137 * Use: Resets a string so that new data gets put at the beginning.
140 extern void dstr_reset(dstr */
*d*/
);
142 #define DRESET(d) ((d)->len = 0)
144 /* --- @dstr_ensure@ --- *
146 * Arguments: @dstr *d@ = pointer to a dynamic string block
147 * @size_t sz@ = amount of free space to ensure
151 * Use: Ensures that at least @sz@ bytes are available in the
155 extern void dstr_ensure(dstr */
*d*/
, size_t /*sz*/);
157 #define DENSURE(d, rq) do { \
160 if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq); \
163 /* --- @dstr_putc@ --- *
165 * Arguments: @dstr *d@ = pointer to a dynamic string block
166 * @char ch@ = character to append
170 * Use: Appends a character to a string.
173 extern void dstr_putc(dstr */
*d*/
, char /*ch*/);
175 #define DPUTC(d, ch) do { \
178 _d->buf[_d->len++] = (ch); \
181 /* --- @dstr_putz@ --- *
183 * Arguments: @dstr *d@ = pointer to a dynamic string block
187 * Use: Appends a null byte to a string. The null byte does not
188 * contribute to the string's length, and will be overwritten
189 * by subsequent `put' operations.
192 extern void dstr_putz(dstr */
*d*/
);
194 #define DPUTZ(d) do { \
197 _d->buf[_d->len] = 0; \
200 /* --- @dstr_puts@ --- *
202 * Arguments: @dstr *d@ = pointer to a dynamic string block
203 * @const char *s@ = pointer to string to append
207 * Use: Appends a character string to a string. A trailing null
208 * byte is added, as for @dstr_putz@.
211 extern void dstr_puts(dstr */
*d*/
, const char */
*s*/
);
213 #define DPUTS(d, s) do { \
215 const char *_s = (s); \
216 size_t _sz = strlen(s); \
217 DENSURE(_d, _sz + 1); \
218 memcpy(_d->buf + _d->len, _s, _sz + 1); \
222 /* --- @dstr_vputf@ --- *
224 * Arguments: @dstr *d@ = pointer to a dynamic string block
225 * @const char *p@ = pointer to @printf@-style format string
226 * @va_list ap@ = argument handle
228 * Returns: The number of characters written to the string.
230 * Use: As for @dstr_putf@, but may be used as a back-end to user-
231 * supplied functions with @printf@-style interfaces.
234 extern int dstr_vputf(dstr */
*d*/
, const char */
*p*/
, va_list /*ap*/);
236 /* --- @dstr_putf@ --- *
238 * Arguments: @dstr *d@ = pointer to a dynamic string block
239 * @const char *p@ = pointer to @printf@-style format string
240 * @...@ = argument handle
242 * Returns: The number of characters written to the string.
244 * Use: Writes a piece of text to a dynamic string, doing @printf@-
245 * style substitutions as it goes. Intended to be robust if
246 * faced with malicious arguments, but not if the format string
247 * itself is malicious.
250 extern int dstr_putf(dstr */
*d*/
, const char */
*p*/
, ...);
252 /* --- @dstr_putd@ --- *
254 * Arguments: @dstr *d@ = pointer to a dynamic string block
255 * @const dstr *s@ = pointer to a dynamic string to append
259 * Use: Appends a dynamic string to a string. A trailing null
260 * byte is added, as for @dstr_putz@.
263 extern void dstr_putd(dstr */
*d*/
, const dstr */
*s*/
);
265 #define DPUTD(d, s) do { \
267 const dstr *_s = (s); \
268 DENSURE(_d, _s->len + 1); \
269 memcpy(_d->buf + _d->len, _s->buf, _s->len); \
270 _d->len += _s->len; \
271 _d->buf[_d->len] = 0; \
274 /* --- @dstr_putm@ --- *
276 * Arguments: @dstr *d@ = pointer to a dynamic string block
277 * @const void *p@ = pointer to a block to append
278 * @size_t sz@ = size of the block
280 * Returns: Appends an arbitrary data block to a string. No trailing
284 extern void dstr_putm(dstr */
*d*/
, const void */
*p*/
, size_t /*sz*/);
286 #define DPUTM(d, p, sz) do { \
290 memcpy(_d->buf + _d->len, (p), _sz); \
294 /* --- @dstr_tidy@ --- *
296 * Arguments: @dstr *d@ = pointer to a dynamic string block
300 * Use: Reduces the amount of memory used by a string. A trailing
301 * null byte is added, as for @dstr_putz@.
304 extern void dstr_tidy(dstr */
*d*/
);
306 /* --- @dstr_putline@ --- *
308 * Arguments: @dstr *d@ = pointer to a dynamic string block
309 * @FILE *fp@ = a stream to read from
311 * Returns: The number of characters read into the buffer, or @EOF@ if
312 * end-of-file was reached before any characters were read.
314 * Use: Appends the next line from the given input stream to the
315 * string. A trailing newline is not added; a trailing null
316 * byte is appended, as for @dstr_putz@.
319 extern int dstr_putline(dstr */
*d*/
, FILE */
*fp*/
);
321 /* --- @dstr_write@ --- *
323 * Arguments: @dstr *d@ = pointer to a dynamic string block
324 * @FILE *fp@ = a stream to write on
326 * Returns: The number of bytes written (as for @fwrite@).
328 * Use: Writes a dynamic string to a file.
331 extern size_t dstr_write(const dstr */
*d*/
, FILE */
*fp*/
);
333 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
335 /*----- That's all, folks -------------------------------------------------*/