3 * $Id: dstr.h,v 1.6 1999/05/21 08:38:14 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.6 1999/05/21 08:38:14 mdw
34 * Add some more macros, particularly for creation and destruction.
36 * Revision 1.5 1999/05/13 22:47:57 mdw
37 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
39 * Revision 1.4 1999/05/06 19:51:35 mdw
40 * Reformatted the LGPL notice a little bit.
42 * Revision 1.3 1999/05/05 18:50:31 mdw
43 * Change licensing conditions to LGPL.
45 * Revision 1.2 1998/12/15 23:53:23 mdw
46 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
47 * formatting in a safe way.
49 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
50 * Initial version of mLib
61 /*----- Rationale ---------------------------------------------------------*
63 * This file declares what is hopefully a fairly useful collection of
64 * primitive string handling functions. The idea is that the strings
65 * allocate memory for themselves as required. The @dstr@ routines don't
66 * assume any sort of terminator character, so arbitrary binary data can
67 * be stored in a dynamic string. With luck, this should put a stop to
68 * any buffer overflow problems.
71 /*----- Header files ------------------------------------------------------*/
77 /*----- Data structures ---------------------------------------------------*/
80 char *buf
; /* Pointer to string buffer */
81 size_t sz
; /* Size of the buffer */
82 size_t len
; /* Length of the string */
85 #define DSTR_INIT = { 0, 0, 0 }; /* How to initialize one */
87 /*----- Functions provided ------------------------------------------------*/
89 /* --- @dstr_create@ --- *
91 * Arguments: @dstr *d@ = pointer to a dynamic string block
95 * Use: Initializes a dynamic string.
98 extern void dstr_create(dstr */
*d*/
);
100 #define DCREATE(d) do { \
106 /* --- @dstr_destroy@ --- *
108 * Arguments: @dstr *d@ = pointer to a dynamic string block
112 * Use: Reclaims the space used by a dynamic string.
115 extern void dstr_destroy(dstr */
*d*/
);
117 #define DDESTROY(d) do { \
118 if ((d)->buf) free((d)->buf); \
122 /* --- @dstr_reset@ --- *
124 * Arguments: @dstr *d@ = pointer to a dynaimc string block
128 * Use: Resets a string so that new data gets put at the beginning.
131 extern void dstr_reset(dstr */
*d*/
);
133 #define DRESET(d) do (d)->len = 0; while (0)
135 /* --- @dstr_ensure@ --- *
137 * Arguments: @dstr *d@ = pointer to a dynamic string block
138 * @size_t sz@ = amount of free space to ensure
142 * Use: Ensures that at least @sz@ bytes are available in the
146 extern void dstr_ensure(dstr */
*d*/
, size_t /*sz*/);
148 #define DENSURE(d, rq) do { \
149 if ((d)->len + (rq) > (d)->sz) dstr_ensure((d), (rq)); \
152 /* --- @dstr_putc@ --- *
154 * Arguments: @dstr *d@ = pointer to a dynamic string block
155 * @char ch@ = character to append
159 * Use: Appends a character to a string.
162 extern void dstr_putc(dstr */
*d*/
, char /*ch*/);
164 #define DPUTC(d, ch) do { \
166 (d)->buf[(d)->len++] = (ch); \
169 /* --- @dstr_putz@ --- *
171 * Arguments: @dstr *d@ = pointer to a dynamic string block
175 * Use: Appends a null byte to a string. The null byte does not
176 * contribute to the string's length, and will be overwritten
177 * by subsequent `put' operations.
180 extern void dstr_putz(dstr */
*d*/
);
182 #define DPUTZ(d) do { \
184 (d)->buf[(d)->len] = 0; \
187 /* --- @dstr_puts@ --- *
189 * Arguments: @dstr *d@ = pointer to a dynamic string block
190 * @const char *s@ = pointer to string to append
194 * Use: Appends a character string to a string. A trailing null
195 * byte is added, as for @dstr_putz@.
198 extern void dstr_puts(dstr */
*d*/
, const char */
*s*/
);
200 #define DPUTS(d, s) do { \
201 size_t sz = strlen(s); \
202 DENSURE((d), sz + 1); \
203 memcpy((d)->buf + (d)->len, (s), sz + 1); \
207 /* --- @dstr_vputf@ --- *
209 * Arguments: @dstr *d@ = pointer to a dynamic string block
210 * @const char *p@ = pointer to @printf@-style format string
211 * @va_list ap@ = argument handle
213 * Returns: The number of characters written to the string.
215 * Use: As for @dstr_putf@, but may be used as a back-end to user-
216 * supplied functions with @printf@-style interfaces.
219 extern int dstr_vputf(dstr */
*d*/
, const char */
*p*/
, va_list /*ap*/);
221 /* --- @dstr_putf@ --- *
223 * Arguments: @dstr *d@ = pointer to a dynamic string block
224 * @const char *p@ = pointer to @printf@-style format string
225 * @...@ = argument handle
227 * Returns: The number of characters written to the string.
229 * Use: Writes a piece of text to a dynamic string, doing @printf@-
230 * style substitutions as it goes. Intended to be robust if
231 * faced with malicious arguments, but not if the format string
232 * itself is malicious.
235 extern int dstr_putf(dstr */
*d*/
, const char */
*p*/
, ...);
237 /* --- @dstr_putd@ --- *
239 * Arguments: @dstr *d@ = pointer to a dynamic string block
240 * @const dstr *s@ = pointer to a dynamic string to append
244 * Use: Appends a dynamic string to a string. A trailing null
245 * byte is added, as for @dstr_putz@.
248 extern void dstr_putd(dstr */
*d*/
, const dstr */
*s*/
);
250 #define DPUTD(d, s) do { \
251 DENSURE((d), (s)->len + 1); \
252 memcpy((d)->buf + (d)->len, (s)->buf, (s)->len); \
253 (d)->len += (s)->len; \
254 (d)->buf[(d)->len] = 0; \
257 /* --- @dstr_putm@ --- *
259 * Arguments: @dstr *d@ = pointer to a dynamic string block
260 * @const void *p@ = pointer to a block to append
261 * @size_t sz@ = size of the block
263 * Returns: Appends an arbitrary data block to a string. No trailing
267 extern void dstr_putm(dstr */
*d*/
, const void */
*p*/
, size_t /*sz*/);
269 #define DPUTM(d, p, sz) do { \
270 DENSURE((d), (sz)); \
271 memcpy((d)->buf + (d)->len, (p), (sz)); \
275 /* --- @dstr_tidy@ --- *
277 * Arguments: @dstr *d@ = pointer to a dynamic string block
281 * Use: Reduces the amount of memory used by a string. A trailing
282 * null byte is added, as for @dstr_putz@.
285 extern void dstr_tidy(dstr */
*d*/
);
287 /* --- @dstr_putline@ --- *
289 * Arguments: @dstr *d@ = pointer to a dynamic string block
290 * @FILE *fp@ = a stream to read from
292 * Returns: The number of characters read into the buffer, or @EOF@ if
293 * end-of-file was reached before any characters were read.
295 * Use: Appends the next line from the given input stream to the
296 * string. A trailing newline is not added; a trailing null
297 * byte is appended, as for @dstr_putz@.
300 extern int dstr_putline(dstr */
*d*/
, FILE */
*fp*/
);
302 /* --- @dstr_write@ --- *
304 * Arguments: @dstr *d@ = pointer to a dynamic string block
305 * @FILE *fp@ = a stream to write on
307 * Returns: The number of bytes written (as for @fwrite@).
309 * Use: Writes a dynamic string to a file.
312 extern size_t dstr_write(const dstr */
*d*/
, FILE */
*fp*/
);
314 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
316 /*----- That's all, folks -------------------------------------------------*/