3 * $Id: dstr.h,v 1.3 1999/05/05 18:50:31 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 Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.2 1998/12/15 23:53:23 mdw
36 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
37 * formatting in a safe way.
39 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
40 * Initial version of mLib
51 /*----- Rationale ---------------------------------------------------------*
53 * This file declares what is hopefully a fairly useful collection of
54 * primitive string handling functions. The idea is that the strings
55 * allocate memory for themselves as required. The @dstr@ routines don't
56 * assume any sort of terminator character, so arbitrary binary data can
57 * be stored in a dynamic string. With luck, this should put a stop to
58 * any buffer overflow problems.
61 /*----- Header files ------------------------------------------------------*/
66 /*----- Data structures ---------------------------------------------------*/
69 char *buf; /* Pointer to string buffer */
70 size_t sz; /* Size of the buffer */
71 size_t len; /* Length of the string */
74 /*----- Functions provided ------------------------------------------------*/
76 /* --- @dstr_create@ --- *
78 * Arguments: @dstr *d@ = pointer to a dynamic string block
82 * Use: Initialises a dynamic string.
85 extern void dstr_create(dstr */*d*/);
87 /* --- @dstr_destroy@ --- *
89 * Arguments: @dstr *d@ = pointer to a dynamic string block
93 * Use: Reclaims the space used by a dynamic string.
96 extern void dstr_destroy(dstr */*d*/);
98 /* --- @dstr_reset@ --- *
100 * Arguments: @dstr *d@ = pointer to a dynaimc string block
104 * Use: Resets a string so that new data gets put at the beginning.
107 extern void dstr_reset(dstr */*d*/);
109 /* --- @dstr_ensure@ --- *
111 * Arguments: @dstr *d@ = pointer to a dynamic string block
112 * @size_t sz@ = amount of free space to ensure
116 * Use: Ensures that at least @sz@ bytes are available in the
120 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
122 #define DENSURE(d, rq) do { \
123 if ((d)->len + (rq) > (d)->sz) dstr_ensure((d), (rq)); \
126 /* --- @dstr_putc@ --- *
128 * Arguments: @dstr *d@ = pointer to a dynamic string block
129 * @char ch@ = character to append
133 * Use: Appends a character to a string.
136 extern void dstr_putc(dstr */*d*/, char /*ch*/);
138 #define DPUTC(d, ch) do { \
140 (d)->buf[(d)->len++] = (ch); \
143 /* --- @dstr_putz@ --- *
145 * Arguments: @dstr *d@ = pointer to a dynamic string block
149 * Use: Appends a null byte to a string. The null byte does not
150 * contribute to the string's length, and will be overwritten
151 * by subsequent `put' operations.
154 extern void dstr_putz(dstr */*d*/);
156 #define DPUTZ(d) do { \
158 (d)->buf[(d)->len] = 0; \
161 /* --- @dstr_puts@ --- *
163 * Arguments: @dstr *d@ = pointer to a dynamic string block
164 * @const char *s@ = pointer to string to append
168 * Use: Appends a character string to a string. A trailing null
169 * byte is added, as for @dstr_putz@.
172 extern void dstr_puts(dstr */*d*/, const char */*s*/);
174 #define DPUTS(d, s) do { \
175 size_t sz = strlen(s); \
176 DENSURE((d), sz + 1); \
177 memcpy((d)->buf + (d)->len, (s), sz + 1); \
181 /* --- @dstr_vputf@ --- *
183 * Arguments: @dstr *d@ = pointer to a dynamic string block
184 * @const char *p@ = pointer to @printf@-style format string
185 * @va_list ap@ = argument handle
189 * Use: As for @dstr_putf@, but may be used as a back-end to user-
190 * supplied functions with @printf@-style interfaces.
193 extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list /*ap*/);
195 /* --- @dstr_putf@ --- *
197 * Arguments: @dstr *d@ = pointer to a dynamic string block
198 * @const char *p@ = pointer to @printf@-style format string
199 * @...@ = argument handle
203 * Use: Writes a piece of text to a dynamic string, doing @printf@-
204 * style substitutions as it goes. Intended to be robust if
205 * faced with malicious arguments, but not if the format string
206 * itself is malicious.
209 extern int dstr_putf(dstr */*d*/, const char */*p*/, ...);
211 /* --- @dstr_putd@ --- *
213 * Arguments: @dstr *d@ = pointer to a dynamic string block
214 * @const dstr *s@ = pointer to a dynamic string to append
218 * Use: Appends a dynamic string to a string. A trailing null
219 * byte is added, as for @dstr_putz@.
222 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
224 #define DPUTD(d, s) do { \
225 DENSURE((d), (s)->len + 1); \
226 memcpy((d)->buf + (d)->len, (s)->buf, (s)->len); \
227 (d)->len += (s)->len; \
228 (d)->buf[(d)->len] = 0; \
231 /* --- @dstr_putm@ --- *
233 * Arguments: @dstr *d@ = pointer to a dynamic string block
234 * @const void *p@ = pointer to a block to append
235 * @size_t sz@ = size of the block
237 * Returns: Appends an arbitrary data block to a string. No trailing
241 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
243 #define DPUTM(d, p, sz) do { \
244 DENSURE((d), (sz)); \
245 memcpy((d)->buf + (d)->len, (p), (sz)); \
249 /* --- @dstr_tidy@ --- *
251 * Arguments: @dstr *d@ = pointer to a dynamic string block
255 * Use: Reduces the amount of memory used by a string. A trailing
256 * null byte is added, as for @dstr_putz@.
259 extern void dstr_tidy(dstr */*d*/);
261 /* --- @dstr_putline@ --- *
263 * Arguments: @dstr *d@ = pointer to a dynamic string block
264 * @FILE *fp@ = a stream to read from
266 * Returns: The number of characters read into the buffer, or @EOF@ if
267 * end-of-file was reached before any characters were read.
269 * Use: Appends the next line from the given input stream to the
270 * string. A trailing newline is not added; a trailing null
271 * byte is appended, as for @dstr_putz@.
274 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
276 /* --- @dstr_write@ --- *
278 * Arguments: @dstr *d@ = pointer to a dynamic string block
279 * @FILE *fp@ = a stream to write on
281 * Returns: The number of bytes written (as for @fwrite@).
283 * Use: Writes a dynamic string to a file.
286 extern size_t dstr_write(dstr */*d*/, FILE */*fp*/);
288 /*----- That's all, folks -------------------------------------------------*/