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