3 * $Id: dputf.c,v 1.6 2004/04/08 01:36:11 mdw Exp $
5 * `printf'-style formatting for dynamic strings
7 * (c) 1999 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 /*----- Header files ------------------------------------------------------*/
45 /*----- Tunable constants -------------------------------------------------*/
48 * For each format specifier, at least @DSTR_PUTFSTEP@ bytes are ensured
49 * before writing the formatted result.
52 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @dstr_vputf@ --- *
58 * Arguments: @dstr *d@ = pointer to a dynamic string block
59 * @const char *p@ = pointer to @printf@-style format string
60 * @va_list *ap@ = argument handle
62 * Returns: The number of characters written to the string.
64 * Use: As for @dstr_putf@, but may be used as a back-end to user-
65 * supplied functions with @printf@-style interfaces.
68 int dstr_vputf(dstr *d, const char *p, va_list *ap)
85 /* --- Most stuff gets passed on through --- */
92 /* --- Dump out what's between @q@ and @p@ --- */
97 /* --- Sort out the various silly flags and things --- */
106 /* --- Various simple flags --- */
125 /* --- Field widths and precision specifiers --- */
140 if (isdigit((unsigned char)*p)) {
150 *ip = va_arg(*ap, int);
151 DENSURE(&dd, DSTR_PUTFSTEP);
152 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
157 while (isdigit((unsigned char)*p)) {
159 *ip = 10 * *ip + *p++ - '0';
165 /* --- Output formatting --- */
167 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
170 if ((f & f_prec) && prec + 16 > sz)
172 if ((f & f_wd) && wd + 1> sz)
176 d->len += sprintf(d->buf + d->len, dd.buf,
177 va_arg(*ap, unsigned long));
179 d->len += sprintf(d->buf + d->len, dd.buf,
180 va_arg(*ap, unsigned int));
183 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
188 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
196 if ((f & f_wd) && wd + 1 > sz)
200 d->len += sprintf(d->buf + d->len, dd.buf,
201 va_arg(*ap, long double));
203 d->len += sprintf(d->buf + d->len, dd.buf,
204 va_arg(*ap, double));
207 DPUTS(d, "<no float support>");
213 if ((f & f_wd) && wd + 1> sz)
216 d->len += sprintf(d->buf + d->len, dd.buf,
217 va_arg(*ap, unsigned));
221 const char *s = va_arg(*ap, const char *);
227 if ((f & f_wd) && wd > sz)
230 d->len += sprintf(d->buf + d->len, dd.buf, s);
237 if ((f & f_prec) && prec + 16 > sz)
239 if ((f & f_wd) && wd + 1> sz)
242 d->len += sprintf(d->buf + d->len, dd.buf,
243 va_arg(*ap, const void *));
248 *va_arg(*ap, long *) = (long)(d->len - n);
249 else if (f & f_short)
250 *va_arg(*ap, short *) = (short)(d->len - n);
252 *va_arg(*ap, int *) = (int)(d->len - n);
255 /* --- Other random stuff --- */
282 /* --- @dstr_putf@ --- *
284 * Arguments: @dstr *d@ = pointer to a dynamic string block
285 * @const char *p@ = pointer to @printf@-style format string
286 * @...@ = argument handle
288 * Returns: The number of characters written to the string.
290 * Use: Writes a piece of text to a dynamic string, doing @printf@-
291 * style substitutions as it goes. Intended to be robust if
292 * faced with malicious arguments, but not if the format string
293 * itself is malicious.
296 int dstr_putf(dstr *d, const char *p, ...)
301 n = dstr_vputf(d, p, &ap);
306 /*----- That's all, folks -------------------------------------------------*/