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 #ifndef MLIB_GPRINTF_H
29 #define MLIB_GPRINTF_H
35 /*----- Header files ------------------------------------------------------*/
47 /*----- Data structures ---------------------------------------------------*/
50 int (*putch)(void */*out*/, int /*ch*/);
51 int (*putm)(void */*out*/, const char */*p*/, size_t /*sz*/);
53 int (*nputf)(void */*out*/, size_t /*maxsz*/, const char */*p*/, ...);
56 extern const struct gprintf_ops file_printops;
58 /*----- Functions provided ------------------------------------------------*/
60 /* --- @gprintf@, @vgprintf@ --- *
62 * Arguments: @const struct gprintf_ops *ops@ = output operations
63 * @void *out@ = context for output operations
64 * @const char *p@ = pointer to @printf@-style format string
65 * @...@ = format arguments
66 * @va_list *ap@ = captured format-arguments tail
68 * Returns: The number of characters written to the string.
70 * Use: Formats a @printf@-like message and writes the result using
71 * the given output operations. This is the backend machinery
72 * for @dstr_putf@, for example.
74 * The @gprintf@ function receives its format arguments as a
75 * variable-length argument tail; the @vgprintf@ function
76 * receives them as %%\emph{a pointer to}%% a captured argument
77 * tail; it updates @*ap@, leaving it ready to read the next
81 extern PRINTF_LIKE(3, 4)
82 int gprintf(const struct gprintf_ops */*ops*/, void */*out*/,
83 const char */*p*/, ...);
84 extern int vgprintf(const struct gprintf_ops */*ops*/, void */*out*/,
85 const char */*p*/, va_list */*ap*/);
87 /* --- @gprintf_memputf@ --- *
89 * Arguments: @arena *a@ = memory allocation arena
90 * @char **buf_inout@ = address of output buffer pointer
91 * @size_t *sz_inout@ = address of buffer size
92 * @size_t maxsz@ = buffer size needed for this operation
93 * @const char *p@ = pointer to format string
94 * @va_list *ap@ = captured format-arguments tail
96 * Returns: The formatted length.
98 * Use: Generic utility for mostly implementing the @nputf@ output
99 * function, if you don't have a better option.
101 * On entry, @*buf_inout@ should be null or a buffer pointer,
102 * with @*sz_inout@ either zero or the buffer's size,
103 * respectively. On exit, @*buf_input@ and @*sz_inout@ will be
104 * updated, if necessary, to describe a sufficiently large
105 * buffer, and the formatted string will have been written to
108 * When the buffer is no longer required, free it using
112 extern size_t gprintf_memputf(arena */*a*/,
113 char **/*buf_inout*/, size_t */*sz_inout*/,
115 const char */*p*/, va_list /*ap*/);
117 /*----- That's all, folks -------------------------------------------------*/