ucx
UAP Common Extensions
Loading...
Searching...
No Matches
printf.h
Go to the documentation of this file.
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 */
36#ifndef UCX_PRINTF_H
37#define UCX_PRINTF_H
38
39#include "common.h"
40#include "string.h"
41#include <stdarg.h>
42
48#define cx_attr_printf(fmt_idx, arg_idx) \
49 __attribute__((__format__(printf, fmt_idx, arg_idx)))
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55
60extern const unsigned cx_printf_sbo_size;
61
77 void *stream,
78 cx_write_func wfc,
79 const char *fmt,
80 ...
81);
82
98 void *stream,
99 cx_write_func wfc,
100 const char *fmt,
101 va_list ap
102);
103
119cx_attr_printf(2, 3)
123 const CxAllocator *allocator,
124 const char *fmt,
125 ...
126);
127
141#define cx_asprintf(fmt, ...) \
142 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
143
162 const CxAllocator *allocator,
163 const char *fmt,
164 va_list ap
165);
166
180#define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
181
192#define cx_bprintf(buffer, fmt, ...) cx_fprintf((void*)buffer, \
193 cxBufferWriteFunc, fmt, __VA_ARGS__)
194
195
209#define cx_sprintf(str, len, fmt, ...) cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__)
210
227cx_attr_nonnull_arg(1, 2, 3, 4)
228cx_attr_printf(4, 5)
232 CxAllocator *alloc,
233 char **str,
234 size_t *len,
235 const char *fmt,
236 ...
237);
238
239
253#define cx_vsprintf(str, len, fmt, ap) cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap)
254
277 CxAllocator *alloc,
278 char **str,
279 size_t *len,
280 const char *fmt,
281 va_list ap
282);
283
284
305#define cx_sprintf_s(buf, len, str, fmt, ...) cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__)
306
328cx_attr_nonnull_arg(1, 2, 4, 5)
329cx_attr_printf(5, 6)
336 CxAllocator *alloc,
337 char *buf,
338 size_t *len,
339 char **str,
340 const char *fmt,
341 ...
342);
343
364#define cx_vsprintf_s(buf, len, str, fmt, ap) cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap)
365
391 CxAllocator *alloc,
392 char *buf,
393 size_t *len,
394 char **str,
395 const char *fmt,
396 va_list ap
397);
398
399
400#ifdef __cplusplus
401} // extern "C"
402#endif
403
404#endif //UCX_PRINTF_H
Common definitions and feature checks.
#define cx_attr_export
Only used for building Windows DLLs.
Definition common.h:285
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
size_t(* cx_write_func)(const void *, size_t, size_t, void *)
Function pointer compatible with fwrite-like functions.
Definition common.h:295
#define cx_attr_access_rw(...)
Specifies that the function will read and write through the given pointer.
Definition common.h:233
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:141
#define cx_attr_cstr_arg(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:206
int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt,...)
An sprintf like function which reallocates the string when the buffer is not large enough.
const unsigned cx_printf_sbo_size
The maximum string length that fits into stack memory.
int cx_vfprintf(void *stream, cx_write_func wfc, const char *fmt, va_list ap)
A vfprintf like function which writes the output to a stream by using a write_func.
cxmutstr cx_asprintf_a(const CxAllocator *allocator, const char *fmt,...)
A asprintf like function which allocates space for a string the result is written to.
cxmutstr cx_vasprintf_a(const CxAllocator *allocator, const char *fmt, va_list ap)
A vasprintf like function which allocates space for a string the result is written to.
int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap)
An sprintf like function which reallocates the string when the buffer is not large enough.
int cx_fprintf(void *stream, cx_write_func wfc, const char *fmt,...)
A fprintf like function which writes the output to a stream by using a write_func.
int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt,...)
An sprintf like function which allocates a new string when the buffer is not large enough.
int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap)
An sprintf like function which allocates a new string when the buffer is not large enough.
#define cx_attr_printf(fmt_idx, arg_idx)
Attribute for printf-like functions.
Definition printf.h:48
Strings that know their length.
Structure holding the data for an allocator.
Definition allocator.h:84
The UCX string structure.
Definition string.h:51