Sat, 05 Nov 2022 17:17:17 +0100
#222 add printf-like functions
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * \file printf.h
30 * \brief Wrapper for write functions with a printf-like interface.
31 * \author Mike Becker
32 * \author Olaf Wintermann
33 * \version 3.0
34 * \copyright 2-Clause BSD License
35 */
37 #ifndef UCX_PRINTF_H
38 #define UCX_PRINTF_H
40 #include "common.h"
41 #include "string.h"
42 #include <stdarg.h>
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
48 /**
49 * A \c fprintf like function which writes the output to a stream by
50 * using a write_func.
51 *
52 * @param stream the stream the data is written to
53 * @param wfc the write function
54 * @param fmt format string
55 * @param ... additional arguments
56 * @return the total number of bytes written
57 */
58 int cx_fprintf(void *stream, cx_write_func wfc, char const *fmt, ...);
60 /**
61 * A \c vfprintf like function which writes the output to a stream by
62 * using a write_func.
63 *
64 * @param stream the stream the data is written to
65 * @param wfc the write function
66 * @param fmt format string
67 * @param ap argument list
68 * @return the total number of bytes written
69 * @see cx_fprintf()
70 */
71 int cx_vfprintf(void *stream, cx_write_func wfc, char const *fmt, va_list ap);
73 /**
74 * A \c asprintf like function which allocates space for a string
75 * the result is written to.
76 *
77 * \note The resulting string is guaranteed to be zero-terminated.
78 *
79 * @param allocator the CxAllocator used for allocating the string
80 * @param fmt format string
81 * @param ... additional arguments
82 * @return the formatted string
83 * @see cx_strfree_a()
84 */
85 cxmutstr cx_asprintf_a(CxAllocator *allocator, char const *fmt, ...);
87 /**
88 * A \c asprintf like function which allocates space for a string
89 * the result is written to.
90 *
91 * \note The resulting string is guaranteed to be zero-terminated.
92 *
93 * @param fmt format string
94 * @param ... additional arguments
95 * @return the formatted string
96 * @see cx_strfree()
97 */
98 #define cx_asprintf(fmt, ...) \
99 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
101 /**
102 * A \c vasprintf like function which allocates space for a string
103 * the result is written to.
104 *
105 * \note The resulting string is guaranteed to be zero-terminated.
106 *
107 * @param allocator the CxAllocator used for allocating the string
108 * @param fmt format string
109 * @param ap argument list
110 * @return the formatted string
111 * @see cx_asprintf_a()
112 */
113 cxmutstr cx_vasprintf_a(CxAllocator *allocator, char const *fmt, va_list ap);
115 /**
116 * A \c vasprintf like function which allocates space for a string
117 * the result is written to.
118 *
119 * \note The resulting string is guaranteed to be zero-terminated.
120 *
121 * @param fmt format string
122 * @param ap argument list
123 * @return the formatted string
124 * @see cx_asprintf()
125 */
126 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
128 /**
129 * A \c printf like function which writes the output to a CxBuffer.
130 *
131 * @param buffer the buffer the data is written to
132 * @param fmt the format string
133 * @param ... additional arguments
134 * @return the total number of bytes written
135 * @see ucx_fprintf()
136 */
137 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((CxBuffer*)buffer, \
138 (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__)
140 #ifdef __cplusplus
141 } // extern "C"
142 #endif
144 #endif //UCX_PRINTF_H