@@@ man wip
[mLib] / struct / dstr-putf.c
CommitLineData
002eaee3 1/* -*-c-*-
2 *
002eaee3 3 * `printf'-style formatting for dynamic strings
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
002eaee3 9 *
10 * This file is part of the mLib utilities library.
11 *
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.
d4efbcd9 16 *
002eaee3 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.
d4efbcd9 21 *
002eaee3 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,
25 * MA 02111-1307, USA.
26 */
27
002eaee3 28/*----- Header files ------------------------------------------------------*/
29
67b5031e
MW
30#include "config.h"
31
002eaee3 32#include <stdarg.h>
e63124bc 33#include <stddef.h>
002eaee3 34#include <stdio.h>
5d45633f 35
002eaee3 36#include "dstr.h"
e63124bc 37#include "gprintf.h"
002eaee3 38
39/*----- Main code ---------------------------------------------------------*/
40
41/* --- @dstr_vputf@ --- *
42 *
43 * Arguments: @dstr *d@ = pointer to a dynamic string block
44 * @const char *p@ = pointer to @printf@-style format string
5a18a126 45 * @va_list *ap@ = argument handle
002eaee3 46 *
47 * Returns: The number of characters written to the string.
48 *
49 * Use: As for @dstr_putf@, but may be used as a back-end to user-
50 * supplied functions with @printf@-style interfaces.
51 */
52
e63124bc 53static int putch(void *out, int ch)
adec5584 54 { dstr *d = out; DPUTC(d, ch); return (1); }
e63124bc 55static int putm(void *out, const char *p, size_t sz)
adec5584 56 { dstr *d = out; DPUTM(d, p, sz); return (sz); }
eff136f6 57
e63124bc 58static int nputf(void *out, size_t maxsz, const char *p, ...)
002eaee3 59{
e63124bc
MW
60 dstr *d = out;
61 va_list ap;
62 int n;
eff136f6 63
e63124bc
MW
64 va_start(ap, p);
65 DENSURE(d, maxsz + 1);
eff136f6 66#ifdef HAVE_SNPRINTF
e63124bc 67 n = vsnprintf(d->buf + d->len, maxsz + 1, p, ap);
eff136f6 68#else
e63124bc 69 n = vsprintf(d->buf + d->len, p, ap);
eff136f6 70#endif
e63124bc
MW
71 assert(0 <= n && n <= maxsz);
72 va_end(ap); d->len += n; return (n);
73}
002eaee3 74
e63124bc
MW
75const struct gprintf_ops dstr_printops =
76 { putch, putm, nputf };
eff136f6 77
e63124bc
MW
78int dstr_vputf(dstr *d, const char *p, va_list *ap)
79 { int n = vgprintf(&dstr_printops, d, p, ap); DPUTZ(d); return (n); }
002eaee3 80
81/* --- @dstr_putf@ --- *
82 *
83 * Arguments: @dstr *d@ = pointer to a dynamic string block
84 * @const char *p@ = pointer to @printf@-style format string
85 * @...@ = argument handle
86 *
87 * Returns: The number of characters written to the string.
88 *
89 * Use: Writes a piece of text to a dynamic string, doing @printf@-
90 * style substitutions as it goes. Intended to be robust if
91 * faced with malicious arguments, but not if the format string
92 * itself is malicious.
93 */
94
95int dstr_putf(dstr *d, const char *p, ...)
96{
97 int n;
98 va_list ap;
99 va_start(ap, p);
5a18a126 100 n = dstr_vputf(d, p, &ap);
002eaee3 101 va_end(ap);
102 return (n);
103}
104
105/*----- That's all, folks -------------------------------------------------*/