ucx/cx/printf.h

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

mercurial