3 * $Id: dstr.h,v 1.4 1999/05/06 19:51:35 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.4 1999/05/06 19:51:35 mdw
34 * Reformatted the LGPL notice a little bit.
36 * Revision 1.3 1999/05/05 18:50:31 mdw
37 * Change licensing conditions to LGPL.
39 * Revision 1.2 1998/12/15 23:53:23 mdw
40 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
41 * formatting in a safe way.
43 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
44 * Initial version of mLib
55 /*----- Rationale ---------------------------------------------------------*
57 * This file declares what is hopefully a fairly useful collection of
58 * primitive string handling functions. The idea is that the strings
59 * allocate memory for themselves as required. The @dstr@ routines don't
60 * assume any sort of terminator character, so arbitrary binary data can
61 * be stored in a dynamic string. With luck, this should put a stop to
62 * any buffer overflow problems.
65 /*----- Header files ------------------------------------------------------*/
70 /*----- Data structures ---------------------------------------------------*/
73 char *buf; /* Pointer to string buffer */
74 size_t sz; /* Size of the buffer */
75 size_t len; /* Length of the string */
78 /*----- Functions provided ------------------------------------------------*/
80 /* --- @dstr_create@ --- *
82 * Arguments: @dstr *d@ = pointer to a dynamic string block
86 * Use: Initialises a dynamic string.
89 extern void dstr_create(dstr */*d*/);
91 /* --- @dstr_destroy@ --- *
93 * Arguments: @dstr *d@ = pointer to a dynamic string block
97 * Use: Reclaims the space used by a dynamic string.
100 extern void dstr_destroy(dstr */*d*/);
102 /* --- @dstr_reset@ --- *
104 * Arguments: @dstr *d@ = pointer to a dynaimc string block
108 * Use: Resets a string so that new data gets put at the beginning.
111 extern void dstr_reset(dstr */*d*/);
113 /* --- @dstr_ensure@ --- *
115 * Arguments: @dstr *d@ = pointer to a dynamic string block
116 * @size_t sz@ = amount of free space to ensure
120 * Use: Ensures that at least @sz@ bytes are available in the
124 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
126 #define DENSURE(d, rq) do { \
127 if ((d)->len + (rq) > (d)->sz) dstr_ensure((d), (rq)); \
130 /* --- @dstr_putc@ --- *
132 * Arguments: @dstr *d@ = pointer to a dynamic string block
133 * @char ch@ = character to append
137 * Use: Appends a character to a string.
140 extern void dstr_putc(dstr */*d*/, char /*ch*/);
142 #define DPUTC(d, ch) do { \
144 (d)->buf[(d)->len++] = (ch); \
147 /* --- @dstr_putz@ --- *
149 * Arguments: @dstr *d@ = pointer to a dynamic string block
153 * Use: Appends a null byte to a string. The null byte does not
154 * contribute to the string's length, and will be overwritten
155 * by subsequent `put' operations.
158 extern void dstr_putz(dstr */*d*/);
160 #define DPUTZ(d) do { \
162 (d)->buf[(d)->len] = 0; \
165 /* --- @dstr_puts@ --- *
167 * Arguments: @dstr *d@ = pointer to a dynamic string block
168 * @const char *s@ = pointer to string to append
172 * Use: Appends a character string to a string. A trailing null
173 * byte is added, as for @dstr_putz@.
176 extern void dstr_puts(dstr */*d*/, const char */*s*/);
178 #define DPUTS(d, s) do { \
179 size_t sz = strlen(s); \
180 DENSURE((d), sz + 1); \
181 memcpy((d)->buf + (d)->len, (s), sz + 1); \
185 /* --- @dstr_vputf@ --- *
187 * Arguments: @dstr *d@ = pointer to a dynamic string block
188 * @const char *p@ = pointer to @printf@-style format string
189 * @va_list ap@ = argument handle
193 * Use: As for @dstr_putf@, but may be used as a back-end to user-
194 * supplied functions with @printf@-style interfaces.
197 extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list /*ap*/);
199 /* --- @dstr_putf@ --- *
201 * Arguments: @dstr *d@ = pointer to a dynamic string block
202 * @const char *p@ = pointer to @printf@-style format string
203 * @...@ = argument handle
207 * Use: Writes a piece of text to a dynamic string, doing @printf@-
208 * style substitutions as it goes. Intended to be robust if
209 * faced with malicious arguments, but not if the format string
210 * itself is malicious.
213 extern int dstr_putf(dstr */*d*/, const char */*p*/, ...);
215 /* --- @dstr_putd@ --- *
217 * Arguments: @dstr *d@ = pointer to a dynamic string block
218 * @const dstr *s@ = pointer to a dynamic string to append
222 * Use: Appends a dynamic string to a string. A trailing null
223 * byte is added, as for @dstr_putz@.
226 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
228 #define DPUTD(d, s) do { \
229 DENSURE((d), (s)->len + 1); \
230 memcpy((d)->buf + (d)->len, (s)->buf, (s)->len); \
231 (d)->len += (s)->len; \
232 (d)->buf[(d)->len] = 0; \
235 /* --- @dstr_putm@ --- *
237 * Arguments: @dstr *d@ = pointer to a dynamic string block
238 * @const void *p@ = pointer to a block to append
239 * @size_t sz@ = size of the block
241 * Returns: Appends an arbitrary data block to a string. No trailing
245 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
247 #define DPUTM(d, p, sz) do { \
248 DENSURE((d), (sz)); \
249 memcpy((d)->buf + (d)->len, (p), (sz)); \
253 /* --- @dstr_tidy@ --- *
255 * Arguments: @dstr *d@ = pointer to a dynamic string block
259 * Use: Reduces the amount of memory used by a string. A trailing
260 * null byte is added, as for @dstr_putz@.
263 extern void dstr_tidy(dstr */*d*/);
265 /* --- @dstr_putline@ --- *
267 * Arguments: @dstr *d@ = pointer to a dynamic string block
268 * @FILE *fp@ = a stream to read from
270 * Returns: The number of characters read into the buffer, or @EOF@ if
271 * end-of-file was reached before any characters were read.
273 * Use: Appends the next line from the given input stream to the
274 * string. A trailing newline is not added; a trailing null
275 * byte is appended, as for @dstr_putz@.
278 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
280 /* --- @dstr_write@ --- *
282 * Arguments: @dstr *d@ = pointer to a dynamic string block
283 * @FILE *fp@ = a stream to write on
285 * Returns: The number of bytes written (as for @fwrite@).
287 * Use: Writes a dynamic string to a file.
290 extern size_t dstr_write(dstr */*d*/, FILE */*fp*/);
292 /*----- That's all, folks -------------------------------------------------*/