3 * $Id: dstr.h,v 1.1 1998/06/17 23:44:42 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 General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1998/06/17 23:44:42 mdw
44 /*----- Rationale ---------------------------------------------------------*
46 * This file declares what is hopefully a fairly useful collection of
47 * primitive string handling functions. The idea is that the strings
48 * allocate memory for themselves as required. The @dstr@ routines don't
49 * assume any sort of terminator character, so arbitrary binary data can
50 * be stored in a dynamic string. With luck, this should put a stop to
51 * any buffer overflow problems.
54 /*----- Header files ------------------------------------------------------*/
58 /*----- Data structures ---------------------------------------------------*/
61 char *buf; /* Pointer to string buffer */
62 size_t sz; /* Size of the buffer */
63 size_t len; /* Length of the string */
66 /*----- Functions provided ------------------------------------------------*/
68 /* --- @dstr_create@ --- *
70 * Arguments: @dstr *d@ = pointer to a dynamic string block
74 * Use: Initialises a dynamic string.
77 extern void dstr_create(dstr */*d*/);
79 /* --- @dstr_destroy@ --- *
81 * Arguments: @dstr *d@ = pointer to a dynamic string block
85 * Use: Reclaims the space used by a dynamic string.
88 extern void dstr_destroy(dstr */*d*/);
90 /* --- @dstr_reset@ --- *
92 * Arguments: @dstr *d@ = pointer to a dynaimc string block
96 * Use: Resets a string so that new data gets put at the beginning.
99 extern void dstr_reset(dstr */*d*/);
101 /* --- @dstr_ensure@ --- *
103 * Arguments: @dstr *d@ = pointer to a dynamic string block
104 * @size_t sz@ = amount of free space to ensure
108 * Use: Ensures that at least @sz@ bytes are available in the
112 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
114 #define DENSURE(d, rq) do { \
115 if ((d)->len + (rq) > (d)->sz) dstr_ensure((d), (rq)); \
118 /* --- @dstr_putc@ --- *
120 * Arguments: @dstr *d@ = pointer to a dynamic string block
121 * @char ch@ = character to append
125 * Use: Appends a character to a string.
128 extern void dstr_putc(dstr */*d*/, char /*ch*/);
130 #define DPUTC(d, ch) do { \
132 (d)->buf[(d)->len++] = (ch); \
135 /* --- @dstr_putz@ --- *
137 * Arguments: @dstr *d@ = pointer to a dynamic string block
141 * Use: Appends a null byte to a string. The null byte does not
142 * contribute to the string's length, and will be overwritten
143 * by subsequent `put' operations.
146 extern void dstr_putz(dstr */*d*/);
148 #define DPUTZ(d) do { \
150 (d)->buf[(d)->len] = 0; \
153 /* --- @dstr_puts@ --- *
155 * Arguments: @dstr *d@ = pointer to a dynamic string block
156 * @const char *s@ = pointer to string to append
160 * Use: Appends a character string to a string. A trailing null
161 * byte is added, as for @dstr_putz@.
164 extern void dstr_puts(dstr */*d*/, const char */*s*/);
166 #define DPUTS(d, s) do { \
167 size_t sz = strlen(s); \
168 DENSURE((d), sz + 1); \
169 memcpy((d)->buf + (d)->len, (s), sz + 1); \
173 /* --- @dstr_putd@ --- *
175 * Arguments: @dstr *d@ = pointer to a dynamic string block
176 * @const dstr *s@ = pointer to a dynamic string to append
180 * Use: Appends a dynamic string to a string. A trailing null
181 * byte is added, as for @dstr_putz@.
184 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
186 #define DPUTD(d, s) do { \
187 DENSURE((d), (s)->len + 1); \
188 memcpy((d)->buf + (d)->len, (s)->buf, (s)->len); \
189 (d)->len += (s)->len; \
190 (d)->buf[(d)->len] = 0; \
193 /* --- @dstr_putm@ --- *
195 * Arguments: @dstr *d@ = pointer to a dynamic string block
196 * @const void *p@ = pointer to a block to append
197 * @size_t sz@ = size of the block
199 * Returns: Appends an arbitrary data block to a string. No trailing
203 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
205 #define DPUTM(d, p, sz) do { \
206 DENSURE((d), (sz)); \
207 memcpy((d)->buf + (d)->len, (p), (sz)); \
211 /* --- @dstr_tidy@ --- *
213 * Arguments: @dstr *d@ = pointer to a dynamic string block
217 * Use: Reduces the amount of memory used by a string. A trailing
218 * null byte is added, as for @dstr_putz@.
221 extern void dstr_tidy(dstr */*d*/);
223 /* --- @dstr_putline@ --- *
225 * Arguments: @dstr *d@ = pointer to a dynamic string block
226 * @FILE *fp@ = a stream to read from
228 * Returns: The number of characters read into the buffer, or @EOF@ if
229 * end-of-file was reached before any characters were read.
231 * Use: Appends the next line from the given input stream to the
232 * string. A trailing newline is not added; a trailing null
233 * byte is appended, as for @dstr_putz@.
236 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
238 /* --- @dstr_write@ --- *
240 * Arguments: @dstr *d@ = pointer to a dynamic string block
241 * @FILE *fp@ = a stream to write on
243 * Returns: The number of bytes written (as for @fwrite@).
245 * Use: Writes a dynamic string to a file.
248 extern size_t dstr_write(dstr */*d*/, FILE */*fp*/);
250 /*----- That's all, folks -------------------------------------------------*/