3 * $Id: dstr.c,v 1.7 1999/05/21 22:14:30 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.7 1999/05/21 22:14:30 mdw
34 * Take advantage of the new dynamic string macros.
36 * Revision 1.6 1999/05/21 08:38:33 mdw
37 * Implement some more functions in terms of macros.
39 * Revision 1.5 1999/05/13 22:47:57 mdw
40 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
42 * Revision 1.4 1999/05/06 19:51:35 mdw
43 * Reformatted the LGPL notice a little bit.
45 * Revision 1.3 1999/05/05 18:50:31 mdw
46 * Change licensing conditions to LGPL.
48 * Revision 1.2 1998/12/15 23:53:22 mdw
49 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
50 * formatting in a safe way.
52 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
53 * Initial version of mLib
57 /*----- Header files ------------------------------------------------------*/
70 /*----- Tunable constants -------------------------------------------------*/
72 #define DSTR_INITSZ 256 /* Initial buffer size */
73 #define DSTR_INCSZ 4096 /* Threshhold for doubling */
74 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
76 /*----- Main code ---------------------------------------------------------*/
78 /* --- @dstr_create@ --- *
80 * Arguments: @dstr *d@ = pointer to a dynamic string block
84 * Use: Initializes a dynamic string.
87 void dstr_create(dstr *d) { DCREATE(d); }
89 /* --- @dstr_destroy@ --- *
91 * Arguments: @dstr *d@ = pointer to a dynamic string block
95 * Use: Reclaims the space used by a dynamic string.
98 void dstr_destroy(dstr *d) { DDESTROY(d); }
100 /* --- @dstr_reset@ --- *
102 * Arguments: @dstr *d@ = pointer to a dynaimc string block
106 * Use: Resets a string so that new data gets put at the beginning.
109 void dstr_reset(dstr *d) { DRESET(d); }
111 /* --- @dstr_ensure@ --- *
113 * Arguments: @dstr *d@ = pointer to a dynamic string block
114 * @size_t sz@ = amount of free space to ensure
118 * Use: Ensures that at least @sz@ bytes are available in the
122 void dstr_ensure(dstr *d, size_t sz)
124 size_t rq = d->len + sz;
127 /* --- If we have enough space, just leave it --- */
132 /* --- Grow the buffer --- *
134 * For small buffers, just double the size. For big buffers, make them
135 * a multiple of some suitably large chunk size.
143 else if (d->sz < 0x1000)
146 nsz = (rq + 0x0fff) & ~0x0fff;
150 d->buf = xrealloc(d->buf, nsz);
152 d->buf = xmalloc(nsz);
156 /* --- @dstr_putc@ --- *
158 * Arguments: @dstr *d@ = pointer to a dynamic string block
159 * @char ch@ = character to append
163 * Use: Appends a character to a string.
166 void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
168 /* --- @dstr_putz@ --- *
170 * Arguments: @dstr *d@ = pointer to a dynamic string block
174 * Use: Appends a null byte to a string. The null byte does not
175 * contribute to the string's length, and will be overwritten
176 * by subsequent `put' operations.
179 void dstr_putz(dstr *d) { DPUTZ(d); }
181 /* --- @dstr_puts@ --- *
183 * Arguments: @dstr *d@ = pointer to a dynamic string block
184 * @const char *s@ = pointer to string to append
188 * Use: Appends a character string to a string. A trailing null
189 * byte is added, as for @dstr_putz@.
192 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
194 /* --- @dstr_vputf@ --- *
196 * Arguments: @dstr *d@ = pointer to a dynamic string block
197 * @const char *p@ = pointer to @printf@-style format string
198 * @va_list ap@ = argument handle
200 * Returns: The number of characters written to the string.
202 * Use: As for @dstr_putf@, but may be used as a back-end to user-
203 * supplied functions with @printf@-style interfaces.
206 int dstr_vputf(dstr *d, const char *p, va_list ap)
225 /* --- Most stuff gets passed on through --- */
232 /* --- Dump out what's between @q@ and @p@ --- */
237 /* --- Sort out the various silly flags and things --- */
246 /* --- Various simple flags --- */
265 /* --- Field widths and precision specifiers --- */
280 if (isdigit((unsigned char)*p)) {
290 *ip = va_arg(ap, int);
291 DENSURE(&dd, DSTR_PUTFSTEP);
292 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
297 while (isdigit((unsigned char)*p)) {
299 *ip = 10 * *ip + *p++ + '0';
305 /* --- Output formatting --- */
307 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
310 if ((f & f_prec) && prec + 16 > sz)
312 if ((f & f_wd) && wd + 1> sz)
316 d->len += sprintf(d->buf + d->len, dd.buf,
317 va_arg(ap, unsigned long));
319 d->len += sprintf(d->buf + d->len, dd.buf,
320 va_arg(ap, unsigned int));
323 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
327 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
331 if ((f & f_prec) == 0)
335 if ((f & f_wd) && wd + 1 > sz)
339 d->len += sprintf(d->buf + d->len, dd.buf,
340 va_arg(ap, long double));
342 d->len += sprintf(d->buf + d->len, dd.buf,
349 if ((f & f_wd) && wd + 1> sz)
352 d->len += sprintf(d->buf + d->len, dd.buf,
353 va_arg(ap, unsigned char));
357 const char *s = va_arg(ap, const char *);
363 if ((f & f_wd) && wd > sz)
366 d->len += sprintf(d->buf + d->len, dd.buf, s);
373 if ((f & f_prec) && prec + 16 > sz)
375 if ((f & f_wd) && wd + 1> sz)
378 d->len += sprintf(d->buf + d->len, dd.buf,
379 va_arg(ap, const void *));
384 *va_arg(ap, long *) = (long)(d->len - n);
385 else if (f & f_short)
386 *va_arg(ap, short *) = (short)(d->len - n);
388 *va_arg(ap, int *) = (int)(d->len - n);
391 /* --- Other random stuff --- */
412 /* --- @dstr_putf@ --- *
414 * Arguments: @dstr *d@ = pointer to a dynamic string block
415 * @const char *p@ = pointer to @printf@-style format string
416 * @...@ = argument handle
418 * Returns: The number of characters written to the string.
420 * Use: Writes a piece of text to a dynamic string, doing @printf@-
421 * style substitutions as it goes. Intended to be robust if
422 * faced with malicious arguments, but not if the format string
423 * itself is malicious.
426 int dstr_putf(dstr *d, const char *p, ...)
431 n = dstr_vputf(d, p, ap);
436 /* --- @dstr_putd@ --- *
438 * Arguments: @dstr *d@ = pointer to a dynamic string block
439 * @const dstr *s@ = pointer to a dynamic string to append
443 * Use: Appends a dynamic string to a string. A trailing null
444 * byte is added, as for @dstr_putz@.
447 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
449 /* --- @dstr_putm@ --- *
451 * Arguments: @dstr *d@ = pointer to a dynamic string block
452 * @const void *p@ = pointer to a block to append
453 * @size_t sz@ = size of the block
455 * Returns: Appends an arbitrary data block to a string. No trailing
459 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
461 /* --- @dstr_tidy@ --- *
463 * Arguments: @dstr *d@ = pointer to a dynamic string block
467 * Use: Reduces the amount of memory used by a string. A trailing
468 * null byte is added, as for @dstr_putz@.
471 void dstr_tidy(dstr *d)
474 d->buf = xrealloc(d->buf, d->len + 1);
478 /* --- @dstr_putline@ --- *
480 * Arguments: @dstr *d@ = pointer to a dynamic string block
481 * @FILE *fp@ = a stream to read from
483 * Returns: The number of characters read into the buffer, or @EOF@ if
484 * end-of-file was reached before any characters were read.
486 * Use: Appends the next line from the given input stream to the
487 * string. A trailing newline is not added; a trailing null
488 * byte is appended, as for @dstr_putz@.
491 int dstr_putline(dstr *d, FILE *fp)
493 size_t left = d->sz - d->len;
500 /* --- Make sure there's some buffer space --- */
507 /* --- Read the next byte --- */
511 /* --- End-of-file when no characters read is special --- */
513 if (ch == EOF && !rd)
516 /* --- End-of-file or newline ends the loop --- */
518 if (ch == EOF || ch == '\n') {
524 /* --- Append the character and continue --- */
531 /* --- @dstr_write@ --- *
533 * Arguments: @dstr *d@ = pointer to a dynamic string block
534 * @FILE *fp@ = a stream to write on
536 * Returns: The number of bytes written (as for @fwrite@).
538 * Use: Writes a dynamic string to a file.
541 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
543 /*----- That's all, folks -------------------------------------------------*/