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 ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @buf_vputstrf@ --- *
42 * Arguments: @buf *b@ = pointer to a buffer
43 * @const char *p@ = pointer to @printf@-style format string
44 * @va_list *ap@ = argument handle
46 * Returns: The number of characters written to the string, or @-1@ on
49 * Use: As for @buf_putstrf@, but may be used as a back-end to user-
50 * supplied functions with @printf@-style interfaces.
53 static int putch(void *out, int ch)
54 { buf *b = out; return (buf_putbyte(b, ch) ? -1 : 1); }
56 static int putm(void *out, const char *p, size_t sz)
57 { buf *b = out; return (buf_put(b, p, sz) ? -1 : sz); }
59 static int nputf(void *out, size_t maxsz, const char *p, ...)
66 if (BENSURE(b, maxsz + 1)) return (-1);
68 n = vsnprintf((char *)BCUR(b), maxsz + 1, p, ap);
70 n = vsprintf((char *)BCUR(b), p, ap);
72 assert(0 <= n && n <= maxsz);
73 va_end(ap); b->p += n; return (n);
76 const struct gprintf_ops buf_printops =
77 { putch, putm, nputf };
79 int buf_vputstrf(buf *b, const char *p, va_list *ap)
80 { return (vgprintf(&buf_printops, b, p, ap)); }
82 /* --- @buf_putstrf@ --- *
84 * Arguments: @buf *b@ = pointer to a buffer
85 * @const char *p@ = pointer to @printf@-style format string
86 * @...@ = argument handle
88 * Returns: The number of characters written to the string, or @-1@ on
91 * Use: Format a string to a buffer. The resulting output is not
95 int buf_putstrf(buf *b, const char *p, ...)
100 va_start(ap, p); n = buf_vputstrf(b, p, &ap); va_end(ap);
104 /* --- @buf_{,v}putstrf{8,{16,24,32,64}{,b,l},z}@ --- *
106 * Arguments: @buf *b@ = pointer to a buffer
107 * @const char *p@ = pointer to @printf@-style format string
108 * @va_list *ap@ = argument handle
110 * Returns: The number of characters written to the string, or @-1@ on
113 * Use: As for @buf_putstr@, but using a format string.
116 #define BUF_DEF_VPUTSTRF_(n, W, w) \
117 int buf_vputstrf##w(buf *b, const char *p, va_list *ap) \
122 BUF_ENCLOSE##W(b, mk) nn = buf_vputstrf(b, p, ap); \
123 return (BOK(b) ? nn : -1); \
125 DOUINTCONV(BUF_DEF_VPUTSTRF_)
126 BUF_DOKLUDGESUFFIXES(BUF_DEF_VPUTSTRF_)
127 #undef BUF_DEF_VPUTSTRF_
129 int buf_vputstrfz(buf *b, const char *p, va_list *ap)
133 BUF_ENCLOSEZ(b) nn = buf_vputstrf(b, p, ap);
134 return (BOK(b) ? nn : -1);
137 #define BUF_DEF_PUTSTRF_(n, W, w) \
138 int buf_putstrf##w(buf *b, const char *p, ...) \
143 va_start(ap, p); nn = buf_vputstrf##w(b, p, &ap); va_end(ap); \
146 BUF_DOSUFFIXES(BUF_DEF_PUTSTRF_)
147 #undef BUF_DEF_PUTSTRF_
149 /*----- That's all, folks -------------------------------------------------*/