3 * Generalized string formatting
5 * (c) 2023 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 it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * 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 Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
53 /*----- Tunable constants -------------------------------------------------*/
55 /* For each format specifier, at least @STEP@ bytes are ensured before
56 * writing the formatted result.
59 #define STEP 64 /* Buffer size for @vgprintf@ */
61 /*----- Preliminary definitions -------------------------------------------*/
64 # define IF_FLOAT(x) x
69 #if defined(LLONG_MAX) || defined(LONG_LONG_MAX)
70 # define IF_LONGLONG(x) x
72 # define IF_LONGLONG(x)
76 # define IF_INTMAX(x) x
81 #define OUTPUT_FMTTYPES(_) \
83 _(li, unsigned long) \
84 IF_LONGLONG( _(lli, unsigned long long) ) \
87 IF_INTMAX( _(ji, uintmax_t) ) \
93 #define PERCENT_N_FMTTYPES(_) \
100 IF_LONGLONG( _(lln, long long *) ) \
101 IF_INTMAX( _(jn, intmax_t *) )
103 #define FMTTYPES(_) \
105 PERCENT_N_FMTTYPES(_)
109 #define CODE(code, ty) fmt_##code,
118 #define MEMB(code, ty) ty code;
124 DA_DECL(fmtarg_v
, struct fmtarg
);
138 #define f_len 0x000fu
140 #define f_wdarg 0x0020u
141 #define f_prec 0x0040u
142 #define f_precarg 0x0080u
143 #define f_plus 0x0100u
144 #define f_minus 0x0200u
145 #define f_sharp 0x0400u
146 #define f_zero 0x0800u
147 #define f_posarg 0x1000u
158 DA_DECL(fmtspec_v
, struct fmtspec
);
160 /*----- Main code ---------------------------------------------------------*/
162 /* --- @vgprintf@ --- *
164 * Arguments: @const struct gprintf_ops *ops@ = output operations
165 * @void *out@ = context for output operations
166 * @const char *p@ = pointer to @printf@-style format string
167 * @va_list *ap@ = argument handle
169 * Returns: The number of characters written to the string.
171 * Use: As for @gprintf@, but takes a reified argument tail.
174 static void set_arg(fmtarg_v
*av
, size_t i
, int fmt
)
180 DA_ENSURE(av
, i
+ 1 - n
);
181 for (j
= n
; j
<= i
; j
++) DA(av
)[j
].fmt
= fmt_unset
;
182 DA_UNSAFE_EXTEND(av
, i
+ 1 - n
);
185 if (DA(av
)[i
].fmt
== fmt_unset
) DA(av
)[i
].fmt
= fmt
;
186 else assert(DA(av
)[i
].fmt
== fmt
);
189 int vgprintf(const struct gprintf_ops
*ops
, void *out
,
190 const char *p
, va_list *ap
)
194 fmtspec_v sv
= DA_INIT
;
195 fmtarg_v av
= DA_INIT
;
196 struct fmtarg
*fa
, *fal
;
197 struct fmtspec
*fs
, *fsl
;
199 int i
, anext
, tot
= 0;
202 /* --- Initial pass through the input, parsing format specifiers --- *
204 * We essentially compile the format string into a vector of @fmtspec@
205 * objects, each of which represents a chunk of literal text followed by a
206 * (possibly imaginary, in the case of the final one) formatting directive.
207 * Output then simply consists of interpreting these specifiers in order.
215 fs
= &DA(&sv
)[DA_LEN(&sv
)];
216 DA_UNSAFE_EXTEND(&sv
, 1);
218 /* --- Find the end of this literal portion --- */
221 while (*p
&& *p
!= '%') p
++;
224 /* --- Some simple cases --- *
226 * We might have reached the end of the string, or maybe a `%%' escape.
229 if (!*p
) { fs
->fmt
= fmt_unset
; fs
->ch
= 0; break; }
231 if (*p
== '%') { fs
->fmt
= fmt_unset
; fs
->ch
= '%'; p
++; continue; }
233 /* --- Pick up initial flags --- */
238 case '+': f
|= f_plus
; break;
239 case '-': f
|= f_minus
; break;
240 case '#': f
|= f_sharp
; break;
241 case '0': f
|= f_zero
; break;
242 default: goto done_flags
;
247 /* --- Pick up the field width --- */
251 while (ISDIGIT(*p
)) i
= 10*i
+ *p
++ - '0';
253 /* --- Snag: this might have been an argument position indicator --- */
255 if (i
&& *p
== '$' && (!f
|| f
== f_zero
)) {
262 /* --- Set the field width --- *
264 * If @i@ is nonzero here then we have a numeric field width. Otherwise
265 * it might be `*', maybe with an explicit argument number.
271 } else if (*p
== '*') {
277 while (ISDIGIT(*p
)) i
= 10*i
+ *p
++ - '0';
278 assert(*p
== '$'); p
++;
282 set_arg(&av
, i
, fmt_i
); fs
->wd
= i
;
285 /* --- Maybe we have a precision spec --- */
292 while (ISDIGIT(*p
)) i
= 10*i
+ *p
++ - '0';
294 } else if (*p
!= '*')
302 while (ISDIGIT(*p
)) i
= 10*i
+ *p
++ - '0';
303 assert(*p
== '$'); p
++;
307 set_arg(&av
, i
, fmt_i
); fs
->prec
= i
;
311 /* --- Maybe some length flags --- */
316 if (*p
== 'h') { f
|= len_hh
; p
++; } else f
|= len_h
;
320 IF_LONGLONG( if (*p
== 'l') { f
|= len_ll
; p
++; } else ) f
|= len_l
;
322 case 'L': f
|= len_L
; p
++; break;
323 case 'z': f
|= len_z
; p
++; break;
324 case 't': f
|= len_t
; p
++; break;
325 IF_INTMAX( case 'j': f
|= len_j
; p
++; break; )
328 /* --- The flags are now ready --- */
332 /* --- At the end, an actual directive --- */
339 case 'd': case 'i': case 'x': case 'X': case 'o': case 'u':
341 case len_l
: fs
->fmt
= fmt_li
; break;
342 case len_z
: fs
->fmt
= fmt_zi
; break;
343 case len_t
: fs
->fmt
= fmt_ti
; break;
344 IF_LONGLONG( case len_ll
: fs
->fmt
= fmt_lli
; break; )
345 IF_INTMAX( case len_j
: fs
->fmt
= fmt_ji
; break; )
346 default: fs
->fmt
= fmt_i
;
350 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
351 fs
->fmt
= (f
&f_len
) == len_L ? fmt_Lf
: fmt_f
;
364 case len_hh
: fs
->fmt
= fmt_hhn
; break;
365 case len_h
: fs
->fmt
= fmt_hn
; break;
366 case len_l
: fs
->fmt
= fmt_ln
; break;
367 case len_z
: fs
->fmt
= fmt_zn
; break;
368 case len_t
: fs
->fmt
= fmt_tn
; break;
369 IF_LONGLONG( case len_ll
: fs
->fmt
= fmt_lln
; break; )
370 IF_INTMAX( case len_j
: fs
->fmt
= fmt_jn
; break; )
371 default: fs
->fmt
= fmt_n
;
376 "FATAL dstr_vputf: unknown format specifier `%c'\n", p
[-1]);
380 /* --- Finally sort out the argument --- *
382 * If we don't have explicit argument positions then this comes after the
383 * width and precision; and we don't know the type code until we've
384 * parsed the specifier, so this seems the right place to handle it.
387 if (!(f
&f_posarg
)) fs
->arg
= anext
++;
388 set_arg(&av
, fs
->arg
, fs
->fmt
);
391 /* --- Quick pass over the argument vector to collect the arguments --- */
393 for (fa
= DA(&av
), fal
= fa
+ DA_LEN(&av
); fa
< fal
; fa
++) {
395 #define CASE(code, ty) case fmt_##code: fa->u.code = va_arg(*ap, ty); break;
402 /* --- Final pass through the format string to produce output --- */
405 for (fs
= DA(&sv
), fsl
= fs
+ DA_LEN(&sv
); fs
< fsl
; fs
++) {
408 /* --- Output the literal portion --- */
411 if (ops
->putm(out
, fs
->p
, fs
->n
)) return (-1);
415 /* --- And now the variable portion --- */
417 if (fs
->fmt
== fmt_unset
) {
420 case '%': ops
->putch(out
, '%'); break;
429 /* --- Resolve the width and precision --- */
434 wd
= (fs
->f
&f_wdarg
) ?
*(int *)&fa
[fs
->wd
].u
.i
: fs
->wd
;
435 if (wd
< 0) { wd
= -wd
; f
|= f_minus
; }
441 prec
= (fs
->f
&f_precarg
) ?
*(int *)&fa
[fs
->prec
].u
.i
: fs
->prec
;
442 if (prec
< 0) { prec
= 0; f
&= ~f_prec
; }
445 /* --- Write out the flags, width and precision --- */
447 if (f
&f_plus
) DPUTC(&dd
, '+');
448 if (f
&f_minus
) DPUTC(&dd
, '-');
449 if (f
&f_sharp
) DPUTC(&dd
, '#');
450 if (f
&f_zero
) DPUTC(&dd
, '0');
454 dd
.len
+= sprintf(dd
.buf
+ dd
.len
, "%d", wd
);
458 DENSURE(&dd
, STEP
+ 1);
459 dd
.len
+= sprintf(dd
.buf
+ dd
.len
, ".%d", prec
);
462 /* --- Write out the length gadget --- */
465 case len_hh
: DPUTC(&dd
, 'h'); /* fall through */
466 case len_h
: DPUTC(&dd
, 'h'); break;
467 IF_LONGLONG( case len_ll
: DPUTC(&dd
, 'l'); /* fall through */ )
468 case len_l
: DPUTC(&dd
, 'l'); break;
469 case len_z
: DPUTC(&dd
, 'z'); break;
470 case len_t
: DPUTC(&dd
, 't'); break;
471 case len_L
: DPUTC(&dd
, 'L'); break;
472 IF_INTMAX( case len_j
: DPUTC(&dd
, 'j'); break; )
477 /* --- And finally the actually important bit --- */
482 /* --- Make sure we have enough space for the output --- */
485 if (sz
< wd
) sz
= wd
;
486 if (sz
< prec
+ 16) sz
= prec
+ 16;
489 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
492 mx
= ((fs
->f
&f_len
) == len_L ?
493 LDBL_MAX_10_EXP
: DBL_MAX_10_EXP
) + 16;
494 if (sz
< mx
) sz
= mx
;
498 # define MSG "<no float support>"
499 if (ops
->putm(out
, MSG
, sizeof(MSG
) - 1)) return (-1);
505 n
= strlen(fa
[fs
->arg
].u
.s
);
511 #define CASE(code, ty) \
512 case fmt_##code: *fa[fs->arg].u.code = tot; break;
513 PERCENT_N_FMTTYPES(CASE
)
520 /* --- Finally do the output stage --- */
523 #define CASE(code, ty) \
525 i = ops->nputf(out, sz, dd.buf, fa[fs->arg].u.code); \
527 OUTPUT_FMTTYPES(CASE
)
531 if (i
< 0) return (-1);
535 /* --- We're done --- */
543 /* --- @gprintf@ --- *
545 * Arguments: @const struct gprintf_ops *ops@ = output operations
546 * @void *out@ = context for output operations
547 * @const char *p@ = pointer to @printf@-style format string
548 * @...@ = argument handle
550 * Returns: The number of characters written to the string.
552 * Use: Formats a @printf@-like message and writes the result using
553 * the given output operations. This is the backend machinery
554 * for @dstr_putf@, for example.
557 int gprintf(const struct gprintf_ops
*ops
, void *out
, const char *p
, ...)
562 va_start(ap
, p
); n
= vgprintf(ops
, out
, p
, &ap
); va_end(ap
);
566 /*----- Utilities ---------------------------------------------------------*/
568 /* --- @gprintf_memputf@ --- *
570 * Arguments: @char **buf_inout@ = address of output buffer pointer
571 * @size_t *sz_inout@ = address of buffer size
572 * @size_t maxsz@ = buffer size needed for this operation
573 * @const char *p@ = pointer to format string
574 * @va_list *ap@ = captured format-arguments tail
576 * Returns: The formatted length.
578 * Use: Generic utility for mostly implementing the @nputf@ output
579 * function, if you don't have a better option.
581 * On entry, @*buf_inout@ should be null or a buffer pointer,
582 * with @*sz_inout@ either zero or the buffer's size,
583 * respectively. On exit, @*buf_input@ and @*sz_inout@ will be
584 * updated, if necessary, to describe a sufficiently large
585 * buffer, and the formatted string will have been written to
588 * When the buffer is no longer required, free it using @xfree@.
591 size_t gprintf_memputf(char **buf_inout
, size_t *sz_inout
,
592 size_t maxsz
, const char *p
, va_list ap
)
594 char *buf
= *buf_inout
;
595 size_t sz
= *sz_inout
;
600 while (sz
<= maxsz
) sz
*= 2;
602 buf
= xmalloc(sz
); *buf_inout
= buf
; *sz_inout
= sz
;
606 n
= vsnprintf(buf
, maxsz
+ 1, p
, ap
);
608 n
= vsprintf(buf
, p
, ap
);
610 assert(0 <= n
&& n
<= maxsz
);
614 /*----- Standard printers -------------------------------------------------*/
616 static int file_putch(void *out
, int ch
)
620 if (putc(ch
, fp
) == EOF
) return (-1);
624 static int file_putm(void *out
, const char *p
, size_t sz
)
628 if (fwrite(p
, 1, sz
, fp
) < sz
) return (-1);
632 static int file_nputf(void *out
, size_t maxsz
, const char *p
, ...)
639 n
= vfprintf(fp
, p
, ap
);
640 va_end(ap
); if (n
< 0) return (-1);
644 const struct gprintf_ops file_printops
=
645 { file_putch
, file_putm
, file_nputf
};
647 /*----- That's all, folks -------------------------------------------------*/