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)
95 { GROWBUF_EXTEND(d->a, d->buf, d->sz, d->len + sz, DSTR_INITSZ, 1); }
97 /* --- @dstr_putc@ --- *
99 * Arguments: @dstr *d@ = pointer to a dynamic string block
100 * @int ch@ = character to append
104 * Use: Appends a character to a string.
107 void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); }
109 /* --- @dstr_putz@ --- *
111 * Arguments: @dstr *d@ = pointer to a dynamic string block
115 * Use: Appends a null byte to a string. The null byte does not
116 * contribute to the string's length, and will be overwritten
117 * by subsequent `put' operations.
120 void dstr_putz(dstr *d) { DPUTZ(d); }
122 /* --- @dstr_puts@ --- *
124 * Arguments: @dstr *d@ = pointer to a dynamic string block
125 * @const char *s@ = pointer to string to append
129 * Use: Appends a character string to a string. A trailing null
130 * byte is added, as for @dstr_putz@.
133 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
135 /* --- @dstr_putd@ --- *
137 * Arguments: @dstr *d@ = pointer to a dynamic string block
138 * @const dstr *s@ = pointer to a dynamic string to append
142 * Use: Appends a dynamic string to a string. A trailing null
143 * byte is added, as for @dstr_putz@.
146 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
148 /* --- @dstr_putm@ --- *
150 * Arguments: @dstr *d@ = pointer to a dynamic string block
151 * @const void *p@ = pointer to a block to append
152 * @size_t sz@ = size of the block
154 * Returns: Appends an arbitrary data block to a string. No trailing
158 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
160 /* --- @dstr_tidy@ --- *
162 * Arguments: @dstr *d@ = pointer to a dynamic string block
166 * Use: Reduces the amount of memory used by a string. A trailing
167 * null byte is added, as for @dstr_putz@.
170 void dstr_tidy(dstr *d)
172 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
177 /* --- @dstr_putline@ --- *
179 * Arguments: @dstr *d@ = pointer to a dynamic string block
180 * @FILE *fp@ = a stream to read from
182 * Returns: The number of characters read into the buffer, or @EOF@ if
183 * end-of-file was reached before any characters were read.
185 * Use: Appends the next line from the given input stream to the
186 * string. A trailing newline is not added; a trailing null
187 * byte is appended, as for @dstr_putz@.
190 int dstr_putline(dstr *d, FILE *fp)
192 size_t left = d->sz - d->len;
199 /* --- Read the next byte --- */
203 /* --- End-of-file when no characters read is special --- */
205 if (ch == EOF && !rd)
208 /* --- Make sure there's some buffer space --- */
216 /* --- End-of-file or newline ends the loop --- */
218 if (ch == EOF || ch == '\n') {
224 /* --- Append the character and continue --- */
231 /* --- @dstr_write@ --- *
233 * Arguments: @dstr *d@ = pointer to a dynamic string block
234 * @FILE *fp@ = a stream to write on
236 * Returns: The number of bytes written (as for @fwrite@).
238 * Use: Writes a dynamic string to a file.
241 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
243 /*----- That's all, folks -------------------------------------------------*/