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 GROWBUF_EXTEND(size_t, d->a, d->buf, d->sz, d->len + sz,
99 /* --- @dstr_putc@ --- *
101 * Arguments: @dstr *d@ = pointer to a dynamic string block
102 * @int ch@ = character to append
106 * Use: Appends a character to a string.
109 void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); }
111 /* --- @dstr_putz@ --- *
113 * Arguments: @dstr *d@ = pointer to a dynamic string block
117 * Use: Appends a null byte to a string. The null byte does not
118 * contribute to the string's length, and will be overwritten
119 * by subsequent `put' operations.
122 void dstr_putz(dstr *d) { DPUTZ(d); }
124 /* --- @dstr_puts@ --- *
126 * Arguments: @dstr *d@ = pointer to a dynamic string block
127 * @const char *s@ = pointer to string to append
131 * Use: Appends a character string to a string. A trailing null
132 * byte is added, as for @dstr_putz@.
135 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
137 /* --- @dstr_putd@ --- *
139 * Arguments: @dstr *d@ = pointer to a dynamic string block
140 * @const dstr *s@ = pointer to a dynamic string to append
144 * Use: Appends a dynamic string to a string. A trailing null
145 * byte is added, as for @dstr_putz@.
148 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
150 /* --- @dstr_putm@ --- *
152 * Arguments: @dstr *d@ = pointer to a dynamic string block
153 * @const void *p@ = pointer to a block to append
154 * @size_t sz@ = size of the block
156 * Returns: Appends an arbitrary data block to a string. No trailing
160 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
162 /* --- @dstr_tidy@ --- *
164 * Arguments: @dstr *d@ = pointer to a dynamic string block
168 * Use: Reduces the amount of memory used by a string. A trailing
169 * null byte is added, as for @dstr_putz@.
172 void dstr_tidy(dstr *d)
174 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
179 /* --- @dstr_putline@ --- *
181 * Arguments: @dstr *d@ = pointer to a dynamic string block
182 * @FILE *fp@ = a stream to read from
184 * Returns: The number of characters read into the buffer, or @EOF@ if
185 * end-of-file was reached before any characters were read.
187 * Use: Appends the next line from the given input stream to the
188 * string. A trailing newline is not added; a trailing null
189 * byte is appended, as for @dstr_putz@.
192 int dstr_putline(dstr *d, FILE *fp)
194 size_t left = d->sz - d->len;
201 /* --- Read the next byte --- */
205 /* --- End-of-file when no characters read is special --- */
207 if (ch == EOF && !rd)
210 /* --- Make sure there's some buffer space --- */
218 /* --- End-of-file or newline ends the loop --- */
220 if (ch == EOF || ch == '\n') {
226 /* --- Append the character and continue --- */
233 /* --- @dstr_write@ --- *
235 * Arguments: @dstr *d@ = pointer to a dynamic string block
236 * @FILE *fp@ = a stream to write on
238 * Returns: The number of bytes written (as for @fwrite@).
240 * Use: Writes a dynamic string to a file.
243 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
245 /*----- That's all, folks -------------------------------------------------*/