3 * $Id: dputf.c,v 1.1 1999/10/04 21:44:47 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.1 1999/10/04 21:44:47 mdw
34 * Move `dstr_putf' and `dstr_vputf' into a separate source file.
38 /*----- Header files ------------------------------------------------------*/
50 /*----- Tunable constants -------------------------------------------------*/
53 * For each format specifier, at least @DSTR_PUTFSTEP@ bytes are ensured
54 * before writing the formatted result.
57 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @dstr_vputf@ --- *
63 * Arguments: @dstr *d@ = pointer to a dynamic string block
64 * @const char *p@ = pointer to @printf@-style format string
65 * @va_list ap@ = argument handle
67 * Returns: The number of characters written to the string.
69 * Use: As for @dstr_putf@, but may be used as a back-end to user-
70 * supplied functions with @printf@-style interfaces.
73 int dstr_vputf(dstr
*d
, const char *p
, va_list ap
)
92 /* --- Most stuff gets passed on through --- */
99 /* --- Dump out what's between @q@ and @p@ --- */
104 /* --- Sort out the various silly flags and things --- */
113 /* --- Various simple flags --- */
132 /* --- Field widths and precision specifiers --- */
147 if (isdigit((unsigned char)*p
)) {
157 *ip
= va_arg(ap
, int);
158 DENSURE(&dd
, DSTR_PUTFSTEP
);
159 dd
.len
+= sprintf(dd
.buf
+ dd
.len
, "%i", *ip
);
164 while (isdigit((unsigned char)*p
)) {
166 *ip
= 10 * *ip
+ *p
++ - '0';
172 /* --- Output formatting --- */
174 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
177 if ((f
& f_prec
) && prec
+ 16 > sz
)
179 if ((f
& f_wd
) && wd
+ 1> sz
)
183 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
184 va_arg(ap
, unsigned long));
186 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
187 va_arg(ap
, unsigned int));
190 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
194 size_t mx
= (f
& f_Long ? LDBL_MAX_10_EXP
: DBL_MAX_10_EXP
) + 16;
198 if ((f
& f_prec
) == 0)
202 if ((f
& f_wd
) && wd
+ 1 > sz
)
206 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
207 va_arg(ap
, long double));
209 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
216 if ((f
& f_wd
) && wd
+ 1> sz
)
219 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
220 va_arg(ap
, unsigned char));
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 --- */
279 /* --- @dstr_putf@ --- *
281 * Arguments: @dstr *d@ = pointer to a dynamic string block
282 * @const char *p@ = pointer to @printf@-style format string
283 * @...@ = argument handle
285 * Returns: The number of characters written to the string.
287 * Use: Writes a piece of text to a dynamic string, doing @printf@-
288 * style substitutions as it goes. Intended to be robust if
289 * faced with malicious arguments, but not if the format string
290 * itself is malicious.
293 int dstr_putf(dstr
*d
, const char *p
, ...)
298 n
= dstr_vputf(d
, p
, ap
);
303 /*----- That's all, folks -------------------------------------------------*/