3 * $Id: dputf.c,v 1.2 2000/08/15 21:26:45 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/08/15 21:26:45 mdw
34 * (dstr_vputf): Don't try calling @va_arg@ on things @char@-sized.
36 * Revision 1.1 1999/10/04 21:44:47 mdw
37 * Move `dstr_putf' and `dstr_vputf' into a separate source file.
41 /*----- Header files ------------------------------------------------------*/
53 /*----- Tunable constants -------------------------------------------------*/
56 * For each format specifier, at least @DSTR_PUTFSTEP@ bytes are ensured
57 * before writing the formatted result.
60 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
62 /*----- Main code ---------------------------------------------------------*/
64 /* --- @dstr_vputf@ --- *
66 * Arguments: @dstr *d@ = pointer to a dynamic string block
67 * @const char *p@ = pointer to @printf@-style format string
68 * @va_list ap@ = argument handle
70 * Returns: The number of characters written to the string.
72 * Use: As for @dstr_putf@, but may be used as a back-end to user-
73 * supplied functions with @printf@-style interfaces.
76 int dstr_vputf(dstr *d, const char *p, va_list ap)
95 /* --- Most stuff gets passed on through --- */
102 /* --- Dump out what's between @q@ and @p@ --- */
107 /* --- Sort out the various silly flags and things --- */
116 /* --- Various simple flags --- */
135 /* --- Field widths and precision specifiers --- */
150 if (isdigit((unsigned char)*p)) {
160 *ip = va_arg(ap, int);
161 DENSURE(&dd, DSTR_PUTFSTEP);
162 dd.len += sprintf(dd.buf + dd.len, "%i", *ip);
167 while (isdigit((unsigned char)*p)) {
169 *ip = 10 * *ip + *p++ - '0';
175 /* --- Output formatting --- */
177 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
180 if ((f & f_prec) && prec + 16 > sz)
182 if ((f & f_wd) && wd + 1> sz)
186 d->len += sprintf(d->buf + d->len, dd.buf,
187 va_arg(ap, unsigned long));
189 d->len += sprintf(d->buf + d->len, dd.buf,
190 va_arg(ap, unsigned int));
193 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
197 size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16;
201 if ((f & f_prec) == 0)
205 if ((f & f_wd) && wd + 1 > sz)
209 d->len += sprintf(d->buf + d->len, dd.buf,
210 va_arg(ap, long double));
212 d->len += sprintf(d->buf + d->len, dd.buf,
219 if ((f & f_wd) && wd + 1> sz)
222 d->len += sprintf(d->buf + d->len, dd.buf,
223 va_arg(ap, unsigned));
227 const char *s = va_arg(ap, const char *);
233 if ((f & f_wd) && wd > sz)
236 d->len += sprintf(d->buf + d->len, dd.buf, s);
243 if ((f & f_prec) && prec + 16 > sz)
245 if ((f & f_wd) && wd + 1> sz)
248 d->len += sprintf(d->buf + d->len, dd.buf,
249 va_arg(ap, const void *));
254 *va_arg(ap, long *) = (long)(d->len - n);
255 else if (f & f_short)
256 *va_arg(ap, short *) = (short)(d->len - n);
258 *va_arg(ap, int *) = (int)(d->len - n);
261 /* --- 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 -------------------------------------------------*/