3 * $Id: dputf.c,v 1.5 2002/01/13 13:30:48 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.5 2002/01/13 13:30:48 mdw
34 * Change interface for @dstr_vputf@.
36 * Revision 1.4 2001/06/22 19:35:29 mdw
37 * Find out whether @<float.h>@ exists (hack for uC-Linux).
39 * Revision 1.3 2001/01/20 12:06:01 mdw
40 * Define flags with macros, to ensure unsignedness.
42 * Revision 1.2 2000/08/15 21:26:45 mdw
43 * (dstr_vputf): Don't try calling @va_arg@ on things @char@-sized.
45 * Revision 1.1 1999/10/04 21:44:47 mdw
46 * Move `dstr_putf' and `dstr_vputf' into a separate source file.
50 /*----- Header files ------------------------------------------------------*/
65 /*----- Tunable constants -------------------------------------------------*/
68 * For each format specifier, at least @DSTR_PUTFSTEP@ bytes are ensured
69 * before writing the formatted result.
72 #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */
74 /*----- Main code ---------------------------------------------------------*/
76 /* --- @dstr_vputf@ --- *
78 * Arguments: @dstr *d@ = pointer to a dynamic string block
79 * @const char *p@ = pointer to @printf@-style format string
80 * @va_list *ap@ = argument handle
82 * Returns: The number of characters written to the string.
84 * Use: As for @dstr_putf@, but may be used as a back-end to user-
85 * supplied functions with @printf@-style interfaces.
88 int dstr_vputf(dstr
*d
, const char *p
, va_list *ap
)
105 /* --- Most stuff gets passed on through --- */
112 /* --- Dump out what's between @q@ and @p@ --- */
117 /* --- Sort out the various silly flags and things --- */
126 /* --- Various simple flags --- */
145 /* --- Field widths and precision specifiers --- */
160 if (isdigit((unsigned char)*p
)) {
170 *ip
= va_arg(*ap
, int);
171 DENSURE(&dd
, DSTR_PUTFSTEP
);
172 dd
.len
+= sprintf(dd
.buf
+ dd
.len
, "%i", *ip
);
177 while (isdigit((unsigned char)*p
)) {
179 *ip
= 10 * *ip
+ *p
++ - '0';
185 /* --- Output formatting --- */
187 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
190 if ((f
& f_prec
) && prec
+ 16 > sz
)
192 if ((f
& f_wd
) && wd
+ 1> sz
)
196 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
197 va_arg(*ap
, unsigned long));
199 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
200 va_arg(*ap
, unsigned int));
203 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
208 size_t mx
= (f
& f_Long ? LDBL_MAX_10_EXP
: DBL_MAX_10_EXP
) + 16;
216 if ((f
& f_wd
) && wd
+ 1 > sz
)
220 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
221 va_arg(*ap
, long double));
223 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
224 va_arg(*ap
, double));
227 DPUTS(d
, "<no float support>");
233 if ((f
& f_wd
) && wd
+ 1> sz
)
236 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
237 va_arg(*ap
, unsigned));
241 const char *s
= va_arg(*ap
, const char *);
247 if ((f
& f_wd
) && wd
> sz
)
250 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
, s
);
257 if ((f
& f_prec
) && prec
+ 16 > sz
)
259 if ((f
& f_wd
) && wd
+ 1> sz
)
262 d
->len
+= sprintf(d
->buf
+ d
->len
, dd
.buf
,
263 va_arg(*ap
, const void *));
268 *va_arg(*ap
, long *) = (long)(d
->len
- n
);
269 else if (f
& f_short
)
270 *va_arg(*ap
, short *) = (short)(d
->len
- n
);
272 *va_arg(*ap
, int *) = (int)(d
->len
- n
);
275 /* --- Other random stuff --- */
302 /* --- @dstr_putf@ --- *
304 * Arguments: @dstr *d@ = pointer to a dynamic string block
305 * @const char *p@ = pointer to @printf@-style format string
306 * @...@ = argument handle
308 * Returns: The number of characters written to the string.
310 * Use: Writes a piece of text to a dynamic string, doing @printf@-
311 * style substitutions as it goes. Intended to be robust if
312 * faced with malicious arguments, but not if the format string
313 * itself is malicious.
316 int dstr_putf(dstr
*d
, const char *p
, ...)
321 n
= dstr_vputf(d
, p
, &ap
);
326 /*----- That's all, folks -------------------------------------------------*/