3 * $Id: dstr.c,v 1.9 1999/07/06 19:16:06 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.9 1999/07/06 19:16:06 mdw
34 * Simplify buffer-growing algorithm. Just double it each time.
36 * Revision 1.8 1999/06/01 09:47:52 mdw
37 * Fix nasty bugs in `dstr_vputf'.
39 * Revision 1.7 1999/05/21 22:14:30 mdw
40 * Take advantage of the new dynamic string macros.
42 * Revision 1.6 1999/05/21 08:38:33 mdw
43 * Implement some more functions in terms of macros.
45 * Revision 1.5 1999/05/13 22:47:57 mdw
46 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
48 * Revision 1.4 1999/05/06 19:51:35 mdw
49 * Reformatted the LGPL notice a little bit.
51 * Revision 1.3 1999/05/05 18:50:31 mdw
52 * Change licensing conditions to LGPL.
54 * Revision 1.2 1998/12/15 23:53:22 mdw
55 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
56 * formatting in a safe way.
58 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
59 * Initial version of mLib
63 /*----- Header files ------------------------------------------------------*/
76 /*----- Tunable constants -------------------------------------------------*/
78 /* --- Buffer expansion parameters --- *
80 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
81 * Otherwise, it's set to the next power of two that's large enough. This is
82 * memory-hungry, but efficient.
85 #define DSTR_INITSZ 256 /* Initial buffer size */
87 /* --- Parameters for @dstr_putf@ --- *
89 * For each format specifier, at least @DSTR_PUTFSTEP@ bytes are ensured
90 * before writing the formatted result.
93 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
95 /*----- Main code ---------------------------------------------------------*/
97 /* --- @dstr_create@ --- *
99 * Arguments: @dstr *d@ = pointer to a dynamic string block
103 * Use: Initializes a dynamic string.
106 void dstr_create(dstr *d) { DCREATE(d); }
108 /* --- @dstr_destroy@ --- *
110 * Arguments: @dstr *d@ = pointer to a dynamic string block
114 * Use: Reclaims the space used by a dynamic string.
117 void dstr_destroy(dstr *d) { DDESTROY(d); }
119 /* --- @dstr_reset@ --- *
121 * Arguments: @dstr *d@ = pointer to a dynaimc string block
125 * Use: Resets a string so that new data gets put at the beginning.
128 void dstr_reset(dstr *d) { DRESET(d); }
130 /* --- @dstr_ensure@ --- *
132 * Arguments: @dstr *d@ = pointer to a dynamic string block
133 * @size_t sz@ = amount of free space to ensure
137 * Use: Ensures that at least @sz@ bytes are available in the
141 void dstr_ensure(dstr *d, size_t sz)
143 size_t rq = d->len + sz;
146 /* --- If we have enough space, just leave it --- */
151 /* --- Grow the buffer --- */
155 if (nsz == 0 && rq < DSTR_INITSZ)
158 do nsz <<= 1; while (nsz < rq);
161 d->buf = xrealloc(d->buf, nsz);
163 d->buf = xmalloc(nsz);
167 /* --- @dstr_putc@ --- *
169 * Arguments: @dstr *d@ = pointer to a dynamic string block
170 * @char ch@ = character to append
174 * Use: Appends a character to a string.
177 void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
179 /* --- @dstr_putz@ --- *
181 * Arguments: @dstr *d@ = pointer to a dynamic string block
185 * Use: Appends a null byte to a string. The null byte does not
186 * contribute to the string's length, and will be overwritten
187 * by subsequent `put' operations.
190 void dstr_putz(dstr *d) { DPUTZ(d); }
192 /* --- @dstr_puts@ --- *
194 * Arguments: @dstr *d@ = pointer to a dynamic string block
195 * @const char *s@ = pointer to string to append
199 * Use: Appends a character string to a string. A trailing null
200 * byte is added, as for @dstr_putz@.
203 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
205 /* --- @dstr_vputf@ --- *
207 * Arguments: @dstr *d@ = pointer to a dynamic string block
208 * @const char *p@ = pointer to @printf@-style format string
209 * @va_list ap@ = argument handle
211 * Returns: The number of characters written to the string.
213 * Use: As for @dstr_putf@, but may be used as a back-end to user-
214 * supplied functions with @printf@-style interfaces.
217 int dstr_vputf(dstr *d, const char *p, va_list ap)
236 /* --- Most stuff gets passed on through --- */
243 /* --- Dump out what's between @q@ and @p@ --- */
248 /* --- Sort out the various silly flags and things --- */
257 /* --- Various simple flags --- */
276 /* --- Field widths and precision specifiers --- */
291 if (isdigit((unsigned char)*p)) {
301 *ip = va_arg(ap, int);
302 DENSURE(&dd, DSTR_PUTFSTEP);
303 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
308 while (isdigit((unsigned char)*p)) {
310 *ip = 10 * *ip + *p++ - '0';
316 /* --- Output formatting --- */
318 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
321 if ((f & f_prec) && prec + 16 > sz)
323 if ((f & f_wd) && wd + 1> sz)
327 d->len += sprintf(d->buf + d->len, dd.buf,
328 va_arg(ap, unsigned long));
330 d->len += sprintf(d->buf + d->len, dd.buf,
331 va_arg(ap, unsigned int));
334 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
338 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
342 if ((f & f_prec) == 0)
346 if ((f & f_wd) && wd + 1 > sz)
350 d->len += sprintf(d->buf + d->len, dd.buf,
351 va_arg(ap, long double));
353 d->len += sprintf(d->buf + d->len, dd.buf,
360 if ((f & f_wd) && wd + 1> sz)
363 d->len += sprintf(d->buf + d->len, dd.buf,
364 va_arg(ap, unsigned char));
368 const char *s = va_arg(ap, const char *);
374 if ((f & f_wd) && wd > sz)
377 d->len += sprintf(d->buf + d->len, dd.buf, s);
384 if ((f & f_prec) && prec + 16 > sz)
386 if ((f & f_wd) && wd + 1> sz)
389 d->len += sprintf(d->buf + d->len, dd.buf,
390 va_arg(ap, const void *));
395 *va_arg(ap, long *) = (long)(d->len - n);
396 else if (f & f_short)
397 *va_arg(ap, short *) = (short)(d->len - n);
399 *va_arg(ap, int *) = (int)(d->len - n);
402 /* --- Other random stuff --- */
423 /* --- @dstr_putf@ --- *
425 * Arguments: @dstr *d@ = pointer to a dynamic string block
426 * @const char *p@ = pointer to @printf@-style format string
427 * @...@ = argument handle
429 * Returns: The number of characters written to the string.
431 * Use: Writes a piece of text to a dynamic string, doing @printf@-
432 * style substitutions as it goes. Intended to be robust if
433 * faced with malicious arguments, but not if the format string
434 * itself is malicious.
437 int dstr_putf(dstr *d, const char *p, ...)
442 n = dstr_vputf(d, p, ap);
447 /* --- @dstr_putd@ --- *
449 * Arguments: @dstr *d@ = pointer to a dynamic string block
450 * @const dstr *s@ = pointer to a dynamic string to append
454 * Use: Appends a dynamic string to a string. A trailing null
455 * byte is added, as for @dstr_putz@.
458 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
460 /* --- @dstr_putm@ --- *
462 * Arguments: @dstr *d@ = pointer to a dynamic string block
463 * @const void *p@ = pointer to a block to append
464 * @size_t sz@ = size of the block
466 * Returns: Appends an arbitrary data block to a string. No trailing
470 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
472 /* --- @dstr_tidy@ --- *
474 * Arguments: @dstr *d@ = pointer to a dynamic string block
478 * Use: Reduces the amount of memory used by a string. A trailing
479 * null byte is added, as for @dstr_putz@.
482 void dstr_tidy(dstr *d)
485 d->buf = xrealloc(d->buf, d->len + 1);
489 /* --- @dstr_putline@ --- *
491 * Arguments: @dstr *d@ = pointer to a dynamic string block
492 * @FILE *fp@ = a stream to read from
494 * Returns: The number of characters read into the buffer, or @EOF@ if
495 * end-of-file was reached before any characters were read.
497 * Use: Appends the next line from the given input stream to the
498 * string. A trailing newline is not added; a trailing null
499 * byte is appended, as for @dstr_putz@.
502 int dstr_putline(dstr *d, FILE *fp)
504 size_t left = d->sz - d->len;
511 /* --- Make sure there's some buffer space --- */
518 /* --- Read the next byte --- */
522 /* --- End-of-file when no characters read is special --- */
524 if (ch == EOF && !rd)
527 /* --- End-of-file or newline ends the loop --- */
529 if (ch == EOF || ch == '\n') {
535 /* --- Append the character and continue --- */
542 /* --- @dstr_write@ --- *
544 * Arguments: @dstr *d@ = pointer to a dynamic string block
545 * @FILE *fp@ = a stream to write on
547 * Returns: The number of bytes written (as for @fwrite@).
549 * Use: Writes a dynamic string to a file.
552 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
554 /*----- That's all, folks -------------------------------------------------*/