2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
30 * \brief Wrapper for write functions with a printf-like interface.
32 * \author Olaf Wintermann
34 * \copyright 2-Clause BSD License
49 * A \c fprintf like function which writes the output to a stream by
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
58 __attribute__((__nonnull__(1, 2, 3), __format__(printf, 3, 4)))
67 * A \c vfprintf like function which writes the output to a stream by
70 * @param stream the stream the data is written to
71 * @param wfc the write function
72 * @param fmt format string
73 * @param ap argument list
74 * @return the total number of bytes written
77 __attribute__((__nonnull__))
86 * A \c asprintf like function which allocates space for a string
87 * the result is written to.
89 * \note The resulting string is guaranteed to be zero-terminated.
91 * @param allocator the CxAllocator used for allocating the string
92 * @param fmt format string
93 * @param ... additional arguments
94 * @return the formatted string
97 __attribute__((__nonnull__(1, 2), __format__(printf, 2, 3)))
98 cxmutstr cx_asprintf_a(
99 CxAllocator const *allocator,
105 * A \c asprintf like function which allocates space for a string
106 * the result is written to.
108 * \note The resulting string is guaranteed to be zero-terminated.
110 * @param fmt format string
111 * @param ... additional arguments
112 * @return the formatted string
115 #define cx_asprintf(fmt, ...) \
116 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
119 * A \c vasprintf like function which allocates space for a string
120 * the result is written to.
122 * \note The resulting string is guaranteed to be zero-terminated.
124 * @param allocator the CxAllocator used for allocating the string
125 * @param fmt format string
126 * @param ap argument list
127 * @return the formatted string
128 * @see cx_asprintf_a()
130 __attribute__((__nonnull__))
131 cxmutstr cx_vasprintf_a(
132 CxAllocator const *allocator,
138 * A \c vasprintf like function which allocates space for a string
139 * the result is written to.
141 * \note The resulting string is guaranteed to be zero-terminated.
143 * @param fmt format string
144 * @param ap argument list
145 * @return the formatted string
148 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
151 * A \c printf like function which writes the output to a CxBuffer.
153 * @param buffer a pointer to the buffer the data is written to
154 * @param fmt the format string
155 * @param ... additional arguments
156 * @return the total number of bytes written
159 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((CxBuffer*)buffer, \
160 (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__)
166 #endif //UCX_PRINTF_H