3 * $Id: dstr.c,v 1.8 1999/06/01 09:47:52 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.8 1999/06/01 09:47:52 mdw
34 * Fix nasty bugs in `dstr_vputf'.
36 * Revision 1.7 1999/05/21 22:14:30 mdw
37 * Take advantage of the new dynamic string macros.
39 * Revision 1.6 1999/05/21 08:38:33 mdw
40 * Implement some more functions in terms of macros.
42 * Revision 1.5 1999/05/13 22:47:57 mdw
43 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
45 * Revision 1.4 1999/05/06 19:51:35 mdw
46 * Reformatted the LGPL notice a little bit.
48 * Revision 1.3 1999/05/05 18:50:31 mdw
49 * Change licensing conditions to LGPL.
51 * Revision 1.2 1998/12/15 23:53:22 mdw
52 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
53 * formatting in a safe way.
55 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
56 * Initial version of mLib
60 /*----- Header files ------------------------------------------------------*/
73 /*----- Tunable constants -------------------------------------------------*/
75 #define DSTR_INITSZ 256 /* Initial buffer size */
76 #define DSTR_INCSZ 4096 /* Threshhold for doubling */
77 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
79 /*----- Main code ---------------------------------------------------------*/
81 /* --- @dstr_create@ --- *
83 * Arguments: @dstr *d@ = pointer to a dynamic string block
87 * Use: Initializes a dynamic string.
90 void dstr_create(dstr *d) { DCREATE(d); }
92 /* --- @dstr_destroy@ --- *
94 * Arguments: @dstr *d@ = pointer to a dynamic string block
98 * Use: Reclaims the space used by a dynamic string.
101 void dstr_destroy(dstr *d) { DDESTROY(d); }
103 /* --- @dstr_reset@ --- *
105 * Arguments: @dstr *d@ = pointer to a dynaimc string block
109 * Use: Resets a string so that new data gets put at the beginning.
112 void dstr_reset(dstr *d) { DRESET(d); }
114 /* --- @dstr_ensure@ --- *
116 * Arguments: @dstr *d@ = pointer to a dynamic string block
117 * @size_t sz@ = amount of free space to ensure
121 * Use: Ensures that at least @sz@ bytes are available in the
125 void dstr_ensure(dstr *d, size_t sz)
127 size_t rq = d->len + sz;
130 /* --- If we have enough space, just leave it --- */
135 /* --- Grow the buffer --- *
137 * For small buffers, just double the size. For big buffers, make them
138 * a multiple of some suitably large chunk size.
146 else if (d->sz < 0x1000)
149 nsz = (rq + 0x0fff) & ~0x0fff;
153 d->buf = xrealloc(d->buf, nsz);
155 d->buf = xmalloc(nsz);
159 /* --- @dstr_putc@ --- *
161 * Arguments: @dstr *d@ = pointer to a dynamic string block
162 * @char ch@ = character to append
166 * Use: Appends a character to a string.
169 void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
171 /* --- @dstr_putz@ --- *
173 * Arguments: @dstr *d@ = pointer to a dynamic string block
177 * Use: Appends a null byte to a string. The null byte does not
178 * contribute to the string's length, and will be overwritten
179 * by subsequent `put' operations.
182 void dstr_putz(dstr *d) { DPUTZ(d); }
184 /* --- @dstr_puts@ --- *
186 * Arguments: @dstr *d@ = pointer to a dynamic string block
187 * @const char *s@ = pointer to string to append
191 * Use: Appends a character string to a string. A trailing null
192 * byte is added, as for @dstr_putz@.
195 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
197 /* --- @dstr_vputf@ --- *
199 * Arguments: @dstr *d@ = pointer to a dynamic string block
200 * @const char *p@ = pointer to @printf@-style format string
201 * @va_list ap@ = argument handle
203 * Returns: The number of characters written to the string.
205 * Use: As for @dstr_putf@, but may be used as a back-end to user-
206 * supplied functions with @printf@-style interfaces.
209 int dstr_vputf(dstr *d, const char *p, va_list ap)
228 /* --- Most stuff gets passed on through --- */
235 /* --- Dump out what's between @q@ and @p@ --- */
240 /* --- Sort out the various silly flags and things --- */
249 /* --- Various simple flags --- */
268 /* --- Field widths and precision specifiers --- */
283 if (isdigit((unsigned char)*p)) {
293 *ip = va_arg(ap, int);
294 DENSURE(&dd, DSTR_PUTFSTEP);
295 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
300 while (isdigit((unsigned char)*p)) {
302 *ip = 10 * *ip + *p++ - '0';
308 /* --- Output formatting --- */
310 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
313 if ((f & f_prec) && prec + 16 > sz)
315 if ((f & f_wd) && wd + 1> sz)
319 d->len += sprintf(d->buf + d->len, dd.buf,
320 va_arg(ap, unsigned long));
322 d->len += sprintf(d->buf + d->len, dd.buf,
323 va_arg(ap, unsigned int));
326 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
330 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
334 if ((f & f_prec) == 0)
338 if ((f & f_wd) && wd + 1 > sz)
342 d->len += sprintf(d->buf + d->len, dd.buf,
343 va_arg(ap, long double));
345 d->len += sprintf(d->buf + d->len, dd.buf,
352 if ((f & f_wd) && wd + 1> sz)
355 d->len += sprintf(d->buf + d->len, dd.buf,
356 va_arg(ap, unsigned char));
360 const char *s = va_arg(ap, const char *);
366 if ((f & f_wd) && wd > sz)
369 d->len += sprintf(d->buf + d->len, dd.buf, s);
376 if ((f & f_prec) && prec + 16 > sz)
378 if ((f & f_wd) && wd + 1> sz)
381 d->len += sprintf(d->buf + d->len, dd.buf,
382 va_arg(ap, const void *));
387 *va_arg(ap, long *) = (long)(d->len - n);
388 else if (f & f_short)
389 *va_arg(ap, short *) = (short)(d->len - n);
391 *va_arg(ap, int *) = (int)(d->len - n);
394 /* --- Other random stuff --- */
415 /* --- @dstr_putf@ --- *
417 * Arguments: @dstr *d@ = pointer to a dynamic string block
418 * @const char *p@ = pointer to @printf@-style format string
419 * @...@ = argument handle
421 * Returns: The number of characters written to the string.
423 * Use: Writes a piece of text to a dynamic string, doing @printf@-
424 * style substitutions as it goes. Intended to be robust if
425 * faced with malicious arguments, but not if the format string
426 * itself is malicious.
429 int dstr_putf(dstr *d, const char *p, ...)
434 n = dstr_vputf(d, p, ap);
439 /* --- @dstr_putd@ --- *
441 * Arguments: @dstr *d@ = pointer to a dynamic string block
442 * @const dstr *s@ = pointer to a dynamic string to append
446 * Use: Appends a dynamic string to a string. A trailing null
447 * byte is added, as for @dstr_putz@.
450 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
452 /* --- @dstr_putm@ --- *
454 * Arguments: @dstr *d@ = pointer to a dynamic string block
455 * @const void *p@ = pointer to a block to append
456 * @size_t sz@ = size of the block
458 * Returns: Appends an arbitrary data block to a string. No trailing
462 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
464 /* --- @dstr_tidy@ --- *
466 * Arguments: @dstr *d@ = pointer to a dynamic string block
470 * Use: Reduces the amount of memory used by a string. A trailing
471 * null byte is added, as for @dstr_putz@.
474 void dstr_tidy(dstr *d)
477 d->buf = xrealloc(d->buf, d->len + 1);
481 /* --- @dstr_putline@ --- *
483 * Arguments: @dstr *d@ = pointer to a dynamic string block
484 * @FILE *fp@ = a stream to read from
486 * Returns: The number of characters read into the buffer, or @EOF@ if
487 * end-of-file was reached before any characters were read.
489 * Use: Appends the next line from the given input stream to the
490 * string. A trailing newline is not added; a trailing null
491 * byte is appended, as for @dstr_putz@.
494 int dstr_putline(dstr *d, FILE *fp)
496 size_t left = d->sz - d->len;
503 /* --- Make sure there's some buffer space --- */
510 /* --- Read the next byte --- */
514 /* --- End-of-file when no characters read is special --- */
516 if (ch == EOF && !rd)
519 /* --- End-of-file or newline ends the loop --- */
521 if (ch == EOF || ch == '\n') {
527 /* --- Append the character and continue --- */
534 /* --- @dstr_write@ --- *
536 * Arguments: @dstr *d@ = pointer to a dynamic string block
537 * @FILE *fp@ = a stream to write on
539 * Returns: The number of bytes written (as for @fwrite@).
541 * Use: Writes a dynamic string to a file.
544 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
546 /*----- That's all, folks -------------------------------------------------*/