3 * $Id: dstr.c,v 1.4 1999/05/06 19:51:35 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.4 1999/05/06 19:51:35 mdw
34 * Reformatted the LGPL notice a little bit.
36 * Revision 1.3 1999/05/05 18:50:31 mdw
37 * Change licensing conditions to LGPL.
39 * Revision 1.2 1998/12/15 23:53:22 mdw
40 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
41 * formatting in a safe way.
43 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
44 * Initial version of mLib
48 /*----- Header files ------------------------------------------------------*/
61 /*----- Tunable constants -------------------------------------------------*/
63 #define DSTR_INITSZ 256 /* Initial buffer size */
64 #define DSTR_INCSZ 4096 /* Threshhold for doubling */
65 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
67 /*----- Main code ---------------------------------------------------------*/
69 /* --- @dstr_create@ --- *
71 * Arguments: @dstr *d@ = pointer to a dynamic string block
75 * Use: Initialises a dynamic string.
78 void dstr_create(dstr *d)
85 /* --- @dstr_destroy@ --- *
87 * Arguments: @dstr *d@ = pointer to a dynamic string block
91 * Use: Reclaims the space used by a dynamic string.
94 void dstr_destroy(dstr *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)
117 /* --- @dstr_ensure@ --- *
119 * Arguments: @dstr *d@ = pointer to a dynamic string block
120 * @size_t sz@ = amount of free space to ensure
124 * Use: Ensures that at least @sz@ bytes are available in the
128 void dstr_ensure(dstr *d, size_t sz)
130 size_t rq = d->len + sz;
133 /* --- If we have enough space, just leave it --- */
138 /* --- Grow the buffer --- *
140 * For small buffers, just double the size. For big buffers, make them
141 * a multiple of some suitably large chunk size.
149 else if (d->sz < 0x1000)
152 nsz = (rq + 0x0fff) & ~0x0fff;
156 d->buf = xrealloc(d->buf, nsz);
158 d->buf = xmalloc(nsz);
162 /* --- @dstr_putc@ --- *
164 * Arguments: @dstr *d@ = pointer to a dynamic string block
165 * @char ch@ = character to append
169 * Use: Appends a character to a string.
172 void dstr_putc(dstr *d, char ch)
177 /* --- @dstr_putz@ --- *
179 * Arguments: @dstr *d@ = pointer to a dynamic string block
183 * Use: Appends a null byte to a string. The null byte does not
184 * contribute to the string's length, and will be overwritten
185 * by subsequent `put' operations.
188 void dstr_putz(dstr *d)
193 /* --- @dstr_puts@ --- *
195 * Arguments: @dstr *d@ = pointer to a dynamic string block
196 * @const char *s@ = pointer to string to append
200 * Use: Appends a character string to a string. A trailing null
201 * byte is added, as for @dstr_putz@.
204 void dstr_puts(dstr *d, const char *s)
209 /* --- @dstr_vputf@ --- *
211 * Arguments: @dstr *d@ = pointer to a dynamic string block
212 * @const char *p@ = pointer to @printf@-style format string
213 * @va_list ap@ = argument handle
217 * Use: As for @dstr_putf@, but may be used as a back-end to user-
218 * supplied functions with @printf@-style interfaces.
221 int dstr_vputf(dstr *d, const char *p, va_list ap)
240 /* --- Most stuff gets passed on through --- */
247 /* --- Dump out what's between @q@ and @p@ --- */
252 /* --- Sort out the various silly flags and things --- */
262 /* --- Various simple flags --- */
281 /* --- Field widths and precision specifiers --- */
296 if (isdigit((unsigned char)*p)) {
306 *ip = va_arg(ap, int);
307 DENSURE(&dd, DSTR_PUTFSTEP);
308 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
313 while (isdigit((unsigned char)*p)) {
315 *ip = 10 * *ip + *p++ + '0';
321 /* --- Output formatting --- */
323 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
326 if ((f & f_prec) && prec + 16 > sz)
328 if ((f & f_wd) && wd + 1> sz)
332 d->len += sprintf(d->buf + d->len, dd.buf,
333 va_arg(ap, unsigned long));
335 d->len += sprintf(d->buf + d->len, dd.buf,
336 va_arg(ap, unsigned int));
339 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
343 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
347 if ((f & f_prec) == 0)
351 if ((f & f_wd) && wd + 1 > sz)
355 d->len += sprintf(d->buf + d->len, dd.buf,
356 va_arg(ap, long double));
358 d->len += sprintf(d->buf + d->len, dd.buf,
365 if ((f & f_wd) && wd + 1> sz)
368 d->len += sprintf(d->buf + d->len, dd.buf,
369 va_arg(ap, unsigned char));
373 const char *s = va_arg(ap, const char *);
379 if ((f & f_wd) && wd > sz)
382 d->len += sprintf(d->buf + d->len, dd.buf, s);
389 if ((f & f_prec) && prec + 16 > sz)
391 if ((f & f_wd) && wd + 1> sz)
394 d->len += sprintf(d->buf + d->len, dd.buf,
395 va_arg(ap, const void *));
400 *va_arg(ap, long *) = (long)(d->len - n);
401 else if (f & f_short)
402 *va_arg(ap, short *) = (short)(d->len - n);
404 *va_arg(ap, int *) = (int)(d->len - n);
407 /* --- Other random stuff --- */
427 /* --- @dstr_putf@ --- *
429 * Arguments: @dstr *d@ = pointer to a dynamic string block
430 * @const char *p@ = pointer to @printf@-style format string
431 * @...@ = argument handle
435 * Use: Writes a piece of text to a dynamic string, doing @printf@-
436 * style substitutions as it goes. Intended to be robust if
437 * faced with malicious arguments, but not if the format string
438 * itself is malicious.
441 int dstr_putf(dstr *d, const char *p, ...)
446 n = dstr_vputf(d, p, ap);
451 /* --- @dstr_putd@ --- *
453 * Arguments: @dstr *d@ = pointer to a dynamic string block
454 * @const dstr *s@ = pointer to a dynamic string to append
458 * Use: Appends a dynamic string to a string. A trailing null
459 * byte is added, as for @dstr_putz@.
462 void dstr_putd(dstr *d, const dstr *s)
467 /* --- @dstr_putm@ --- *
469 * Arguments: @dstr *d@ = pointer to a dynamic string block
470 * @const void *p@ = pointer to a block to append
471 * @size_t sz@ = size of the block
473 * Returns: Appends an arbitrary data block to a string. No trailing
477 void dstr_putm(dstr *d, const void *p, size_t sz)
482 /* --- @dstr_tidy@ --- *
484 * Arguments: @dstr *d@ = pointer to a dynamic string block
488 * Use: Reduces the amount of memory used by a string. A trailing
489 * null byte is added, as for @dstr_putz@.
492 void dstr_tidy(dstr *d)
495 d->buf = xrealloc(d->buf, d->len + 1);
499 /* --- @dstr_putline@ --- *
501 * Arguments: @dstr *d@ = pointer to a dynamic string block
502 * @FILE *fp@ = a stream to read from
504 * Returns: The number of characters read into the buffer, or @EOF@ if
505 * end-of-file was reached before any characters were read.
507 * Use: Appends the next line from the given input stream to the
508 * string. A trailing newline is not added; a trailing null
509 * byte is appended, as for @dstr_putz@.
512 int dstr_putline(dstr *d, FILE *fp)
514 size_t left = d->sz - d->len;
521 /* --- Make sure there's some buffer space --- */
528 /* --- Read the next byte --- */
532 /* --- End-of-file when no characters read is special --- */
534 if (ch == EOF && !rd)
537 /* --- End-of-file or newline ends the loop --- */
539 if (ch == EOF || ch == '\n') {
545 /* --- Append the character and continue --- */
552 /* --- @dstr_write@ --- *
554 * Arguments: @dstr *d@ = pointer to a dynamic string block
555 * @FILE *fp@ = a stream to write on
557 * Returns: The number of bytes written (as for @fwrite@).
559 * Use: Writes a dynamic string to a file.
562 size_t dstr_write(dstr *d, FILE *fp)
564 return (fwrite(d->buf, 1, d->len, fp));
567 /*----- That's all, folks -------------------------------------------------*/