3 * $Id: dstr.c,v 1.16 2004/04/08 01:36:11 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 /*----- Header files ------------------------------------------------------*/
39 /*----- Tunable constants -------------------------------------------------*/
42 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
43 * Otherwise, it's set to the next power of two that's large enough. This is
44 * memory-hungry, but efficient.
47 #define DSTR_INITSZ 64 /* Initial buffer size */
49 /*----- Main code ---------------------------------------------------------*/
51 /* --- @dstr_create@ --- *
53 * Arguments: @dstr *d@ = pointer to a dynamic string block
57 * Use: Initializes a dynamic string.
60 void dstr_create(dstr *d) { DCREATE(d); }
62 /* --- @dstr_destroy@ --- *
64 * Arguments: @dstr *d@ = pointer to a dynamic string block
68 * Use: Reclaims the space used by a dynamic string.
71 void dstr_destroy(dstr *d) { DDESTROY(d); }
73 /* --- @dstr_reset@ --- *
75 * Arguments: @dstr *d@ = pointer to a dynamic string block
79 * Use: Resets a string so that new data gets put at the beginning.
82 void dstr_reset(dstr *d) { DRESET(d); }
84 /* --- @dstr_ensure@ --- *
86 * Arguments: @dstr *d@ = pointer to a dynamic string block
87 * @size_t sz@ = amount of free space to ensure
91 * Use: Ensures that at least @sz@ bytes are available in the
95 void dstr_ensure(dstr *d, size_t sz)
97 size_t rq = d->len + sz;
100 /* --- If we have enough space, just leave it --- */
105 /* --- Grow the buffer --- */
110 nsz = (DSTR_INITSZ >> 1);
111 do nsz <<= 1; while (nsz < rq);
114 d->buf = x_realloc(d->a, d->buf, nsz, d->sz);
116 d->buf = x_alloc(d->a, nsz);
120 /* --- @dstr_putc@ --- *
122 * Arguments: @dstr *d@ = pointer to a dynamic string block
123 * @char ch@ = character to append
127 * Use: Appends a character to a string.
130 void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
132 /* --- @dstr_putz@ --- *
134 * Arguments: @dstr *d@ = pointer to a dynamic string block
138 * Use: Appends a null byte to a string. The null byte does not
139 * contribute to the string's length, and will be overwritten
140 * by subsequent `put' operations.
143 void dstr_putz(dstr *d) { DPUTZ(d); }
145 /* --- @dstr_puts@ --- *
147 * Arguments: @dstr *d@ = pointer to a dynamic string block
148 * @const char *s@ = pointer to string to append
152 * Use: Appends a character string to a string. A trailing null
153 * byte is added, as for @dstr_putz@.
156 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
158 /* --- @dstr_putd@ --- *
160 * Arguments: @dstr *d@ = pointer to a dynamic string block
161 * @const dstr *s@ = pointer to a dynamic string to append
165 * Use: Appends a dynamic string to a string. A trailing null
166 * byte is added, as for @dstr_putz@.
169 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
171 /* --- @dstr_putm@ --- *
173 * Arguments: @dstr *d@ = pointer to a dynamic string block
174 * @const void *p@ = pointer to a block to append
175 * @size_t sz@ = size of the block
177 * Returns: Appends an arbitrary data block to a string. No trailing
181 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
183 /* --- @dstr_tidy@ --- *
185 * Arguments: @dstr *d@ = pointer to a dynamic string block
189 * Use: Reduces the amount of memory used by a string. A trailing
190 * null byte is added, as for @dstr_putz@.
193 void dstr_tidy(dstr *d)
195 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
200 /* --- @dstr_putline@ --- *
202 * Arguments: @dstr *d@ = pointer to a dynamic string block
203 * @FILE *fp@ = a stream to read from
205 * Returns: The number of characters read into the buffer, or @EOF@ if
206 * end-of-file was reached before any characters were read.
208 * Use: Appends the next line from the given input stream to the
209 * string. A trailing newline is not added; a trailing null
210 * byte is appended, as for @dstr_putz@.
213 int dstr_putline(dstr *d, FILE *fp)
215 size_t left = d->sz - d->len;
222 /* --- Read the next byte --- */
226 /* --- End-of-file when no characters read is special --- */
228 if (ch == EOF && !rd)
231 /* --- Make sure there's some buffer space --- */
239 /* --- End-of-file or newline ends the loop --- */
241 if (ch == EOF || ch == '\n') {
247 /* --- Append the character and continue --- */
254 /* --- @dstr_write@ --- *
256 * Arguments: @dstr *d@ = pointer to a dynamic string block
257 * @FILE *fp@ = a stream to write on
259 * Returns: The number of bytes written (as for @fwrite@).
261 * Use: Writes a dynamic string to a file.
264 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
266 /*----- That's all, folks -------------------------------------------------*/