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