3 * Format a string to a buffer
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 ------------------------------------------------------*/
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @buf_vputstrf@ --- *
43 * Arguments: @buf *b@ = pointer to a buffer
44 * @const char *p@ = pointer to @printf@-style format string
45 * @va_list *ap@ = argument handle
47 * Returns: The number of characters written to the string, or @-1@ on
50 * Use: As for @buf_putstrf@, but may be used as a back-end to user-
51 * supplied functions with @printf@-style interfaces.
54 static int putch(void *out, int ch)
55 { buf *b = out; return (buf_putbyte(b, ch) ? -1 : 1); }
57 static int putm(void *out, const char *p, size_t sz)
58 { buf *b = out; return (buf_put(b, p, sz) ? -1 : sz); }
60 static int nputf(void *out, size_t maxsz, const char *p, ...)
67 if (BENSURE(b, maxsz + 1)) return (-1);
69 n = vsnprintf((char *)BCUR(b), maxsz + 1, p, ap);
71 n = vsprintf((char *)BCUR(b), p, ap);
73 assert(0 <= n && n <= maxsz);
74 va_end(ap); b->p += n; return (n);
77 const struct gprintf_ops buf_printops =
78 { putch, putm, nputf };
80 int buf_vputstrf(buf *b, const char *p, va_list *ap)
81 { return (vgprintf(&buf_printops, b, p, ap)); }
83 /* --- @buf_putstrf@ --- *
85 * Arguments: @buf *b@ = pointer to a buffer
86 * @const char *p@ = pointer to @printf@-style format string
87 * @...@ = argument handle
89 * Returns: The number of characters written to the string, or @-1@ on
92 * Use: Format a string to a buffer. The resulting output is not
96 int buf_putstrf(buf *b, const char *p, ...)
101 va_start(ap, p); n = buf_vputstrf(b, p, &ap); va_end(ap);
105 /* --- @buf_{,v}putstrf{8,{16,24,32,64}{,b,l},z}@ --- *
107 * Arguments: @buf *b@ = pointer to a buffer
108 * @const char *p@ = pointer to @printf@-style format string
109 * @va_list *ap@ = argument handle
111 * Returns: The number of characters written to the string, or @-1@ on
114 * Use: As for @buf_putstr@, but using a format string.
117 #define BUF_DEF_VPUTSTRF_(n, W, w) \
118 int buf_vputstrf##w(buf *b, const char *p, va_list *ap) \
123 BUF_ENCLOSE##W(b, mk) nn = buf_vputstrf(b, p, ap); \
124 return (BOK(b) ? nn : -1); \
126 DOUINTCONV(BUF_DEF_VPUTSTRF_)
127 BUF_DOKLUDGESUFFIXES(BUF_DEF_VPUTSTRF_)
128 #undef BUF_DEF_VPUTSTRF_
130 int buf_vputstrfz(buf *b, const char *p, va_list *ap)
134 BUF_ENCLOSEZ(b) nn = buf_vputstrf(b, p, ap);
135 return (BOK(b) ? nn : -1);
138 #define BUF_DEF_PUTSTRF_(n, W, w) \
139 int buf_putstrf##w(buf *b, const char *p, ...) \
144 va_start(ap, p); nn = buf_vputstrf##w(b, p, &ap); va_end(ap); \
147 BUF_DOSUFFIXES(BUF_DEF_PUTSTRF_)
148 #undef BUF_DEF_PUTSTRF_
150 /*----- That's all, folks -------------------------------------------------*/