3 * Handle dynamically growing strings
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 /*----- Tunable constants -------------------------------------------------*/
40 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
41 * Otherwise, it's set to the next power of two that's large enough. This is
42 * memory-hungry, but efficient.
45 #define DSTR_INITSZ 64 /* Initial buffer size */
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @dstr_create@ --- *
51 * Arguments: @dstr *d@ = pointer to a dynamic string block
55 * Use: Initializes a dynamic string.
58 void dstr_create(dstr *d) { DCREATE(d); }
60 /* --- @dstr_destroy@ --- *
62 * Arguments: @dstr *d@ = pointer to a dynamic string block
66 * Use: Reclaims the space used by a dynamic string.
69 void dstr_destroy(dstr *d) { DDESTROY(d); }
71 /* --- @dstr_reset@ --- *
73 * Arguments: @dstr *d@ = pointer to a dynamic string block
77 * Use: Resets a string so that new data gets put at the beginning.
80 void dstr_reset(dstr *d) { DRESET(d); }
82 /* --- @dstr_ensure@ --- *
84 * Arguments: @dstr *d@ = pointer to a dynamic string block
85 * @size_t sz@ = amount of free space to ensure
89 * Use: Ensures that at least @sz@ bytes are available in the
93 void dstr_ensure(dstr *d, size_t sz)
95 size_t rq = d->len + sz;
98 /* --- If we have enough space, just leave it --- */
103 /* --- Grow the buffer --- */
108 nsz = (DSTR_INITSZ >> 1);
109 do nsz <<= 1; while (nsz < rq);
112 d->buf = x_realloc(d->a, d->buf, nsz, d->sz);
114 d->buf = x_alloc(d->a, nsz);
118 /* --- @dstr_putc@ --- *
120 * Arguments: @dstr *d@ = pointer to a dynamic string block
121 * @int ch@ = character to append
125 * Use: Appends a character to a string.
128 void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); }
130 /* --- @dstr_putz@ --- *
132 * Arguments: @dstr *d@ = pointer to a dynamic string block
136 * Use: Appends a null byte to a string. The null byte does not
137 * contribute to the string's length, and will be overwritten
138 * by subsequent `put' operations.
141 void dstr_putz(dstr *d) { DPUTZ(d); }
143 /* --- @dstr_puts@ --- *
145 * Arguments: @dstr *d@ = pointer to a dynamic string block
146 * @const char *s@ = pointer to string to append
150 * Use: Appends a character string to a string. A trailing null
151 * byte is added, as for @dstr_putz@.
154 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
156 /* --- @dstr_putd@ --- *
158 * Arguments: @dstr *d@ = pointer to a dynamic string block
159 * @const dstr *s@ = pointer to a dynamic string to append
163 * Use: Appends a dynamic string to a string. A trailing null
164 * byte is added, as for @dstr_putz@.
167 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
169 /* --- @dstr_putm@ --- *
171 * Arguments: @dstr *d@ = pointer to a dynamic string block
172 * @const void *p@ = pointer to a block to append
173 * @size_t sz@ = size of the block
175 * Returns: Appends an arbitrary data block to a string. No trailing
179 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
181 /* --- @dstr_tidy@ --- *
183 * Arguments: @dstr *d@ = pointer to a dynamic string block
187 * Use: Reduces the amount of memory used by a string. A trailing
188 * null byte is added, as for @dstr_putz@.
191 void dstr_tidy(dstr *d)
193 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
198 /* --- @dstr_putline@ --- *
200 * Arguments: @dstr *d@ = pointer to a dynamic string block
201 * @FILE *fp@ = a stream to read from
203 * Returns: The number of characters read into the buffer, or @EOF@ if
204 * end-of-file was reached before any characters were read.
206 * Use: Appends the next line from the given input stream to the
207 * string. A trailing newline is not added; a trailing null
208 * byte is appended, as for @dstr_putz@.
211 int dstr_putline(dstr *d, FILE *fp)
213 size_t left = d->sz - d->len;
220 /* --- Read the next byte --- */
224 /* --- End-of-file when no characters read is special --- */
226 if (ch == EOF && !rd)
229 /* --- Make sure there's some buffer space --- */
237 /* --- End-of-file or newline ends the loop --- */
239 if (ch == EOF || ch == '\n') {
245 /* --- Append the character and continue --- */
252 /* --- @dstr_write@ --- *
254 * Arguments: @dstr *d@ = pointer to a dynamic string block
255 * @FILE *fp@ = a stream to write on
257 * Returns: The number of bytes written (as for @fwrite@).
259 * Use: Writes a dynamic string to a file.
262 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
264 /*----- That's all, folks -------------------------------------------------*/