3 * `printf'-style formatting for dynamic strings
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- 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 --- */
141 if (isdigit((unsigned char)*p)) {
151 *ip = va_arg(*ap, int);
152 DENSURE(&dd, DSTR_PUTFSTEP);
153 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
159 while (isdigit((unsigned char)*p)) {
161 *ip = 10 * *ip + *p++ - '0';
167 /* --- Output formatting --- */
169 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
172 if ((f & f_prec) && prec + 16 > sz)
174 if ((f & f_wd) && wd + 1> sz)
178 d->len += sprintf(d->buf + d->len, dd.buf,
179 va_arg(*ap, unsigned long));
181 d->len += sprintf(d->buf + d->len, dd.buf,
182 va_arg(*ap, unsigned int));
185 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
190 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
198 if ((f & f_wd) && wd + 1 > sz)
202 d->len += sprintf(d->buf + d->len, dd.buf,
203 va_arg(*ap, long double));
205 d->len += sprintf(d->buf + d->len, dd.buf,
206 va_arg(*ap, double));
209 DPUTS(d, "<no float support>");
215 if ((f & f_wd) && wd + 1> sz)
218 d->len += sprintf(d->buf + d->len, dd.buf,
219 va_arg(*ap, unsigned));
223 const char *s = va_arg(*ap, const char *);
229 if ((f & f_wd) && wd > sz)
232 d->len += sprintf(d->buf + d->len, dd.buf, s);
239 if ((f & f_prec) && prec + 16 > sz)
241 if ((f & f_wd) && wd + 1> sz)
244 d->len += sprintf(d->buf + d->len, dd.buf,
245 va_arg(*ap, const void *));
250 *va_arg(*ap, long *) = (long)(d->len - n);
251 else if (f & f_short)
252 *va_arg(*ap, short *) = (short)(d->len - n);
254 *va_arg(*ap, int *) = (int)(d->len - n);
257 /* --- Other random stuff --- */
284 /* --- @dstr_putf@ --- *
286 * Arguments: @dstr *d@ = pointer to a dynamic string block
287 * @const char *p@ = pointer to @printf@-style format string
288 * @...@ = argument handle
290 * Returns: The number of characters written to the string.
292 * Use: Writes a piece of text to a dynamic string, doing @printf@-
293 * style substitutions as it goes. Intended to be robust if
294 * faced with malicious arguments, but not if the format string
295 * itself is malicious.
298 int dstr_putf(dstr *d, const char *p, ...)
303 n = dstr_vputf(d, p, &ap);
308 /*----- That's all, folks -------------------------------------------------*/