src/ucx/cx/printf.h

changeset 579
e10457d74fe1
parent 490
d218607f5a7e
equal deleted inserted replaced
578:eb48f716b31c 579:e10457d74fe1
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file printf.h 29 * @file printf.h
30 * \brief Wrapper for write functions with a printf-like interface. 30 * @brief Wrapper for write functions with a printf-like interface.
31 * \author Mike Becker 31 * @author Mike Becker
32 * \author Olaf Wintermann 32 * @author Olaf Wintermann
33 * \version 3.0 33 * @copyright 2-Clause BSD License
34 * \copyright 2-Clause BSD License
35 */ 34 */
36 35
37 #ifndef UCX_PRINTF_H 36 #ifndef UCX_PRINTF_H
38 #define UCX_PRINTF_H 37 #define UCX_PRINTF_H
39 38
40 #include "common.h" 39 #include "common.h"
41 #include "string.h" 40 #include "string.h"
42 #include <stdarg.h> 41 #include <stdarg.h>
43 42
43 /**
44 * Attribute for printf-like functions.
45 * @param fmt_idx index of the format string parameter
46 * @param arg_idx index of the first formatting argument
47 */
48 #define cx_attr_printf(fmt_idx, arg_idx) \
49 __attribute__((__format__(printf, fmt_idx, arg_idx)))
50
44 #ifdef __cplusplus 51 #ifdef __cplusplus
45 extern "C" { 52 extern "C" {
46 #endif 53 #endif
47 54
48 /** 55
49 * A \c fprintf like function which writes the output to a stream by 56 /**
57 * The maximum string length that fits into stack memory.
58 */
59 cx_attr_export
60 extern const unsigned cx_printf_sbo_size;
61
62 /**
63 * A @c fprintf like function which writes the output to a stream by
50 * using a write_func. 64 * using a write_func.
51 * 65 *
52 * @param stream the stream the data is written to 66 * @param stream the stream the data is written to
53 * @param wfc the write function 67 * @param wfc the write function
54 * @param fmt format string 68 * @param fmt format string
55 * @param ... additional arguments 69 * @param ... additional arguments
56 * @return the total number of bytes written 70 * @return the total number of bytes written or an error code from stdlib printf implementation
57 */ 71 */
58 __attribute__((__nonnull__(1, 2, 3), __format__(printf, 3, 4))) 72 cx_attr_nonnull_arg(1, 2, 3)
73 cx_attr_printf(3, 4)
74 cx_attr_cstr_arg(3)
75 cx_attr_export
59 int cx_fprintf( 76 int cx_fprintf(
60 void *stream, 77 void *stream,
61 cx_write_func wfc, 78 cx_write_func wfc,
62 char const *fmt, 79 const char *fmt,
63 ... 80 ...
64 ); 81 );
65 82
66 /** 83 /**
67 * A \c vfprintf like function which writes the output to a stream by 84 * A @c vfprintf like function which writes the output to a stream by
68 * using a write_func. 85 * using a write_func.
69 * 86 *
70 * @param stream the stream the data is written to 87 * @param stream the stream the data is written to
71 * @param wfc the write function 88 * @param wfc the write function
72 * @param fmt format string 89 * @param fmt format string
73 * @param ap argument list 90 * @param ap argument list
74 * @return the total number of bytes written 91 * @return the total number of bytes written or an error code from stdlib printf implementation
75 * @see cx_fprintf() 92 * @see cx_fprintf()
76 */ 93 */
77 __attribute__((__nonnull__)) 94 cx_attr_nonnull
95 cx_attr_cstr_arg(3)
96 cx_attr_export
78 int cx_vfprintf( 97 int cx_vfprintf(
79 void *stream, 98 void *stream,
80 cx_write_func wfc, 99 cx_write_func wfc,
81 char const *fmt, 100 const char *fmt,
82 va_list ap 101 va_list ap
83 ); 102 );
84 103
85 /** 104 /**
86 * A \c asprintf like function which allocates space for a string 105 * A @c asprintf like function which allocates space for a string
87 * the result is written to. 106 * the result is written to.
88 * 107 *
89 * \note The resulting string is guaranteed to be zero-terminated. 108 * @note The resulting string is guaranteed to be zero-terminated,
109 * unless there was an error, in which case the string's pointer
110 * will be @c NULL.
90 * 111 *
91 * @param allocator the CxAllocator used for allocating the string 112 * @param allocator the CxAllocator used for allocating the string
92 * @param fmt format string 113 * @param fmt format string
93 * @param ... additional arguments 114 * @param ... additional arguments
94 * @return the formatted string 115 * @return the formatted string
95 * @see cx_strfree_a() 116 * @see cx_strfree_a()
96 */ 117 */
97 __attribute__((__nonnull__(1, 2), __format__(printf, 2, 3))) 118 cx_attr_nonnull_arg(1, 2)
119 cx_attr_printf(2, 3)
120 cx_attr_cstr_arg(2)
121 cx_attr_export
98 cxmutstr cx_asprintf_a( 122 cxmutstr cx_asprintf_a(
99 CxAllocator const *allocator, 123 const CxAllocator *allocator,
100 char const *fmt, 124 const char *fmt,
101 ... 125 ...
102 ); 126 );
103 127
104 /** 128 /**
105 * A \c asprintf like function which allocates space for a string 129 * A @c asprintf like function which allocates space for a string
106 * the result is written to. 130 * the result is written to.
107 * 131 *
108 * \note The resulting string is guaranteed to be zero-terminated. 132 * @note The resulting string is guaranteed to be zero-terminated,
109 * 133 * unless there was an error, in which case the string's pointer
110 * @param fmt format string 134 * will be @c NULL.
111 * @param ... additional arguments 135 *
112 * @return the formatted string 136 * @param fmt (@c char*) format string
137 * @param ... additional arguments
138 * @return (@c cxmutstr) the formatted string
113 * @see cx_strfree() 139 * @see cx_strfree()
114 */ 140 */
115 #define cx_asprintf(fmt, ...) \ 141 #define cx_asprintf(fmt, ...) \
116 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__) 142 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
117 143
118 /** 144 /**
119 * A \c vasprintf like function which allocates space for a string 145 * A @c vasprintf like function which allocates space for a string
120 * the result is written to. 146 * the result is written to.
121 * 147 *
122 * \note The resulting string is guaranteed to be zero-terminated. 148 * @note The resulting string is guaranteed to be zero-terminated,
149 * unless there was an error, in which case the string's pointer
150 * will be @c NULL.
123 * 151 *
124 * @param allocator the CxAllocator used for allocating the string 152 * @param allocator the CxAllocator used for allocating the string
125 * @param fmt format string 153 * @param fmt format string
126 * @param ap argument list 154 * @param ap argument list
127 * @return the formatted string 155 * @return the formatted string
128 * @see cx_asprintf_a() 156 * @see cx_asprintf_a()
129 */ 157 */
130 __attribute__((__nonnull__)) 158 cx_attr_nonnull
159 cx_attr_cstr_arg(2)
160 cx_attr_export
131 cxmutstr cx_vasprintf_a( 161 cxmutstr cx_vasprintf_a(
132 CxAllocator const *allocator, 162 const CxAllocator *allocator,
133 char const *fmt, 163 const char *fmt,
134 va_list ap 164 va_list ap
135 ); 165 );
136 166
137 /** 167 /**
138 * A \c vasprintf like function which allocates space for a string 168 * A @c vasprintf like function which allocates space for a string
139 * the result is written to. 169 * the result is written to.
140 * 170 *
141 * \note The resulting string is guaranteed to be zero-terminated. 171 * @note The resulting string is guaranteed to be zero-terminated,
142 * 172 * unless there was in error, in which case the string's pointer
143 * @param fmt format string 173 * will be @c NULL.
174 *
175 * @param fmt (@c char*) format string
176 * @param ap (@c va_list) argument list
177 * @return (@c cxmutstr) the formatted string
178 * @see cx_asprintf()
179 */
180 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
181
182 /**
183 * A @c printf like function which writes the output to a CxBuffer.
184 *
185 * @param buffer (@c CxBuffer*) a pointer to the buffer the data is written to
186 * @param fmt (@c char*) the format string
187 * @param ... additional arguments
188 * @return (@c int) the total number of bytes written or an error code from stdlib printf implementation
189 * @see cx_fprintf()
190 * @see cxBufferWrite()
191 */
192 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((void*)buffer, \
193 cxBufferWriteFunc, fmt, __VA_ARGS__)
194
195
196 /**
197 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
198 *
199 * The size of the buffer will be updated in @p len when necessary.
200 *
201 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
202 *
203 * @param str (@c char**) a pointer to the string buffer
204 * @param len (@c size_t*) a pointer to the length of the buffer
205 * @param fmt (@c char*) the format string
206 * @param ... additional arguments
207 * @return (@c int) the length of produced string or an error code from stdlib printf implementation
208 */
209 #define cx_sprintf(str, len, fmt, ...) cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__)
210
211 /**
212 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
213 *
214 * The size of the buffer will be updated in @p len when necessary.
215 *
216 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
217 *
218 * @attention The original buffer MUST have been allocated with the same allocator!
219 *
220 * @param alloc the allocator to use
221 * @param str a pointer to the string buffer
222 * @param len a pointer to the length of the buffer
223 * @param fmt the format string
224 * @param ... additional arguments
225 * @return the length of produced string or an error code from stdlib printf implementation
226 */
227 cx_attr_nonnull_arg(1, 2, 3, 4)
228 cx_attr_printf(4, 5)
229 cx_attr_cstr_arg(4)
230 cx_attr_export
231 int cx_sprintf_a(
232 CxAllocator *alloc,
233 char **str,
234 size_t *len,
235 const char *fmt,
236 ...
237 );
238
239
240 /**
241 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
242 *
243 * The size of the buffer will be updated in @p len when necessary.
244 *
245 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
246 *
247 * @param str (@c char**) a pointer to the string buffer
248 * @param len (@c size_t*) a pointer to the length of the buffer
249 * @param fmt (@c char*) the format string
250 * @param ap (@c va_list) argument list
251 * @return (@c int) the length of produced string or an error code from stdlib printf implementation
252 */
253 #define cx_vsprintf(str, len, fmt, ap) cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap)
254
255 /**
256 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
257 *
258 * The size of the buffer will be updated in @p len when necessary.
259 *
260 * @note The resulting string is guaranteed to be zero-terminated.
261 *
262 * @attention The original buffer MUST have been allocated with the same allocator!
263 *
264 * @param alloc the allocator to use
265 * @param str a pointer to the string buffer
266 * @param len a pointer to the length of the buffer
267 * @param fmt the format string
144 * @param ap argument list 268 * @param ap argument list
145 * @return the formatted string 269 * @return the length of produced string or an error code from stdlib printf implementation
146 * @see cx_asprintf() 270 */
147 */ 271 cx_attr_nonnull
148 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap) 272 cx_attr_cstr_arg(4)
149 273 cx_attr_access_rw(2)
150 /** 274 cx_attr_access_rw(3)
151 * A \c printf like function which writes the output to a CxBuffer. 275 cx_attr_export
152 * 276 int cx_vsprintf_a(
153 * @param buffer a pointer to the buffer the data is written to 277 CxAllocator *alloc,
278 char **str,
279 size_t *len,
280 const char *fmt,
281 va_list ap
282 );
283
284
285 /**
286 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
287 *
288 * The size of the buffer will be updated in @p len when necessary.
289 *
290 * The location of the resulting string will @em always be stored to @p str. When the buffer
291 * was sufficiently large, @p buf itself will be stored to the location of @p str.
292 *
293 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
294 *
295 * @remark When a new string needed to be allocated, the contents of @p buf will be
296 * poisoned after the call, because this function tries to produce the string in @p buf, first.
297 *
298 * @param buf (@c char*) a pointer to the buffer
299 * @param len (@c size_t*) a pointer to the length of the buffer
300 * @param str (@c char**) a pointer where the location of the result shall be stored
301 * @param fmt (@c char*) the format string
302 * @param ... additional arguments
303 * @return (@c int) the length of produced string or an error code from stdlib printf implementation
304 */
305 #define cx_sprintf_s(buf, len, str, fmt, ...) cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__)
306
307 /**
308 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
309 *
310 * The size of the buffer will be updated in @p len when necessary.
311 *
312 * The location of the resulting string will @em always be stored to @p str. When the buffer
313 * was sufficiently large, @p buf itself will be stored to the location of @p str.
314 *
315 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
316 *
317 * @remark When a new string needed to be allocated, the contents of @p buf will be
318 * poisoned after the call, because this function tries to produce the string in @p buf, first.
319 *
320 * @param alloc the allocator to use
321 * @param buf a pointer to the buffer
322 * @param len a pointer to the length of the buffer
323 * @param str a pointer where the location of the result shall be stored
154 * @param fmt the format string 324 * @param fmt the format string
155 * @param ... additional arguments 325 * @param ... additional arguments
156 * @return the total number of bytes written 326 * @return the length of produced string or an error code from stdlib printf implementation
157 * @see ucx_fprintf() 327 */
158 */ 328 cx_attr_nonnull_arg(1, 2, 4, 5)
159 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((CxBuffer*)buffer, \ 329 cx_attr_printf(5, 6)
160 (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__) 330 cx_attr_cstr_arg(5)
331 cx_attr_access_rw(2)
332 cx_attr_access_rw(3)
333 cx_attr_access_rw(4)
334 cx_attr_export
335 int cx_sprintf_sa(
336 CxAllocator *alloc,
337 char *buf,
338 size_t *len,
339 char **str,
340 const char *fmt,
341 ...
342 );
343
344 /**
345 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
346 *
347 * The size of the buffer will be updated in @p len when necessary.
348 *
349 * The location of the resulting string will @em always be stored to @p str. When the buffer
350 * was sufficiently large, @p buf itself will be stored to the location of @p str.
351 *
352 * @note The resulting string is guaranteed to be zero-terminated.
353 *
354 * @remark When a new string needed to be allocated, the contents of @p buf will be
355 * poisoned after the call, because this function tries to produce the string in @p buf, first.
356 *
357 * @param buf (@c char*) a pointer to the buffer
358 * @param len (@c size_t*) a pointer to the length of the buffer
359 * @param str (@c char**) a pointer where the location of the result shall be stored
360 * @param fmt (@c char*) the format string
361 * @param ap (@c va_list) argument list
362 * @return (@c int) the length of produced string or an error code from stdlib printf implementation
363 */
364 #define cx_vsprintf_s(buf, len, str, fmt, ap) cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap)
365
366 /**
367 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
368 *
369 * The size of the buffer will be updated in @p len when necessary.
370 *
371 * The location of the resulting string will @em always be stored to @p str. When the buffer
372 * was sufficiently large, @p buf itself will be stored to the location of @p str.
373 *
374 * @note The resulting string is guaranteed to be zero-terminated.
375 *
376 * @remark When a new string needed to be allocated, the contents of @p buf will be
377 * poisoned after the call, because this function tries to produce the string in @p buf, first.
378 *
379 * @param alloc the allocator to use
380 * @param buf a pointer to the buffer
381 * @param len a pointer to the length of the buffer
382 * @param str a pointer where the location of the result shall be stored
383 * @param fmt the format string
384 * @param ap argument list
385 * @return the length of produced string or an error code from stdlib printf implementation
386 */
387 cx_attr_nonnull
388 cx_attr_cstr_arg(5)
389 cx_attr_export
390 int cx_vsprintf_sa(
391 CxAllocator *alloc,
392 char *buf,
393 size_t *len,
394 char **str,
395 const char *fmt,
396 va_list ap
397 );
398
161 399
162 #ifdef __cplusplus 400 #ifdef __cplusplus
163 } // extern "C" 401 } // extern "C"
164 #endif 402 #endif
165 403

mercurial