3 * $Id: dstr.h,v 1.7 1999/05/21 22:12:12 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.7 1999/05/21 22:12:12 mdw
34 * Fix the bugs in the new macros. (Whoops.)
36 * Revision 1.6 1999/05/21 08:38:14 mdw
37 * Add some more macros, particularly for creation and destruction.
39 * Revision 1.5 1999/05/13 22:47:57 mdw
40 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
42 * Revision 1.4 1999/05/06 19:51:35 mdw
43 * Reformatted the LGPL notice a little bit.
45 * Revision 1.3 1999/05/05 18:50:31 mdw
46 * Change licensing conditions to LGPL.
48 * Revision 1.2 1998/12/15 23:53:23 mdw
49 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
50 * formatting in a safe way.
52 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
53 * Initial version of mLib
64 /*----- Rationale ---------------------------------------------------------*
66 * This file declares what is hopefully a fairly useful collection of
67 * primitive string handling functions. The idea is that the strings
68 * allocate memory for themselves as required. The @dstr@ routines don't
69 * assume any sort of terminator character, so arbitrary binary data can
70 * be stored in a dynamic string. With luck, this should put a stop to
71 * any buffer overflow problems.
74 /*----- Header files ------------------------------------------------------*/
80 /*----- Data structures ---------------------------------------------------*/
83 char *buf; /* Pointer to string buffer */
84 size_t sz; /* Size of the buffer */
85 size_t len; /* Length of the string */
88 #define DSTR_INIT { 0, 0, 0 } /* How to initialize one */
90 /*----- Functions provided ------------------------------------------------*/
92 /* --- @dstr_create@ --- *
94 * Arguments: @dstr *d@ = pointer to a dynamic string block
98 * Use: Initializes a dynamic string.
101 extern void dstr_create(dstr */*d*/);
103 #define DCREATE(d) do { \
109 /* --- @dstr_destroy@ --- *
111 * Arguments: @dstr *d@ = pointer to a dynamic string block
115 * Use: Reclaims the space used by a dynamic string.
118 extern void dstr_destroy(dstr */*d*/);
120 #define DDESTROY(d) do { \
121 if ((d)->buf) free((d)->buf); \
125 /* --- @dstr_reset@ --- *
127 * Arguments: @dstr *d@ = pointer to a dynaimc string block
131 * Use: Resets a string so that new data gets put at the beginning.
134 extern void dstr_reset(dstr */*d*/);
136 #define DRESET(d) ((d)->len = 0)
138 /* --- @dstr_ensure@ --- *
140 * Arguments: @dstr *d@ = pointer to a dynamic string block
141 * @size_t sz@ = amount of free space to ensure
145 * Use: Ensures that at least @sz@ bytes are available in the
149 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
151 #define DENSURE(d, rq) do { \
152 if ((d)->len + (rq) > (d)->sz) dstr_ensure((d), (rq)); \
155 /* --- @dstr_putc@ --- *
157 * Arguments: @dstr *d@ = pointer to a dynamic string block
158 * @char ch@ = character to append
162 * Use: Appends a character to a string.
165 extern void dstr_putc(dstr */*d*/, char /*ch*/);
167 #define DPUTC(d, ch) do { \
169 (d)->buf[(d)->len++] = (ch); \
172 /* --- @dstr_putz@ --- *
174 * Arguments: @dstr *d@ = pointer to a dynamic string block
178 * Use: Appends a null byte to a string. The null byte does not
179 * contribute to the string's length, and will be overwritten
180 * by subsequent `put' operations.
183 extern void dstr_putz(dstr */*d*/);
185 #define DPUTZ(d) do { \
187 (d)->buf[(d)->len] = 0; \
190 /* --- @dstr_puts@ --- *
192 * Arguments: @dstr *d@ = pointer to a dynamic string block
193 * @const char *s@ = pointer to string to append
197 * Use: Appends a character string to a string. A trailing null
198 * byte is added, as for @dstr_putz@.
201 extern void dstr_puts(dstr */*d*/, const char */*s*/);
203 #define DPUTS(d, s) do { \
204 size_t sz = strlen(s); \
205 DENSURE((d), sz + 1); \
206 memcpy((d)->buf + (d)->len, (s), sz + 1); \
210 /* --- @dstr_vputf@ --- *
212 * Arguments: @dstr *d@ = pointer to a dynamic string block
213 * @const char *p@ = pointer to @printf@-style format string
214 * @va_list ap@ = argument handle
216 * Returns: The number of characters written to the string.
218 * Use: As for @dstr_putf@, but may be used as a back-end to user-
219 * supplied functions with @printf@-style interfaces.
222 extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list /*ap*/);
224 /* --- @dstr_putf@ --- *
226 * Arguments: @dstr *d@ = pointer to a dynamic string block
227 * @const char *p@ = pointer to @printf@-style format string
228 * @...@ = argument handle
230 * Returns: The number of characters written to the string.
232 * Use: Writes a piece of text to a dynamic string, doing @printf@-
233 * style substitutions as it goes. Intended to be robust if
234 * faced with malicious arguments, but not if the format string
235 * itself is malicious.
238 extern int dstr_putf(dstr */*d*/, const char */*p*/, ...);
240 /* --- @dstr_putd@ --- *
242 * Arguments: @dstr *d@ = pointer to a dynamic string block
243 * @const dstr *s@ = pointer to a dynamic string to append
247 * Use: Appends a dynamic string to a string. A trailing null
248 * byte is added, as for @dstr_putz@.
251 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
253 #define DPUTD(d, s) do { \
254 DENSURE((d), (s)->len + 1); \
255 memcpy((d)->buf + (d)->len, (s)->buf, (s)->len); \
256 (d)->len += (s)->len; \
257 (d)->buf[(d)->len] = 0; \
260 /* --- @dstr_putm@ --- *
262 * Arguments: @dstr *d@ = pointer to a dynamic string block
263 * @const void *p@ = pointer to a block to append
264 * @size_t sz@ = size of the block
266 * Returns: Appends an arbitrary data block to a string. No trailing
270 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
272 #define DPUTM(d, p, sz) do { \
273 DENSURE((d), (sz)); \
274 memcpy((d)->buf + (d)->len, (p), (sz)); \
278 /* --- @dstr_tidy@ --- *
280 * Arguments: @dstr *d@ = pointer to a dynamic string block
284 * Use: Reduces the amount of memory used by a string. A trailing
285 * null byte is added, as for @dstr_putz@.
288 extern void dstr_tidy(dstr */*d*/);
290 /* --- @dstr_putline@ --- *
292 * Arguments: @dstr *d@ = pointer to a dynamic string block
293 * @FILE *fp@ = a stream to read from
295 * Returns: The number of characters read into the buffer, or @EOF@ if
296 * end-of-file was reached before any characters were read.
298 * Use: Appends the next line from the given input stream to the
299 * string. A trailing newline is not added; a trailing null
300 * byte is appended, as for @dstr_putz@.
303 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
305 /* --- @dstr_write@ --- *
307 * Arguments: @dstr *d@ = pointer to a dynamic string block
308 * @FILE *fp@ = a stream to write on
310 * Returns: The number of bytes written (as for @fwrite@).
312 * Use: Writes a dynamic string to a file.
315 extern size_t dstr_write(const dstr */*d*/, FILE */*fp*/);
317 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
319 /*----- That's all, folks -------------------------------------------------*/