Commit | Line | Data |
---|---|---|
e63124bc MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Generalized string formatting | |
4 | * | |
5 | * (c) 2023 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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, | |
25 | * USA. | |
26 | */ | |
27 | ||
28 | #ifndef MLIB_GPRINTF_H | |
29 | #define MLIB_GPRINTF_H | |
30 | ||
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
35 | /*----- Header files ------------------------------------------------------*/ | |
36 | ||
37 | #include <stdarg.h> | |
38 | ||
39 | #ifndef MLIB_MACROS_H | |
40 | # include "macros.h" | |
41 | #endif | |
42 | ||
43 | /*----- Data structures ---------------------------------------------------*/ | |
44 | ||
45 | struct gprintf_ops { | |
46 | int (*putch)(void */*out*/, int /*ch*/); | |
47 | int (*putm)(void */*out*/, const char */*p*/, size_t /*sz*/); | |
31d0247c MW |
48 | PRINTF_LIKE(3, 4) |
49 | int (*nputf)(void */*out*/, size_t /*maxsz*/, const char */*p*/, ...); | |
e63124bc MW |
50 | }; |
51 | ||
52 | extern const struct gprintf_ops file_printops; | |
53 | ||
54 | /*----- Functions provided ------------------------------------------------*/ | |
55 | ||
adec5584 | 56 | /* --- @gprintf@, @vgprintf@ --- * |
e63124bc MW |
57 | * |
58 | * Arguments: @const struct gprintf_ops *ops@ = output operations | |
59 | * @void *out@ = context for output operations | |
60 | * @const char *p@ = pointer to @printf@-style format string | |
adec5584 MW |
61 | * @...@ = format arguments |
62 | * @va_list *ap@ = captured format-arguments tail | |
e63124bc MW |
63 | * |
64 | * Returns: The number of characters written to the string. | |
65 | * | |
66 | * Use: Formats a @printf@-like message and writes the result using | |
67 | * the given output operations. This is the backend machinery | |
68 | * for @dstr_putf@, for example. | |
adec5584 MW |
69 | * |
70 | * The @gprintf@ function receives its format arguments as a | |
71 | * variable-length argument tail; the @vgprintf@ function | |
72 | * receives them as %%\emph{a pointer to}%% a captured argument | |
73 | * tail; it updates @*ap@, leaving it ready to read the next | |
74 | * unused argument. | |
e63124bc MW |
75 | */ |
76 | ||
31d0247c MW |
77 | extern PRINTF_LIKE(3, 4) |
78 | int gprintf(const struct gprintf_ops */*ops*/, void */*out*/, | |
79 | const char */*p*/, ...); | |
adec5584 MW |
80 | extern int vgprintf(const struct gprintf_ops */*ops*/, void */*out*/, |
81 | const char */*p*/, va_list */*ap*/); | |
e63124bc | 82 | |
67b5031e MW |
83 | /* --- @gprintf_memputf@ --- * |
84 | * | |
b1a20bee MW |
85 | * Arguments: @arena *a@ = memory allocation arena |
86 | * @char **buf_inout@ = address of output buffer pointer | |
67b5031e MW |
87 | * @size_t *sz_inout@ = address of buffer size |
88 | * @size_t maxsz@ = buffer size needed for this operation | |
89 | * @const char *p@ = pointer to format string | |
90 | * @va_list *ap@ = captured format-arguments tail | |
91 | * | |
92 | * Returns: The formatted length. | |
93 | * | |
94 | * Use: Generic utility for mostly implementing the @nputf@ output | |
95 | * function, if you don't have a better option. | |
96 | * | |
97 | * On entry, @*buf_inout@ should be null or a buffer pointer, | |
98 | * with @*sz_inout@ either zero or the buffer's size, | |
99 | * respectively. On exit, @*buf_input@ and @*sz_inout@ will be | |
100 | * updated, if necessary, to describe a sufficiently large | |
101 | * buffer, and the formatted string will have been written to | |
102 | * the buffer. | |
103 | * | |
b1a20bee MW |
104 | * When the buffer is no longer required, free it using |
105 | * @x_free@. | |
67b5031e MW |
106 | */ |
107 | ||
b1a20bee MW |
108 | extern size_t gprintf_memputf(arena */*a*/, |
109 | char **/*buf_inout*/, size_t */*sz_inout*/, | |
67b5031e MW |
110 | size_t /*maxsz*/, |
111 | const char */*p*/, va_list /*ap*/); | |
112 | ||
e63124bc MW |
113 | /*----- That's all, folks -------------------------------------------------*/ |
114 | ||
115 | #ifdef __cplusplus | |
116 | } | |
117 | #endif | |
118 | ||
119 | #endif |