3 * $Id: dstr.c,v 1.10 1999/10/04 21:44:47 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.10 1999/10/04 21:44:47 mdw
34 * Move `dstr_putf' and `dstr_vputf' into a separate source file.
36 * Revision 1.9 1999/07/06 19:16:06 mdw
37 * Simplify buffer-growing algorithm. Just double it each time.
39 * Revision 1.8 1999/06/01 09:47:52 mdw
40 * Fix nasty bugs in `dstr_vputf'.
42 * Revision 1.7 1999/05/21 22:14:30 mdw
43 * Take advantage of the new dynamic string macros.
45 * Revision 1.6 1999/05/21 08:38:33 mdw
46 * Implement some more functions in terms of macros.
48 * Revision 1.5 1999/05/13 22:47:57 mdw
49 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
51 * Revision 1.4 1999/05/06 19:51:35 mdw
52 * Reformatted the LGPL notice a little bit.
54 * Revision 1.3 1999/05/05 18:50:31 mdw
55 * Change licensing conditions to LGPL.
57 * Revision 1.2 1998/12/15 23:53:22 mdw
58 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
59 * formatting in a safe way.
61 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
62 * Initial version of mLib
66 /*----- Header files ------------------------------------------------------*/
75 /*----- Tunable constants -------------------------------------------------*/
78 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
79 * Otherwise, it's set to the next power of two that's large enough. This is
80 * memory-hungry, but efficient.
83 #define DSTR_INITSZ 256 /* Initial buffer size */
85 /*----- Main code ---------------------------------------------------------*/
87 /* --- @dstr_create@ --- *
89 * Arguments: @dstr *d@ = pointer to a dynamic string block
93 * Use: Initializes a dynamic string.
96 void dstr_create(dstr *d) { DCREATE(d); }
98 /* --- @dstr_destroy@ --- *
100 * Arguments: @dstr *d@ = pointer to a dynamic string block
104 * Use: Reclaims the space used by a dynamic string.
107 void dstr_destroy(dstr *d) { DDESTROY(d); }
109 /* --- @dstr_reset@ --- *
111 * Arguments: @dstr *d@ = pointer to a dynaimc string block
115 * Use: Resets a string so that new data gets put at the beginning.
118 void dstr_reset(dstr *d) { DRESET(d); }
120 /* --- @dstr_ensure@ --- *
122 * Arguments: @dstr *d@ = pointer to a dynamic string block
123 * @size_t sz@ = amount of free space to ensure
127 * Use: Ensures that at least @sz@ bytes are available in the
131 void dstr_ensure(dstr *d, size_t sz)
133 size_t rq = d->len + sz;
136 /* --- If we have enough space, just leave it --- */
141 /* --- Grow the buffer --- */
145 if (nsz == 0 && rq < DSTR_INITSZ)
148 do nsz <<= 1; while (nsz < rq);
151 d->buf = xrealloc(d->buf, nsz);
153 d->buf = xmalloc(nsz);
157 /* --- @dstr_putc@ --- *
159 * Arguments: @dstr *d@ = pointer to a dynamic string block
160 * @char ch@ = character to append
164 * Use: Appends a character to a string.
167 void dstr_putc(dstr *d, char ch) { DPUTC(d, 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 void dstr_putz(dstr *d) { DPUTZ(d); }
182 /* --- @dstr_puts@ --- *
184 * Arguments: @dstr *d@ = pointer to a dynamic string block
185 * @const char *s@ = pointer to string to append
189 * Use: Appends a character string to a string. A trailing null
190 * byte is added, as for @dstr_putz@.
193 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
195 /* --- @dstr_putd@ --- *
197 * Arguments: @dstr *d@ = pointer to a dynamic string block
198 * @const dstr *s@ = pointer to a dynamic string to append
202 * Use: Appends a dynamic string to a string. A trailing null
203 * byte is added, as for @dstr_putz@.
206 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
208 /* --- @dstr_putm@ --- *
210 * Arguments: @dstr *d@ = pointer to a dynamic string block
211 * @const void *p@ = pointer to a block to append
212 * @size_t sz@ = size of the block
214 * Returns: Appends an arbitrary data block to a string. No trailing
218 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
220 /* --- @dstr_tidy@ --- *
222 * Arguments: @dstr *d@ = pointer to a dynamic string block
226 * Use: Reduces the amount of memory used by a string. A trailing
227 * null byte is added, as for @dstr_putz@.
230 void dstr_tidy(dstr *d)
233 d->buf = xrealloc(d->buf, d->len + 1);
237 /* --- @dstr_putline@ --- *
239 * Arguments: @dstr *d@ = pointer to a dynamic string block
240 * @FILE *fp@ = a stream to read from
242 * Returns: The number of characters read into the buffer, or @EOF@ if
243 * end-of-file was reached before any characters were read.
245 * Use: Appends the next line from the given input stream to the
246 * string. A trailing newline is not added; a trailing null
247 * byte is appended, as for @dstr_putz@.
250 int dstr_putline(dstr *d, FILE *fp)
252 size_t left = d->sz - d->len;
259 /* --- Make sure there's some buffer space --- */
266 /* --- Read the next byte --- */
270 /* --- End-of-file when no characters read is special --- */
272 if (ch == EOF && !rd)
275 /* --- End-of-file or newline ends the loop --- */
277 if (ch == EOF || ch == '\n') {
283 /* --- Append the character and continue --- */
290 /* --- @dstr_write@ --- *
292 * Arguments: @dstr *d@ = pointer to a dynamic string block
293 * @FILE *fp@ = a stream to write on
295 * Returns: The number of bytes written (as for @fwrite@).
297 * Use: Writes a dynamic string to a file.
300 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
302 /*----- That's all, folks -------------------------------------------------*/