38eb8f6d1c1580c921220b6ecab7184a3df2b348
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
);
158 while (isdigit((unsigned char)*p
)) {
160 *ip
= 10 * *ip
+ *p
++ - '0';
166 /* --- Output formatting --- */
168 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
171 if ((f
& f_prec
) && prec
+ 16 > sz
)
173 if ((f
& f_wd
) && wd
+ 1> sz
)
177 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
178 va_arg(*ap
, unsigned long));
180 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
181 va_arg(*ap
, unsigned int));
184 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
189 size_t mx
= (f
& f_Long ? LDBL_MAX_10_EXP
: DBL_MAX_10_EXP
) + 16;
197 if ((f
& f_wd
) && wd
+ 1 > sz
)
201 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
202 va_arg(*ap
, long double));
204 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
205 va_arg(*ap
, double));
208 DPUTS(d
, "<no float support>");
214 if ((f
& f_wd
) && wd
+ 1> sz
)
217 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
218 va_arg(*ap
, unsigned));
222 const char *s
= va_arg(*ap
, const char *);
228 if ((f
& f_wd
) && wd
> sz
)
231 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
, s
);
238 if ((f
& f_prec
) && prec
+ 16 > sz
)
240 if ((f
& f_wd
) && wd
+ 1> sz
)
243 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
244 va_arg(*ap
, const void *));
249 *va_arg(*ap
, long *) = (long)(d
->len
- n
);
250 else if (f
& f_short
)
251 *va_arg(*ap
, short *) = (short)(d
->len
- n
);
253 *va_arg(*ap
, int *) = (int)(d
->len
- n
);
256 /* --- Other random stuff --- */
283 /* --- @dstr_putf@ --- *
285 * Arguments: @dstr *d@ = pointer to a dynamic string block
286 * @const char *p@ = pointer to @printf@-style format string
287 * @...@ = argument handle
289 * Returns: The number of characters written to the string.
291 * Use: Writes a piece of text to a dynamic string, doing @printf@-
292 * style substitutions as it goes. Intended to be robust if
293 * faced with malicious arguments, but not if the format string
294 * itself is malicious.
297 int dstr_putf(dstr
*d
, const char *p
, ...)
302 n
= dstr_vputf(d
, p
, &ap
);
307 /*----- That's all, folks -------------------------------------------------*/