3 * $Id: dstr.c,v 1.2 1998/12/15 23:53:22 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 General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1998/12/15 23:53:22 mdw
33 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
34 * formatting in a safe way.
36 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
37 * Initial version of mLib
41 /*----- Header files ------------------------------------------------------*/
54 /*----- Tunable constants -------------------------------------------------*/
56 #define DSTR_INITSZ 256 /* Initial buffer size */
57 #define DSTR_INCSZ 4096 /* Threshhold for doubling */
58 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
60 /*----- Main code ---------------------------------------------------------*/
62 /* --- @dstr_create@ --- *
64 * Arguments: @dstr *d@ = pointer to a dynamic string block
68 * Use: Initialises a dynamic string.
71 void dstr_create(dstr
*d
)
78 /* --- @dstr_destroy@ --- *
80 * Arguments: @dstr *d@ = pointer to a dynamic string block
84 * Use: Reclaims the space used by a dynamic string.
87 void dstr_destroy(dstr
*d
)
96 /* --- @dstr_reset@ --- *
98 * Arguments: @dstr *d@ = pointer to a dynaimc string block
102 * Use: Resets a string so that new data gets put at the beginning.
105 void dstr_reset(dstr
*d
)
110 /* --- @dstr_ensure@ --- *
112 * Arguments: @dstr *d@ = pointer to a dynamic string block
113 * @size_t sz@ = amount of free space to ensure
117 * Use: Ensures that at least @sz@ bytes are available in the
121 void dstr_ensure(dstr
*d
, size_t sz
)
123 size_t rq
= d
->len
+ sz
;
126 /* --- If we have enough space, just leave it --- */
131 /* --- Grow the buffer --- *
133 * For small buffers, just double the size. For big buffers, make them
134 * a multiple of some suitably large chunk size.
142 else if (d
->sz
< 0x1000)
145 nsz
= (rq
+ 0x0fff) & ~0x0fff;
149 d
->buf
= xrealloc(d
->buf
, nsz
);
151 d
->buf
= xmalloc(nsz
);
155 /* --- @dstr_putc@ --- *
157 * Arguments: @dstr *d@ = pointer to a dynamic string block
158 * @char ch@ = character to append
162 * Use: Appends a character to a string.
165 void dstr_putc(dstr
*d
, char ch
)
170 /* --- @dstr_putz@ --- *
172 * Arguments: @dstr *d@ = pointer to a dynamic string block
176 * Use: Appends a null byte to a string. The null byte does not
177 * contribute to the string's length, and will be overwritten
178 * by subsequent `put' operations.
181 void dstr_putz(dstr
*d
)
186 /* --- @dstr_puts@ --- *
188 * Arguments: @dstr *d@ = pointer to a dynamic string block
189 * @const char *s@ = pointer to string to append
193 * Use: Appends a character string to a string. A trailing null
194 * byte is added, as for @dstr_putz@.
197 void dstr_puts(dstr
*d
, const char *s
)
202 /* --- @dstr_vputf@ --- *
204 * Arguments: @dstr *d@ = pointer to a dynamic string block
205 * @const char *p@ = pointer to @printf@-style format string
206 * @va_list ap@ = argument handle
210 * Use: As for @dstr_putf@, but may be used as a back-end to user-
211 * supplied functions with @printf@-style interfaces.
214 int dstr_vputf(dstr
*d
, const char *p
, va_list ap
)
233 /* --- Most stuff gets passed on through --- */
240 /* --- Dump out what's between @q@ and @p@ --- */
245 /* --- Sort out the various silly flags and things --- */
255 /* --- Various simple flags --- */
274 /* --- Field widths and precision specifiers --- */
289 if (isdigit((unsigned char)*p
)) {
299 *ip
= va_arg(ap
, int);
300 DENSURE(&dd
, DSTR_PUTFSTEP
);
301 dd
.len
+= sprintf(dd
.buf
+ dd
.len
, "%i", *ip
);
306 while (isdigit((unsigned char)*p
)) {
308 *ip
= 10 * *ip
+ *p
++ + '0';
314 /* --- Output formatting --- */
316 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
319 if ((f
& f_prec
) && prec
+ 16 > sz
)
321 if ((f
& f_wd
) && wd
+ 1> sz
)
325 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
326 va_arg(ap
, unsigned long));
328 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
329 va_arg(ap
, unsigned int));
332 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
336 size_t mx
= (f
& f_Long ? LDBL_MAX_10_EXP
: DBL_MAX_10_EXP
) + 16;
340 if ((f
& f_prec
) == 0)
344 if ((f
& f_wd
) && wd
+ 1 > sz
)
348 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
349 va_arg(ap
, long double));
351 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
358 if ((f
& f_wd
) && wd
+ 1> sz
)
361 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
362 va_arg(ap
, unsigned char));
366 const char *s
= va_arg(ap
, const char *);
372 if ((f
& f_wd
) && wd
> sz
)
375 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
, s
);
382 if ((f
& f_prec
) && prec
+ 16 > sz
)
384 if ((f
& f_wd
) && wd
+ 1> sz
)
387 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
388 va_arg(ap
, const void *));
393 *va_arg(ap
, long *) = (long)(d
->len
- n
);
394 else if (f
& f_short
)
395 *va_arg(ap
, short *) = (short)(d
->len
- n
);
397 *va_arg(ap
, int *) = (int)(d
->len
- n
);
400 /* --- Other random stuff --- */
420 /* --- @dstr_putf@ --- *
422 * Arguments: @dstr *d@ = pointer to a dynamic string block
423 * @const char *p@ = pointer to @printf@-style format string
424 * @...@ = argument handle
428 * Use: Writes a piece of text to a dynamic string, doing @printf@-
429 * style substitutions as it goes. Intended to be robust if
430 * faced with malicious arguments, but not if the format string
431 * itself is malicious.
434 int dstr_putf(dstr
*d
, const char *p
, ...)
439 n
= dstr_vputf(d
, p
, ap
);
444 /* --- @dstr_putd@ --- *
446 * Arguments: @dstr *d@ = pointer to a dynamic string block
447 * @const dstr *s@ = pointer to a dynamic string to append
451 * Use: Appends a dynamic string to a string. A trailing null
452 * byte is added, as for @dstr_putz@.
455 void dstr_putd(dstr
*d
, const dstr
*s
)
460 /* --- @dstr_putm@ --- *
462 * Arguments: @dstr *d@ = pointer to a dynamic string block
463 * @const void *p@ = pointer to a block to append
464 * @size_t sz@ = size of the block
466 * Returns: Appends an arbitrary data block to a string. No trailing
470 void dstr_putm(dstr
*d
, const void *p
, size_t sz
)
475 /* --- @dstr_tidy@ --- *
477 * Arguments: @dstr *d@ = pointer to a dynamic string block
481 * Use: Reduces the amount of memory used by a string. A trailing
482 * null byte is added, as for @dstr_putz@.
485 void dstr_tidy(dstr
*d
)
488 d
->buf
= xrealloc(d
->buf
, d
->len
+ 1);
492 /* --- @dstr_putline@ --- *
494 * Arguments: @dstr *d@ = pointer to a dynamic string block
495 * @FILE *fp@ = a stream to read from
497 * Returns: The number of characters read into the buffer, or @EOF@ if
498 * end-of-file was reached before any characters were read.
500 * Use: Appends the next line from the given input stream to the
501 * string. A trailing newline is not added; a trailing null
502 * byte is appended, as for @dstr_putz@.
505 int dstr_putline(dstr
*d
, FILE *fp
)
507 size_t left
= d
->sz
- d
->len
;
514 /* --- Make sure there's some buffer space --- */
521 /* --- Read the next byte --- */
525 /* --- End-of-file when no characters read is special --- */
527 if (ch
== EOF
&& !rd
)
530 /* --- End-of-file or newline ends the loop --- */
532 if (ch
== EOF
|| ch
== '\n') {
538 /* --- Append the character and continue --- */
545 /* --- @dstr_write@ --- *
547 * Arguments: @dstr *d@ = pointer to a dynamic string block
548 * @FILE *fp@ = a stream to write on
550 * Returns: The number of bytes written (as for @fwrite@).
552 * Use: Writes a dynamic string to a file.
555 size_t dstr_write(dstr
*d
, FILE *fp
)
557 return (fwrite(d
->buf
, 1, d
->len
, fp
));
560 /*----- That's all, folks -------------------------------------------------*/