| 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 * |
| 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 |
| 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 |
| 97 * @param fmt format string |
108 * @param fmt format string |
| 98 * @param ... additional arguments |
109 * @param ... additional arguments |
| 99 * @return the formatted string |
110 * @return the formatted string |
| 100 * @see cx_strfree_a() |
111 * @see cx_strfree_a() |
| 101 */ |
112 */ |
| 102 __attribute__((__nonnull__(1, 2), __format__(printf, 2, 3))) |
113 cx_attr_nonnull_arg(1, 2) |
| |
114 cx_attr_printf(2, 3) |
| |
115 cx_attr_cstr_arg(2) |
| 103 cxmutstr cx_asprintf_a( |
116 cxmutstr cx_asprintf_a( |
| 104 CxAllocator const *allocator, |
117 const CxAllocator *allocator, |
| 105 char const *fmt, |
118 const char *fmt, |
| 106 ... |
119 ... |
| 107 ); |
120 ); |
| 108 |
121 |
| 109 /** |
122 /** |
| 110 * A \c asprintf like function which allocates space for a string |
123 * A \c asprintf like function which allocates space for a string |
| 130 * @param fmt format string |
143 * @param fmt format string |
| 131 * @param ap argument list |
144 * @param ap argument list |
| 132 * @return the formatted string |
145 * @return the formatted string |
| 133 * @see cx_asprintf_a() |
146 * @see cx_asprintf_a() |
| 134 */ |
147 */ |
| 135 __attribute__((__nonnull__)) |
148 cx_attr_nonnull |
| |
149 cx_attr_cstr_arg(2) |
| 136 cxmutstr cx_vasprintf_a( |
150 cxmutstr cx_vasprintf_a( |
| 137 CxAllocator const *allocator, |
151 const CxAllocator *allocator, |
| 138 char const *fmt, |
152 const char *fmt, |
| 139 va_list ap |
153 va_list ap |
| 140 ); |
154 ); |
| 141 |
155 |
| 142 /** |
156 /** |
| 143 * A \c vasprintf like function which allocates space for a string |
157 * A \c vasprintf like function which allocates space for a string |
| 194 * @param len a pointer to the length of the buffer |
208 * @param len a pointer to the length of the buffer |
| 195 * @param fmt the format string |
209 * @param fmt the format string |
| 196 * @param ... additional arguments |
210 * @param ... additional arguments |
| 197 * @return the length of produced string |
211 * @return the length of produced string |
| 198 */ |
212 */ |
| 199 __attribute__((__nonnull__(1, 2, 3, 4), __format__(printf, 4, 5))) |
213 cx_attr_nonnull_arg(1, 2, 3, 4) |
| 200 int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, ... ); |
214 cx_attr_printf(4, 5) |
| |
215 cx_attr_cstr_arg(4) |
| |
216 int cx_sprintf_a( |
| |
217 CxAllocator *alloc, |
| |
218 char **str, |
| |
219 size_t *len, |
| |
220 const char *fmt, |
| |
221 ... |
| |
222 ); |
| 201 |
223 |
| 202 |
224 |
| 203 /** |
225 /** |
| 204 * An \c sprintf like function which reallocates the string when the buffer is not large enough. |
226 * An \c sprintf like function which reallocates the string when the buffer is not large enough. |
| 205 * |
227 * |
| 229 * @param len a pointer to the length of the buffer |
251 * @param len a pointer to the length of the buffer |
| 230 * @param fmt the format string |
252 * @param fmt the format string |
| 231 * @param ap argument list |
253 * @param ap argument list |
| 232 * @return the length of produced string |
254 * @return the length of produced string |
| 233 */ |
255 */ |
| 234 __attribute__((__nonnull__)) |
256 cx_attr_nonnull |
| 235 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap); |
257 cx_attr_cstr_arg(4) |
| |
258 int cx_vsprintf_a( |
| |
259 CxAllocator *alloc, |
| |
260 char **str, |
| |
261 size_t *len, |
| |
262 const char *fmt, |
| |
263 va_list ap |
| |
264 ); |
| 236 |
265 |
| 237 |
266 |
| 238 /** |
267 /** |
| 239 * An \c sprintf like function which allocates a new string when the buffer is not large enough. |
268 * An \c sprintf like function which allocates a new string when the buffer is not large enough. |
| 240 * |
269 * |
| 277 * @param fmt the format string |
306 * @param fmt the format string |
| 278 * @param ... additional arguments |
307 * @param ... additional arguments |
| 279 * @return the length of produced string |
308 * @return the length of produced string |
| 280 */ |
309 */ |
| 281 __attribute__((__nonnull__(1, 2, 4, 5), __format__(printf, 5, 6))) |
310 __attribute__((__nonnull__(1, 2, 4, 5), __format__(printf, 5, 6))) |
| 282 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... ); |
311 cx_attr_nonnull_arg(1, 2, 4, 5) |
| |
312 cx_attr_printf(5, 6) |
| |
313 cx_attr_cstr_arg(5) |
| |
314 int cx_sprintf_sa( |
| |
315 CxAllocator *alloc, |
| |
316 char *buf, |
| |
317 size_t *len, |
| |
318 char **str, |
| |
319 const char *fmt, |
| |
320 ... |
| |
321 ); |
| 283 |
322 |
| 284 /** |
323 /** |
| 285 * An \c sprintf like function which allocates a new string when the buffer is not large enough. |
324 * An \c sprintf like function which allocates a new string when the buffer is not large enough. |
| 286 * |
325 * |
| 287 * The size of the buffer will be updated in \p len when necessary. |
326 * The size of the buffer will be updated in \p len when necessary. |
| 322 * @param str a pointer to the location |
361 * @param str a pointer to the location |
| 323 * @param fmt the format string |
362 * @param fmt the format string |
| 324 * @param ap argument list |
363 * @param ap argument list |
| 325 * @return the length of produced string |
364 * @return the length of produced string |
| 326 */ |
365 */ |
| 327 __attribute__((__nonnull__)) |
366 cx_attr_nonnull |
| 328 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap); |
367 cx_attr_cstr_arg(5) |
| |
368 int cx_vsprintf_sa( |
| |
369 CxAllocator *alloc, |
| |
370 char *buf, |
| |
371 size_t *len, |
| |
372 char **str, |
| |
373 const char *fmt, |
| |
374 va_list ap |
| |
375 ); |
| 329 |
376 |
| 330 |
377 |
| 331 #ifdef __cplusplus |
378 #ifdef __cplusplus |
| 332 } // extern "C" |
379 } // extern "C" |
| 333 #endif |
380 #endif |