Mon, 10 Nov 2025 21:06:55 +0100
add ucx kv_list
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | /** |
| 579 | 29 | * @file printf.h |
| 621 | 30 | * @brief Wrapper for write-functions with a printf-like interface. |
| 579 | 31 | * @author Mike Becker |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #ifndef UCX_PRINTF_H |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #define UCX_PRINTF_H |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #include "common.h" |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | #include "string.h" |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | #include <stdarg.h> |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | |
| 579 | 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 | ||
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | #ifdef __cplusplus |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | extern "C" { |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | #endif |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | |
| 579 | 55 | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | /** |
| 579 | 57 | * The maximum string length that fits into stack memory. |
| 58 | */ | |
| 621 | 59 | CX_EXPORT extern const unsigned cx_printf_sbo_size; |
| 579 | 60 | |
| 61 | /** | |
| 62 | * A @c fprintf like function which writes the output to a stream by | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | * using a write_func. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | * @param stream the stream the data is written to |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | * @param wfc the write function |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | * @param fmt format string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | * @param ... additional arguments |
| 579 | 69 | * @return the total number of bytes written or an error code from stdlib printf implementation |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | */ |
| 621 | 71 | cx_attr_nonnull_arg(1, 2, 3) cx_attr_printf(3, 4) cx_attr_cstr_arg(3) |
| 72 | CX_EXPORT int cx_fprintf(void *stream, cx_write_func wfc, const char *fmt, ...); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | /** |
| 579 | 75 | * A @c vfprintf like function which writes the output to a stream by |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | * using a write_func. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | * @param stream the stream the data is written to |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | * @param wfc the write function |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | * @param fmt format string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | * @param ap argument list |
| 579 | 82 | * @return the total number of bytes written or an error code from stdlib printf implementation |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | * @see cx_fprintf() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | */ |
| 621 | 85 | cx_attr_nonnull cx_attr_cstr_arg(3) |
| 86 | CX_EXPORT int cx_vfprintf(void *stream, cx_write_func wfc, const char *fmt, va_list ap); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | /** |
| 621 | 89 | * An @c asprintf like function which allocates space for a string |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | * the result is written to. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | * |
| 579 | 92 | * @note The resulting string is guaranteed to be zero-terminated, |
| 93 | * unless there was an error, in which case the string's pointer | |
| 94 | * will be @c NULL. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | * @param allocator the CxAllocator used for allocating the string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | * @param fmt format string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | * @param ... additional arguments |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | * @return the formatted string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | * @see cx_strfree_a() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | */ |
| 621 | 102 | cx_attr_nonnull_arg(1, 2) cx_attr_printf(2, 3) cx_attr_cstr_arg(2) |
| 103 | CX_EXPORT cxmutstr cx_asprintf_a(const CxAllocator *allocator, const char *fmt, ...); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | /** |
| 621 | 106 | * An @c asprintf like function which allocates space for a string |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | * the result is written to. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | * |
| 579 | 109 | * @note The resulting string is guaranteed to be zero-terminated, |
| 110 | * unless there was an error, in which case the string's pointer | |
| 111 | * will be @c NULL. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | * |
| 579 | 113 | * @param fmt (@c char*) format string |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | * @param ... additional arguments |
| 579 | 115 | * @return (@c cxmutstr) the formatted string |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | * @see cx_strfree() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | */ |
| 621 | 118 | #define cx_asprintf(fmt, ...) cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__) |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | /** |
| 579 | 121 | * A @c vasprintf like function which allocates space for a string |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | * the result is written to. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | * |
| 579 | 124 | * @note The resulting string is guaranteed to be zero-terminated, |
| 125 | * unless there was an error, in which case the string's pointer | |
| 126 | * will be @c NULL. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127 | * |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | * @param allocator the CxAllocator used for allocating the string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129 | * @param fmt format string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | * @param ap argument list |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | * @return the formatted string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | * @see cx_asprintf_a() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | */ |
| 621 | 134 | cx_attr_nonnull cx_attr_cstr_arg(2) |
| 135 | CX_EXPORT cxmutstr cx_vasprintf_a(const CxAllocator *allocator, const char *fmt, va_list ap); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | /** |
| 579 | 138 | * A @c vasprintf like function which allocates space for a string |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | * the result is written to. |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | * |
| 579 | 141 | * @note The resulting string is guaranteed to be zero-terminated, |
| 621 | 142 | * unless there was an error, in which case the string's pointer |
| 579 | 143 | * will be @c NULL. |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | * |
| 579 | 145 | * @param fmt (@c char*) format string |
| 146 | * @param ap (@c va_list) argument list | |
| 147 | * @return (@c cxmutstr) the formatted string | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | * @see cx_asprintf() |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | */ |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap) |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152 | /** |
| 579 | 153 | * A @c printf like function which writes the output to a CxBuffer. |
| 154 | * | |
| 155 | * @param buffer (@c CxBuffer*) a pointer to the buffer the data is written to | |
| 156 | * @param fmt (@c char*) the format string | |
| 157 | * @param ... additional arguments | |
| 158 | * @return (@c int) the total number of bytes written or an error code from stdlib printf implementation | |
| 159 | * @see cx_fprintf() | |
| 160 | * @see cxBufferWrite() | |
| 161 | */ | |
| 621 | 162 | #define cx_bprintf(buffer, fmt, ...) cx_fprintf((void*)buffer, cxBufferWriteFunc, fmt, __VA_ARGS__) |
| 579 | 163 | |
| 164 | ||
| 165 | /** | |
| 166 | * An @c sprintf like function which reallocates the string when the buffer is not large enough. | |
| 167 | * | |
| 168 | * The size of the buffer will be updated in @p len when necessary. | |
| 169 | * | |
| 170 | * @note The resulting string, if successful, is guaranteed to be zero-terminated. | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | * |
| 579 | 172 | * @param str (@c char**) a pointer to the string buffer |
| 173 | * @param len (@c size_t*) a pointer to the length of the buffer | |
| 174 | * @param fmt (@c char*) the format string | |
| 175 | * @param ... additional arguments | |
| 621 | 176 | * @return (@c int) the length of the produced string or an error code from stdlib printf implementation |
| 579 | 177 | */ |
| 178 | #define cx_sprintf(str, len, fmt, ...) cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__) | |
| 179 | ||
| 180 | /** | |
| 181 | * An @c sprintf like function which reallocates the string when the buffer is not large enough. | |
| 182 | * | |
| 183 | * The size of the buffer will be updated in @p len when necessary. | |
| 184 | * | |
| 185 | * @note The resulting string, if successful, is guaranteed to be zero-terminated. | |
| 186 | * | |
| 187 | * @attention The original buffer MUST have been allocated with the same allocator! | |
| 188 | * | |
| 189 | * @param alloc the allocator to use | |
| 190 | * @param str a pointer to the string buffer | |
| 191 | * @param len a pointer to the length of the buffer | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | * @param fmt the format string |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | * @param ... additional arguments |
| 621 | 194 | * @return the length of the produced string or an error code from stdlib printf implementation |
| 579 | 195 | */ |
| 621 | 196 | cx_attr_nonnull_arg(1, 2, 3, 4) cx_attr_printf(4, 5) cx_attr_cstr_arg(4) |
| 197 | CX_EXPORT int cx_sprintf_a(const CxAllocator *alloc, char **str, size_t *len, const char *fmt, ...); | |
| 579 | 198 | |
| 199 | ||
| 200 | /** | |
| 201 | * An @c sprintf like function which reallocates the string when the buffer is not large enough. | |
| 202 | * | |
| 203 | * The size of the buffer will be updated in @p len when necessary. | |
| 204 | * | |
| 205 | * @note The resulting string, if successful, is guaranteed to be zero-terminated. | |
| 206 | * | |
| 207 | * @param str (@c char**) a pointer to the string buffer | |
| 208 | * @param len (@c size_t*) a pointer to the length of the buffer | |
| 209 | * @param fmt (@c char*) the format string | |
| 210 | * @param ap (@c va_list) argument list | |
| 621 | 211 | * @return (@c int) the length of the produced string or an error code from stdlib printf implementation |
| 579 | 212 | */ |
| 213 | #define cx_vsprintf(str, len, fmt, ap) cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap) | |
| 214 | ||
| 215 | /** | |
| 216 | * An @c sprintf like function which reallocates the string when the buffer is not large enough. | |
| 217 | * | |
| 218 | * The size of the buffer will be updated in @p len when necessary. | |
| 219 | * | |
| 220 | * @note The resulting string is guaranteed to be zero-terminated. | |
| 221 | * | |
| 222 | * @attention The original buffer MUST have been allocated with the same allocator! | |
| 223 | * | |
| 224 | * @param alloc the allocator to use | |
| 225 | * @param str a pointer to the string buffer | |
| 226 | * @param len a pointer to the length of the buffer | |
| 227 | * @param fmt the format string | |
| 228 | * @param ap argument list | |
| 621 | 229 | * @return the length of the produced string or an error code from stdlib printf implementation |
| 579 | 230 | */ |
| 621 | 231 | cx_attr_nonnull cx_attr_cstr_arg(4) cx_attr_access_rw(2) cx_attr_access_rw(3) |
| 232 | CX_EXPORT int cx_vsprintf_a(const CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap); | |
| 579 | 233 | |
| 234 | ||
| 235 | /** | |
| 236 | * An @c sprintf like function which allocates a new string when the buffer is not large enough. | |
| 237 | * | |
| 238 | * The size of the buffer will be updated in @p len when necessary. | |
| 239 | * | |
| 240 | * The location of the resulting string will @em always be stored to @p str. When the buffer | |
| 241 | * was sufficiently large, @p buf itself will be stored to the location of @p str. | |
| 242 | * | |
| 243 | * @note The resulting string, if successful, is guaranteed to be zero-terminated. | |
| 244 | * | |
| 245 | * @remark When a new string needed to be allocated, the contents of @p buf will be | |
| 246 | * poisoned after the call, because this function tries to produce the string in @p buf, first. | |
| 247 | * | |
| 248 | * @param buf (@c char*) a pointer to the buffer | |
| 249 | * @param len (@c size_t*) a pointer to the length of the buffer | |
| 250 | * @param str (@c char**) a pointer where the location of the result shall be stored | |
| 251 | * @param fmt (@c char*) the format string | |
| 252 | * @param ... additional arguments | |
| 621 | 253 | * @return (@c int) the length of the produced string or an error code from stdlib printf implementation |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254 | */ |
| 579 | 255 | #define cx_sprintf_s(buf, len, str, fmt, ...) cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__) |
| 256 | ||
| 257 | /** | |
| 258 | * An @c sprintf like function which allocates a new string when the buffer is not large enough. | |
| 259 | * | |
| 260 | * The size of the buffer will be updated in @p len when necessary. | |
| 261 | * | |
| 262 | * The location of the resulting string will @em always be stored to @p str. When the buffer | |
| 263 | * was sufficiently large, @p buf itself will be stored to the location of @p str. | |
| 264 | * | |
| 265 | * @note The resulting string, if successful, is guaranteed to be zero-terminated. | |
| 266 | * | |
| 267 | * @remark When a new string needed to be allocated, the contents of @p buf will be | |
| 268 | * poisoned after the call, because this function tries to produce the string in @p buf, first. | |
| 269 | * | |
| 270 | * @param alloc the allocator to use | |
| 271 | * @param buf a pointer to the buffer | |
| 272 | * @param len a pointer to the length of the buffer | |
| 273 | * @param str a pointer where the location of the result shall be stored | |
| 274 | * @param fmt the format string | |
| 275 | * @param ... additional arguments | |
| 621 | 276 | * @return the length of the produced string or an error code from stdlib printf implementation |
| 579 | 277 | */ |
| 621 | 278 | cx_attr_nonnull_arg(1, 2, 4, 5) cx_attr_printf(5, 6) cx_attr_cstr_arg(5) |
| 279 | cx_attr_access_rw(2) cx_attr_access_rw(3) cx_attr_access_rw(4) | |
| 280 | CX_EXPORT int cx_sprintf_sa(const CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ...); | |
| 579 | 281 | |
| 282 | /** | |
| 283 | * An @c sprintf like function which allocates a new string when the buffer is not large enough. | |
| 284 | * | |
| 285 | * The size of the buffer will be updated in @p len when necessary. | |
| 286 | * | |
| 287 | * The location of the resulting string will @em always be stored to @p str. When the buffer | |
| 288 | * was sufficiently large, @p buf itself will be stored to the location of @p str. | |
| 289 | * | |
| 290 | * @note The resulting string is guaranteed to be zero-terminated. | |
| 291 | * | |
| 292 | * @remark When a new string needed to be allocated, the contents of @p buf will be | |
| 293 | * poisoned after the call, because this function tries to produce the string in @p buf, first. | |
| 294 | * | |
| 295 | * @param buf (@c char*) a pointer to the buffer | |
| 296 | * @param len (@c size_t*) a pointer to the length of the buffer | |
| 297 | * @param str (@c char**) a pointer where the location of the result shall be stored | |
| 298 | * @param fmt (@c char*) the format string | |
| 299 | * @param ap (@c va_list) argument list | |
| 621 | 300 | * @return (@c int) the length of the produced string or an error code from stdlib printf implementation |
| 579 | 301 | */ |
| 302 | #define cx_vsprintf_s(buf, len, str, fmt, ap) cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap) | |
| 303 | ||
| 304 | /** | |
| 305 | * An @c sprintf like function which allocates a new string when the buffer is not large enough. | |
| 306 | * | |
| 307 | * The size of the buffer will be updated in @p len when necessary. | |
| 308 | * | |
| 309 | * The location of the resulting string will @em always be stored to @p str. When the buffer | |
| 310 | * was sufficiently large, @p buf itself will be stored to the location of @p str. | |
| 311 | * | |
| 312 | * @note The resulting string is guaranteed to be zero-terminated. | |
| 313 | * | |
| 314 | * @remark When a new string needed to be allocated, the contents of @p buf will be | |
| 315 | * poisoned after the call, because this function tries to produce the string in @p buf, first. | |
| 316 | * | |
| 317 | * @param alloc the allocator to use | |
| 318 | * @param buf a pointer to the buffer | |
| 319 | * @param len a pointer to the length of the buffer | |
| 320 | * @param str a pointer where the location of the result shall be stored | |
| 321 | * @param fmt the format string | |
| 322 | * @param ap argument list | |
| 621 | 323 | * @return the length of the produced string or an error code from stdlib printf implementation |
| 579 | 324 | */ |
| 621 | 325 | cx_attr_nonnull cx_attr_cstr_arg(5) |
| 326 | CX_EXPORT int cx_vsprintf_sa(const CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap); | |
| 579 | 327 | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
329 | #ifdef __cplusplus |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
330 | } // extern "C" |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
331 | #endif |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
332 | |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
333 | #endif //UCX_PRINTF_H |