3 * $Id: dstr.c,v 1.1 1998/06/17 23:44:42 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 General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1998/06/17 23:44:42 mdw
37 /*----- Header files ------------------------------------------------------*/
46 /*----- Tunable constants -------------------------------------------------*/
48 #define DSTR_INITSZ 256 /* Initial buffer size */
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @dstr_create@ --- *
54 * Arguments: @dstr *d@ = pointer to a dynamic string block
58 * Use: Initialises a dynamic string.
61 void dstr_create(dstr *d)
68 /* --- @dstr_destroy@ --- *
70 * Arguments: @dstr *d@ = pointer to a dynamic string block
74 * Use: Reclaims the space used by a dynamic string.
77 void dstr_destroy(dstr *d)
86 /* --- @dstr_reset@ --- *
88 * Arguments: @dstr *d@ = pointer to a dynaimc string block
92 * Use: Resets a string so that new data gets put at the beginning.
95 void dstr_reset(dstr *d)
100 /* --- @dstr_ensure@ --- *
102 * Arguments: @dstr *d@ = pointer to a dynamic string block
103 * @size_t sz@ = amount of free space to ensure
107 * Use: Ensures that at least @sz@ bytes are available in the
111 void dstr_ensure(dstr *d, size_t sz)
113 size_t rq = d->len + sz;
116 /* --- If we have enough space, just leave it --- */
121 /* --- Grow the buffer --- *
123 * For small buffers, just double the size. For big buffers, make them
124 * a multiple of some suitably large chunk size.
132 else if (d->sz < 0x1000)
135 nsz = (rq + 0x0fff) & ~0x0fff;
139 d->buf = xrealloc(d->buf, nsz);
141 d->buf = xmalloc(nsz);
145 /* --- @dstr_putc@ --- *
147 * Arguments: @dstr *d@ = pointer to a dynamic string block
148 * @char ch@ = character to append
152 * Use: Appends a character to a string.
155 void dstr_putc(dstr *d, char ch)
160 /* --- @dstr_putz@ --- *
162 * Arguments: @dstr *d@ = pointer to a dynamic string block
166 * Use: Appends a null byte to a string. The null byte does not
167 * contribute to the string's length, and will be overwritten
168 * by subsequent `put' operations.
171 void dstr_putz(dstr *d)
176 /* --- @dstr_puts@ --- *
178 * Arguments: @dstr *d@ = pointer to a dynamic string block
179 * @const char *s@ = pointer to string to append
183 * Use: Appends a character string to a string. A trailing null
184 * byte is added, as for @dstr_putz@.
187 void dstr_puts(dstr *d, const char *s)
192 /* --- @dstr_putd@ --- *
194 * Arguments: @dstr *d@ = pointer to a dynamic string block
195 * @const dstr *s@ = pointer to a dynamic string to append
199 * Use: Appends a dynamic string to a string. A trailing null
200 * byte is added, as for @dstr_putz@.
203 void dstr_putd(dstr *d, const dstr *s)
208 /* --- @dstr_putm@ --- *
210 * Arguments: @dstr *d@ = pointer to a dynamic string block
211 * @const void *p@ = pointer to a block to append
212 * @size_t sz@ = size of the block
214 * Returns: Appends an arbitrary data block to a string. No trailing
218 void dstr_putm(dstr *d, const void *p, size_t sz)
223 /* --- @dstr_tidy@ --- *
225 * Arguments: @dstr *d@ = pointer to a dynamic string block
229 * Use: Reduces the amount of memory used by a string. A trailing
230 * null byte is added, as for @dstr_putz@.
233 void dstr_tidy(dstr *d)
236 d->buf = xrealloc(d->buf, d->len + 1);
240 /* --- @dstr_putline@ --- *
242 * Arguments: @dstr *d@ = pointer to a dynamic string block
243 * @FILE *fp@ = a stream to read from
245 * Returns: The number of characters read into the buffer, or @EOF@ if
246 * end-of-file was reached before any characters were read.
248 * Use: Appends the next line from the given input stream to the
249 * string. A trailing newline is not added; a trailing null
250 * byte is appended, as for @dstr_putz@.
253 int dstr_putline(dstr *d, FILE *fp)
255 size_t left = d->sz - d->len;
262 /* --- Make sure there's some buffer space --- */
269 /* --- Read the next byte --- */
273 /* --- End-of-file when no characters read is special --- */
275 if (ch == EOF && !rd)
278 /* --- End-of-file or newline ends the loop --- */
280 if (ch == EOF || ch == '\n') {
286 /* --- Append the character and continue --- */
293 /* --- @dstr_write@ --- *
295 * Arguments: @dstr *d@ = pointer to a dynamic string block
296 * @FILE *fp@ = a stream to write on
298 * Returns: The number of bytes written (as for @fwrite@).
300 * Use: Writes a dynamic string to a file.
303 size_t dstr_write(dstr *d, FILE *fp)
305 return (fwrite(d->buf, 1, d->len, fp));
308 /*----- That's all, folks -------------------------------------------------*/