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