3 * $Id: dstr.c,v 1.5 1999/05/13 22:47:57 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.5 1999/05/13 22:47:57 mdw
34 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
36 * Revision 1.4 1999/05/06 19:51:35 mdw
37 * Reformatted the LGPL notice a little bit.
39 * Revision 1.3 1999/05/05 18:50:31 mdw
40 * Change licensing conditions to LGPL.
42 * Revision 1.2 1998/12/15 23:53:22 mdw
43 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
44 * formatting in a safe way.
46 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
47 * Initial version of mLib
51 /*----- Header files ------------------------------------------------------*/
64 /*----- Tunable constants -------------------------------------------------*/
66 #define DSTR_INITSZ 256 /* Initial buffer size */
67 #define DSTR_INCSZ 4096 /* Threshhold for doubling */
68 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
70 /*----- Main code ---------------------------------------------------------*/
72 /* --- @dstr_create@ --- *
74 * Arguments: @dstr *d@ = pointer to a dynamic string block
78 * Use: Initializes a dynamic string.
81 void dstr_create(dstr *d)
88 /* --- @dstr_destroy@ --- *
90 * Arguments: @dstr *d@ = pointer to a dynamic string block
94 * Use: Reclaims the space used by a dynamic string.
97 void dstr_destroy(dstr *d)
106 /* --- @dstr_reset@ --- *
108 * Arguments: @dstr *d@ = pointer to a dynaimc string block
112 * Use: Resets a string so that new data gets put at the beginning.
115 void dstr_reset(dstr *d)
120 /* --- @dstr_ensure@ --- *
122 * Arguments: @dstr *d@ = pointer to a dynamic string block
123 * @size_t sz@ = amount of free space to ensure
127 * Use: Ensures that at least @sz@ bytes are available in the
131 void dstr_ensure(dstr *d, size_t sz)
133 size_t rq = d->len + sz;
136 /* --- If we have enough space, just leave it --- */
141 /* --- Grow the buffer --- *
143 * For small buffers, just double the size. For big buffers, make them
144 * a multiple of some suitably large chunk size.
152 else if (d->sz < 0x1000)
155 nsz = (rq + 0x0fff) & ~0x0fff;
159 d->buf = xrealloc(d->buf, nsz);
161 d->buf = xmalloc(nsz);
165 /* --- @dstr_putc@ --- *
167 * Arguments: @dstr *d@ = pointer to a dynamic string block
168 * @char ch@ = character to append
172 * Use: Appends a character to a string.
175 void dstr_putc(dstr *d, char ch)
180 /* --- @dstr_putz@ --- *
182 * Arguments: @dstr *d@ = pointer to a dynamic string block
186 * Use: Appends a null byte to a string. The null byte does not
187 * contribute to the string's length, and will be overwritten
188 * by subsequent `put' operations.
191 void dstr_putz(dstr *d)
196 /* --- @dstr_puts@ --- *
198 * Arguments: @dstr *d@ = pointer to a dynamic string block
199 * @const char *s@ = pointer to string to append
203 * Use: Appends a character string to a string. A trailing null
204 * byte is added, as for @dstr_putz@.
207 void dstr_puts(dstr *d, const char *s)
212 /* --- @dstr_vputf@ --- *
214 * Arguments: @dstr *d@ = pointer to a dynamic string block
215 * @const char *p@ = pointer to @printf@-style format string
216 * @va_list ap@ = argument handle
218 * Returns: The number of characters written to the string.
220 * Use: As for @dstr_putf@, but may be used as a back-end to user-
221 * supplied functions with @printf@-style interfaces.
224 int dstr_vputf(dstr *d, const char *p, va_list ap)
243 /* --- Most stuff gets passed on through --- */
250 /* --- Dump out what's between @q@ and @p@ --- */
255 /* --- Sort out the various silly flags and things --- */
265 /* --- Various simple flags --- */
284 /* --- Field widths and precision specifiers --- */
299 if (isdigit((unsigned char)*p)) {
309 *ip = va_arg(ap, int);
310 DENSURE(&dd, DSTR_PUTFSTEP);
311 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
316 while (isdigit((unsigned char)*p)) {
318 *ip = 10 * *ip + *p++ + '0';
324 /* --- Output formatting --- */
326 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
329 if ((f & f_prec) && prec + 16 > sz)
331 if ((f & f_wd) && wd + 1> sz)
335 d->len += sprintf(d->buf + d->len, dd.buf,
336 va_arg(ap, unsigned long));
338 d->len += sprintf(d->buf + d->len, dd.buf,
339 va_arg(ap, unsigned int));
342 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
346 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
350 if ((f & f_prec) == 0)
354 if ((f & f_wd) && wd + 1 > sz)
358 d->len += sprintf(d->buf + d->len, dd.buf,
359 va_arg(ap, long double));
361 d->len += sprintf(d->buf + d->len, dd.buf,
368 if ((f & f_wd) && wd + 1> sz)
371 d->len += sprintf(d->buf + d->len, dd.buf,
372 va_arg(ap, unsigned char));
376 const char *s = va_arg(ap, const char *);
382 if ((f & f_wd) && wd > sz)
385 d->len += sprintf(d->buf + d->len, dd.buf, s);
392 if ((f & f_prec) && prec + 16 > sz)
394 if ((f & f_wd) && wd + 1> sz)
397 d->len += sprintf(d->buf + d->len, dd.buf,
398 va_arg(ap, const void *));
403 *va_arg(ap, long *) = (long)(d->len - n);
404 else if (f & f_short)
405 *va_arg(ap, short *) = (short)(d->len - n);
407 *va_arg(ap, int *) = (int)(d->len - n);
410 /* --- Other random stuff --- */
430 /* --- @dstr_putf@ --- *
432 * Arguments: @dstr *d@ = pointer to a dynamic string block
433 * @const char *p@ = pointer to @printf@-style format string
434 * @...@ = argument handle
436 * Returns: The number of characters written to the string.
438 * Use: Writes a piece of text to a dynamic string, doing @printf@-
439 * style substitutions as it goes. Intended to be robust if
440 * faced with malicious arguments, but not if the format string
441 * itself is malicious.
444 int dstr_putf(dstr *d, const char *p, ...)
449 n = dstr_vputf(d, p, ap);
454 /* --- @dstr_putd@ --- *
456 * Arguments: @dstr *d@ = pointer to a dynamic string block
457 * @const dstr *s@ = pointer to a dynamic string to append
461 * Use: Appends a dynamic string to a string. A trailing null
462 * byte is added, as for @dstr_putz@.
465 void dstr_putd(dstr *d, const dstr *s)
470 /* --- @dstr_putm@ --- *
472 * Arguments: @dstr *d@ = pointer to a dynamic string block
473 * @const void *p@ = pointer to a block to append
474 * @size_t sz@ = size of the block
476 * Returns: Appends an arbitrary data block to a string. No trailing
480 void dstr_putm(dstr *d, const void *p, size_t sz)
485 /* --- @dstr_tidy@ --- *
487 * Arguments: @dstr *d@ = pointer to a dynamic string block
491 * Use: Reduces the amount of memory used by a string. A trailing
492 * null byte is added, as for @dstr_putz@.
495 void dstr_tidy(dstr *d)
498 d->buf = xrealloc(d->buf, d->len + 1);
502 /* --- @dstr_putline@ --- *
504 * Arguments: @dstr *d@ = pointer to a dynamic string block
505 * @FILE *fp@ = a stream to read from
507 * Returns: The number of characters read into the buffer, or @EOF@ if
508 * end-of-file was reached before any characters were read.
510 * Use: Appends the next line from the given input stream to the
511 * string. A trailing newline is not added; a trailing null
512 * byte is appended, as for @dstr_putz@.
515 int dstr_putline(dstr *d, FILE *fp)
517 size_t left = d->sz - d->len;
524 /* --- Make sure there's some buffer space --- */
531 /* --- Read the next byte --- */
535 /* --- End-of-file when no characters read is special --- */
537 if (ch == EOF && !rd)
540 /* --- End-of-file or newline ends the loop --- */
542 if (ch == EOF || ch == '\n') {
548 /* --- Append the character and continue --- */
555 /* --- @dstr_write@ --- *
557 * Arguments: @dstr *d@ = pointer to a dynamic string block
558 * @FILE *fp@ = a stream to write on
560 * Returns: The number of bytes written (as for @fwrite@).
562 * Use: Writes a dynamic string to a file.
565 size_t dstr_write(const dstr *d, FILE *fp)
567 return (fwrite(d->buf, 1, d->len, fp));
570 /*----- That's all, folks -------------------------------------------------*/