3 * $Id: dstr.c,v 1.3 1999/05/05 18:50:31 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 Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.2 1998/12/15 23:53:22 mdw
36 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
37 * formatting in a safe way.
39 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
40 * Initial version of mLib
44 /*----- Header files ------------------------------------------------------*/
57 /*----- Tunable constants -------------------------------------------------*/
59 #define DSTR_INITSZ 256 /* Initial buffer size */
60 #define DSTR_INCSZ 4096 /* Threshhold for doubling */
61 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
63 /*----- Main code ---------------------------------------------------------*/
65 /* --- @dstr_create@ --- *
67 * Arguments: @dstr *d@ = pointer to a dynamic string block
71 * Use: Initialises a dynamic string.
74 void dstr_create(dstr *d)
81 /* --- @dstr_destroy@ --- *
83 * Arguments: @dstr *d@ = pointer to a dynamic string block
87 * Use: Reclaims the space used by a dynamic string.
90 void dstr_destroy(dstr *d)
99 /* --- @dstr_reset@ --- *
101 * Arguments: @dstr *d@ = pointer to a dynaimc string block
105 * Use: Resets a string so that new data gets put at the beginning.
108 void dstr_reset(dstr *d)
113 /* --- @dstr_ensure@ --- *
115 * Arguments: @dstr *d@ = pointer to a dynamic string block
116 * @size_t sz@ = amount of free space to ensure
120 * Use: Ensures that at least @sz@ bytes are available in the
124 void dstr_ensure(dstr *d, size_t sz)
126 size_t rq = d->len + sz;
129 /* --- If we have enough space, just leave it --- */
134 /* --- Grow the buffer --- *
136 * For small buffers, just double the size. For big buffers, make them
137 * a multiple of some suitably large chunk size.
145 else if (d->sz < 0x1000)
148 nsz = (rq + 0x0fff) & ~0x0fff;
152 d->buf = xrealloc(d->buf, nsz);
154 d->buf = xmalloc(nsz);
158 /* --- @dstr_putc@ --- *
160 * Arguments: @dstr *d@ = pointer to a dynamic string block
161 * @char ch@ = character to append
165 * Use: Appends a character to a string.
168 void dstr_putc(dstr *d, char ch)
173 /* --- @dstr_putz@ --- *
175 * Arguments: @dstr *d@ = pointer to a dynamic string block
179 * Use: Appends a null byte to a string. The null byte does not
180 * contribute to the string's length, and will be overwritten
181 * by subsequent `put' operations.
184 void dstr_putz(dstr *d)
189 /* --- @dstr_puts@ --- *
191 * Arguments: @dstr *d@ = pointer to a dynamic string block
192 * @const char *s@ = pointer to string to append
196 * Use: Appends a character string to a string. A trailing null
197 * byte is added, as for @dstr_putz@.
200 void dstr_puts(dstr *d, const char *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
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 --- */
258 /* --- Various simple flags --- */
277 /* --- Field widths and precision specifiers --- */
292 if (isdigit((unsigned char)*p)) {
302 *ip = va_arg(ap, int);
303 DENSURE(&dd, DSTR_PUTFSTEP);
304 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
309 while (isdigit((unsigned char)*p)) {
311 *ip = 10 * *ip + *p++ + '0';
317 /* --- Output formatting --- */
319 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
322 if ((f & f_prec) && prec + 16 > sz)
324 if ((f & f_wd) && wd + 1> sz)
328 d->len += sprintf(d->buf + d->len, dd.buf,
329 va_arg(ap, unsigned long));
331 d->len += sprintf(d->buf + d->len, dd.buf,
332 va_arg(ap, unsigned int));
335 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
339 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
343 if ((f & f_prec) == 0)
347 if ((f & f_wd) && wd + 1 > sz)
351 d->len += sprintf(d->buf + d->len, dd.buf,
352 va_arg(ap, long double));
354 d->len += sprintf(d->buf + d->len, dd.buf,
361 if ((f & f_wd) && wd + 1> sz)
364 d->len += sprintf(d->buf + d->len, dd.buf,
365 va_arg(ap, unsigned char));
369 const char *s = va_arg(ap, const char *);
375 if ((f & f_wd) && wd > sz)
378 d->len += sprintf(d->buf + d->len, dd.buf, s);
385 if ((f & f_prec) && prec + 16 > sz)
387 if ((f & f_wd) && wd + 1> sz)
390 d->len += sprintf(d->buf + d->len, dd.buf,
391 va_arg(ap, const void *));
396 *va_arg(ap, long *) = (long)(d->len - n);
397 else if (f & f_short)
398 *va_arg(ap, short *) = (short)(d->len - n);
400 *va_arg(ap, int *) = (int)(d->len - n);
403 /* --- 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
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)
463 /* --- @dstr_putm@ --- *
465 * Arguments: @dstr *d@ = pointer to a dynamic string block
466 * @const void *p@ = pointer to a block to append
467 * @size_t sz@ = size of the block
469 * Returns: Appends an arbitrary data block to a string. No trailing
473 void dstr_putm(dstr *d, const void *p, size_t sz)
478 /* --- @dstr_tidy@ --- *
480 * Arguments: @dstr *d@ = pointer to a dynamic string block
484 * Use: Reduces the amount of memory used by a string. A trailing
485 * null byte is added, as for @dstr_putz@.
488 void dstr_tidy(dstr *d)
491 d->buf = xrealloc(d->buf, d->len + 1);
495 /* --- @dstr_putline@ --- *
497 * Arguments: @dstr *d@ = pointer to a dynamic string block
498 * @FILE *fp@ = a stream to read from
500 * Returns: The number of characters read into the buffer, or @EOF@ if
501 * end-of-file was reached before any characters were read.
503 * Use: Appends the next line from the given input stream to the
504 * string. A trailing newline is not added; a trailing null
505 * byte is appended, as for @dstr_putz@.
508 int dstr_putline(dstr *d, FILE *fp)
510 size_t left = d->sz - d->len;
517 /* --- Make sure there's some buffer space --- */
524 /* --- Read the next byte --- */
528 /* --- End-of-file when no characters read is special --- */
530 if (ch == EOF && !rd)
533 /* --- End-of-file or newline ends the loop --- */
535 if (ch == EOF || ch == '\n') {
541 /* --- Append the character and continue --- */
548 /* --- @dstr_write@ --- *
550 * Arguments: @dstr *d@ = pointer to a dynamic string block
551 * @FILE *fp@ = a stream to write on
553 * Returns: The number of bytes written (as for @fwrite@).
555 * Use: Writes a dynamic string to a file.
558 size_t dstr_write(dstr *d, FILE *fp)
560 return (fwrite(d->buf, 1, d->len, fp));
563 /*----- That's all, folks -------------------------------------------------*/