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 ------------------------------------------------------*/
38 /*----- Tunable constants -------------------------------------------------*/
41 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
42 * Otherwise, it's set to the next power of two that's large enough. This is
43 * memory-hungry, but efficient.
46 #define DSTR_INITSZ 64 /* Initial buffer size */
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @dstr_create@ --- *
52 * Arguments: @dstr *d@ = pointer to a dynamic string block
56 * Use: Initializes a dynamic string.
59 void dstr_create(dstr *d) { DCREATE(d); }
61 /* --- @dstr_destroy@ --- *
63 * Arguments: @dstr *d@ = pointer to a dynamic string block
67 * Use: Reclaims the space used by a dynamic string.
70 void dstr_destroy(dstr *d) { DDESTROY(d); }
72 /* --- @dstr_reset@ --- *
74 * Arguments: @dstr *d@ = pointer to a dynamic string block
78 * Use: Resets a string so that new data gets put at the beginning.
81 void dstr_reset(dstr *d) { DRESET(d); }
83 /* --- @dstr_ensure@ --- *
85 * Arguments: @dstr *d@ = pointer to a dynamic string block
86 * @size_t sz@ = amount of free space to ensure
90 * Use: Ensures that at least @sz@ bytes are available in the
94 void dstr_ensure(dstr *d, size_t sz)
96 GROWBUF_EXTEND(size_t, d->a, d->buf, d->sz, d->len + sz,
100 /* --- @dstr_putc@ --- *
102 * Arguments: @dstr *d@ = pointer to a dynamic string block
103 * @int ch@ = character to append
107 * Use: Appends a character to a string.
110 void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); }
112 /* --- @dstr_putz@ --- *
114 * Arguments: @dstr *d@ = pointer to a dynamic string block
118 * Use: Appends a null byte to a string. The null byte does not
119 * contribute to the string's length, and will be overwritten
120 * by subsequent `put' operations.
123 void dstr_putz(dstr *d) { DPUTZ(d); }
125 /* --- @dstr_puts@ --- *
127 * Arguments: @dstr *d@ = pointer to a dynamic string block
128 * @const char *s@ = pointer to string to append
132 * Use: Appends a character string to a string. A trailing null
133 * byte is added, as for @dstr_putz@.
136 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
138 /* --- @dstr_putd@ --- *
140 * Arguments: @dstr *d@ = pointer to a dynamic string block
141 * @const dstr *s@ = pointer to a dynamic string to append
145 * Use: Appends a dynamic string to a string. A trailing null
146 * byte is added, as for @dstr_putz@.
149 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
151 /* --- @dstr_putm@ --- *
153 * Arguments: @dstr *d@ = pointer to a dynamic string block
154 * @const void *p@ = pointer to a block to append
155 * @size_t sz@ = size of the block
157 * Returns: Appends an arbitrary data block to a string. No trailing
161 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
163 /* --- @dstr_tidy@ --- *
165 * Arguments: @dstr *d@ = pointer to a dynamic string block
169 * Use: Reduces the amount of memory used by a string. A trailing
170 * null byte is added, as for @dstr_putz@.
173 void dstr_tidy(dstr *d)
175 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
180 /* --- @dstr_putline@ --- *
182 * Arguments: @dstr *d@ = pointer to a dynamic string block
183 * @FILE *fp@ = a stream to read from
185 * Returns: The number of characters read into the buffer, or @EOF@ if
186 * end-of-file was reached before any characters were read.
188 * Use: Appends the next line from the given input stream to the
189 * string. A trailing newline is not added; a trailing null
190 * byte is appended, as for @dstr_putz@.
193 int dstr_putline(dstr *d, FILE *fp)
195 size_t left = d->sz - d->len;
202 /* --- Read the next byte --- */
206 /* --- End-of-file when no characters read is special --- */
208 if (ch == EOF && !rd)
211 /* --- Make sure there's some buffer space --- */
219 /* --- End-of-file or newline ends the loop --- */
221 if (ch == EOF || ch == '\n') {
227 /* --- Append the character and continue --- */
234 /* --- @dstr_write@ --- *
236 * Arguments: @dstr *d@ = pointer to a dynamic string block
237 * @FILE *fp@ = a stream to write on
239 * Returns: The number of bytes written (as for @fwrite@).
241 * Use: Writes a dynamic string to a file.
244 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
246 /*----- That's all, folks -------------------------------------------------*/