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 ------------------------------------------------------*/
47 /*----- Tunable constants -------------------------------------------------*/
50 * For each format specifier, at least @DSTR_PUTFSTEP@ bytes are ensured
51 * before writing the formatted result.
54 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @dstr_vputf@ --- *
60 * Arguments: @dstr *d@ = pointer to a dynamic string block
61 * @const char *p@ = pointer to @printf@-style format string
62 * @va_list *ap@ = argument handle
64 * Returns: The number of characters written to the string.
66 * Use: As for @dstr_putf@, but may be used as a back-end to user-
67 * supplied functions with @printf@-style interfaces.
70 int dstr_vputf(dstr *d, const char *p, va_list *ap)
87 /* --- Most stuff gets passed on through --- */
94 /* --- Dump out what's between @q@ and @p@ --- */
99 /* --- Sort out the various silly flags and things --- */
108 /* --- Various simple flags --- */
127 /* --- Field widths and precision specifiers --- */
143 if (isdigit((unsigned char)*p)) {
153 *ip = va_arg(*ap, int);
154 DENSURE(&dd, DSTR_PUTFSTEP);
155 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
160 while (isdigit((unsigned char)*p)) {
162 *ip = 10 * *ip + *p++ - '0';
168 /* --- Output formatting --- */
170 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
173 if ((f & f_prec) && prec + 16 > sz)
175 if ((f & f_wd) && wd + 1> sz)
179 d->len += sprintf(d->buf + d->len, dd.buf,
180 va_arg(*ap, unsigned long));
182 d->len += sprintf(d->buf + d->len, dd.buf,
183 va_arg(*ap, unsigned int));
186 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
191 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
199 if ((f & f_wd) && wd + 1 > sz)
203 d->len += sprintf(d->buf + d->len, dd.buf,
204 va_arg(*ap, long double));
206 d->len += sprintf(d->buf + d->len, dd.buf,
207 va_arg(*ap, double));
210 DPUTS(d, "<no float support>");
216 if ((f & f_wd) && wd + 1> sz)
219 d->len += sprintf(d->buf + d->len, dd.buf,
220 va_arg(*ap, unsigned));
224 const char *s = va_arg(*ap, const char *);
230 if ((f & f_wd) && wd > sz)
233 d->len += sprintf(d->buf + d->len, dd.buf, s);
240 if ((f & f_prec) && prec + 16 > sz)
242 if ((f & f_wd) && wd + 1> sz)
245 d->len += sprintf(d->buf + d->len, dd.buf,
246 va_arg(*ap, const void *));
251 *va_arg(*ap, long *) = (long)(d->len - n);
252 else if (f & f_short)
253 *va_arg(*ap, short *) = (short)(d->len - n);
255 *va_arg(*ap, int *) = (int)(d->len - n);
258 /* --- Other random stuff --- */
285 /* --- @dstr_putf@ --- *
287 * Arguments: @dstr *d@ = pointer to a dynamic string block
288 * @const char *p@ = pointer to @printf@-style format string
289 * @...@ = argument handle
291 * Returns: The number of characters written to the string.
293 * Use: Writes a piece of text to a dynamic string, doing @printf@-
294 * style substitutions as it goes. Intended to be robust if
295 * faced with malicious arguments, but not if the format string
296 * itself is malicious.
299 int dstr_putf(dstr *d, const char *p, ...)
304 n = dstr_vputf(d, p, &ap);
309 /*----- That's all, folks -------------------------------------------------*/