3 * $Id: dstr.c,v 1.6 1999/05/21 08:38:33 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.6 1999/05/21 08:38:33 mdw
34 * Implement some more functions in terms of macros.
36 * Revision 1.5 1999/05/13 22:47:57 mdw
37 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
39 * Revision 1.4 1999/05/06 19:51:35 mdw
40 * Reformatted the LGPL notice a little bit.
42 * Revision 1.3 1999/05/05 18:50:31 mdw
43 * Change licensing conditions to LGPL.
45 * Revision 1.2 1998/12/15 23:53:22 mdw
46 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
47 * formatting in a safe way.
49 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
50 * Initial version of mLib
54 /*----- Header files ------------------------------------------------------*/
67 /*----- Tunable constants -------------------------------------------------*/
69 #define DSTR_INITSZ 256 /* Initial buffer size */
70 #define DSTR_INCSZ 4096 /* Threshhold for doubling */
71 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
73 /*----- Main code ---------------------------------------------------------*/
75 /* --- @dstr_create@ --- *
77 * Arguments: @dstr *d@ = pointer to a dynamic string block
81 * Use: Initializes a dynamic string.
84 void dstr_create(dstr
*d
) { DCREATE(d
); }
86 /* --- @dstr_destroy@ --- *
88 * Arguments: @dstr *d@ = pointer to a dynamic string block
92 * Use: Reclaims the space used by a dynamic string.
95 void dstr_destroy(dstr
*d
) { DDESTROY(d
); }
97 /* --- @dstr_reset@ --- *
99 * Arguments: @dstr *d@ = pointer to a dynaimc string block
103 * Use: Resets a string so that new data gets put at the beginning.
106 void dstr_reset(dstr
*d
) { DRESET(d
); }
108 /* --- @dstr_ensure@ --- *
110 * Arguments: @dstr *d@ = pointer to a dynamic string block
111 * @size_t sz@ = amount of free space to ensure
115 * Use: Ensures that at least @sz@ bytes are available in the
119 void dstr_ensure(dstr
*d
, size_t sz
)
121 size_t rq
= d
->len
+ sz
;
124 /* --- If we have enough space, just leave it --- */
129 /* --- Grow the buffer --- *
131 * For small buffers, just double the size. For big buffers, make them
132 * a multiple of some suitably large chunk size.
140 else if (d
->sz
< 0x1000)
143 nsz
= (rq
+ 0x0fff) & ~0x0fff;
147 d
->buf
= xrealloc(d
->buf
, nsz
);
149 d
->buf
= xmalloc(nsz
);
153 /* --- @dstr_putc@ --- *
155 * Arguments: @dstr *d@ = pointer to a dynamic string block
156 * @char ch@ = character to append
160 * Use: Appends a character to a string.
163 void dstr_putc(dstr
*d
, char ch
) { DPUTC(d
, ch
); }
165 /* --- @dstr_putz@ --- *
167 * Arguments: @dstr *d@ = pointer to a dynamic string block
171 * Use: Appends a null byte to a string. The null byte does not
172 * contribute to the string's length, and will be overwritten
173 * by subsequent `put' operations.
176 void dstr_putz(dstr
*d
) { DPUTZ(d
); }
178 /* --- @dstr_puts@ --- *
180 * Arguments: @dstr *d@ = pointer to a dynamic string block
181 * @const char *s@ = pointer to string to append
185 * Use: Appends a character string to a string. A trailing null
186 * byte is added, as for @dstr_putz@.
189 void dstr_puts(dstr
*d
, const char *s
) { DPUTS(d
, s
); }
191 /* --- @dstr_vputf@ --- *
193 * Arguments: @dstr *d@ = pointer to a dynamic string block
194 * @const char *p@ = pointer to @printf@-style format string
195 * @va_list ap@ = argument handle
197 * Returns: The number of characters written to the string.
199 * Use: As for @dstr_putf@, but may be used as a back-end to user-
200 * supplied functions with @printf@-style interfaces.
203 int dstr_vputf(dstr
*d
, const char *p
, va_list ap
)
222 /* --- Most stuff gets passed on through --- */
229 /* --- Dump out what's between @q@ and @p@ --- */
234 /* --- Sort out the various silly flags and things --- */
244 /* --- Various simple flags --- */
263 /* --- Field widths and precision specifiers --- */
278 if (isdigit((unsigned char)*p
)) {
288 *ip
= va_arg(ap
, int);
289 DENSURE(&dd
, DSTR_PUTFSTEP
);
290 dd
.len
+= sprintf(dd
.buf
+ dd
.len
, "%i", *ip
);
295 while (isdigit((unsigned char)*p
)) {
297 *ip
= 10 * *ip
+ *p
++ + '0';
303 /* --- Output formatting --- */
305 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
308 if ((f
& f_prec
) && prec
+ 16 > sz
)
310 if ((f
& f_wd
) && wd
+ 1> sz
)
314 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
315 va_arg(ap
, unsigned long));
317 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
318 va_arg(ap
, unsigned int));
321 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
325 size_t mx
= (f
& f_Long ? LDBL_MAX_10_EXP
: DBL_MAX_10_EXP
) + 16;
329 if ((f
& f_prec
) == 0)
333 if ((f
& f_wd
) && wd
+ 1 > sz
)
337 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
338 va_arg(ap
, long double));
340 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
347 if ((f
& f_wd
) && wd
+ 1> sz
)
350 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
351 va_arg(ap
, unsigned char));
355 const char *s
= va_arg(ap
, const char *);
361 if ((f
& f_wd
) && wd
> sz
)
364 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
, s
);
371 if ((f
& f_prec
) && prec
+ 16 > sz
)
373 if ((f
& f_wd
) && wd
+ 1> sz
)
376 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
377 va_arg(ap
, const void *));
382 *va_arg(ap
, long *) = (long)(d
->len
- n
);
383 else if (f
& f_short
)
384 *va_arg(ap
, short *) = (short)(d
->len
- n
);
386 *va_arg(ap
, int *) = (int)(d
->len
- n
);
389 /* --- Other random stuff --- */
409 /* --- @dstr_putf@ --- *
411 * Arguments: @dstr *d@ = pointer to a dynamic string block
412 * @const char *p@ = pointer to @printf@-style format string
413 * @...@ = argument handle
415 * Returns: The number of characters written to the string.
417 * Use: Writes a piece of text to a dynamic string, doing @printf@-
418 * style substitutions as it goes. Intended to be robust if
419 * faced with malicious arguments, but not if the format string
420 * itself is malicious.
423 int dstr_putf(dstr
*d
, const char *p
, ...)
428 n
= dstr_vputf(d
, p
, ap
);
433 /* --- @dstr_putd@ --- *
435 * Arguments: @dstr *d@ = pointer to a dynamic string block
436 * @const dstr *s@ = pointer to a dynamic string to append
440 * Use: Appends a dynamic string to a string. A trailing null
441 * byte is added, as for @dstr_putz@.
444 void dstr_putd(dstr
*d
, const dstr
*s
) { DPUTD(d
, s
); }
446 /* --- @dstr_putm@ --- *
448 * Arguments: @dstr *d@ = pointer to a dynamic string block
449 * @const void *p@ = pointer to a block to append
450 * @size_t sz@ = size of the block
452 * Returns: Appends an arbitrary data block to a string. No trailing
456 void dstr_putm(dstr
*d
, const void *p
, size_t sz
) { DPUTM(d
, p
, sz
); }
458 /* --- @dstr_tidy@ --- *
460 * Arguments: @dstr *d@ = pointer to a dynamic string block
464 * Use: Reduces the amount of memory used by a string. A trailing
465 * null byte is added, as for @dstr_putz@.
468 void dstr_tidy(dstr
*d
)
471 d
->buf
= xrealloc(d
->buf
, d
->len
+ 1);
475 /* --- @dstr_putline@ --- *
477 * Arguments: @dstr *d@ = pointer to a dynamic string block
478 * @FILE *fp@ = a stream to read from
480 * Returns: The number of characters read into the buffer, or @EOF@ if
481 * end-of-file was reached before any characters were read.
483 * Use: Appends the next line from the given input stream to the
484 * string. A trailing newline is not added; a trailing null
485 * byte is appended, as for @dstr_putz@.
488 int dstr_putline(dstr
*d
, FILE *fp
)
490 size_t left
= d
->sz
- d
->len
;
497 /* --- Make sure there's some buffer space --- */
504 /* --- Read the next byte --- */
508 /* --- End-of-file when no characters read is special --- */
510 if (ch
== EOF
&& !rd
)
513 /* --- End-of-file or newline ends the loop --- */
515 if (ch
== EOF
|| ch
== '\n') {
521 /* --- Append the character and continue --- */
528 /* --- @dstr_write@ --- *
530 * Arguments: @dstr *d@ = pointer to a dynamic string block
531 * @FILE *fp@ = a stream to write on
533 * Returns: The number of bytes written (as for @fwrite@).
535 * Use: Writes a dynamic string to a file.
538 size_t dstr_write(const dstr
*d
, FILE *fp
) { return (DWRITE(d
, fp
)); }
540 /*----- That's all, folks -------------------------------------------------*/