| 43 /** |
43 /** |
| 44 * Attribute for printf-like functions. |
44 * Attribute for printf-like functions. |
| 45 * @param fmt_idx index of the format string parameter |
45 * @param fmt_idx index of the format string parameter |
| 46 * @param arg_idx index of the first formatting argument |
46 * @param arg_idx index of the first formatting argument |
| 47 */ |
47 */ |
| 48 #define cx_attr_printf(fmt_idx, arg_idx) \ |
48 #define CX_PRINTF_ARGS(fmt_idx, arg_idx) \ |
| 49 __attribute__((__format__(printf, fmt_idx, arg_idx))) |
49 __attribute__((__format__(printf, fmt_idx, arg_idx))) |
| 50 |
|
| 51 #ifdef __cplusplus |
|
| 52 extern "C" { |
|
| 53 #endif |
|
| 54 |
|
| 55 |
50 |
| 56 /** |
51 /** |
| 57 * The maximum string length that fits into stack memory. |
52 * The maximum string length that fits into stack memory. |
| 58 */ |
53 */ |
| 59 CX_EXPORT extern const unsigned cx_printf_sbo_size; |
54 CX_EXPORT extern const unsigned cx_printf_sbo_size; |
| 66 * @param wfc the write function |
61 * @param wfc the write function |
| 67 * @param fmt format string |
62 * @param fmt format string |
| 68 * @param ... additional arguments |
63 * @param ... additional arguments |
| 69 * @return the total number of bytes written or an error code from stdlib printf implementation |
64 * @return the total number of bytes written or an error code from stdlib printf implementation |
| 70 */ |
65 */ |
| 71 cx_attr_nonnull_arg(1, 2, 3) cx_attr_printf(3, 4) cx_attr_cstr_arg(3) |
66 CX_EXTERN CX_NONNULL_ARG(1, 2, 3) CX_PRINTF_ARGS(3, 4) CX_CSTR_ARG(3) |
| 72 CX_EXPORT int cx_fprintf(void *stream, cx_write_func wfc, const char *fmt, ...); |
67 int cx_fprintf(void *stream, cx_write_func wfc, const char *fmt, ...); |
| 73 |
68 |
| 74 /** |
69 /** |
| 75 * A @c vfprintf like function which writes the output to a stream by |
70 * A @c vfprintf like function which writes the output to a stream by |
| 76 * using a write_func. |
71 * using a write_func. |
| 77 * |
72 * |
| 80 * @param fmt format string |
75 * @param fmt format string |
| 81 * @param ap argument list |
76 * @param ap argument list |
| 82 * @return the total number of bytes written or an error code from stdlib printf implementation |
77 * @return the total number of bytes written or an error code from stdlib printf implementation |
| 83 * @see cx_fprintf() |
78 * @see cx_fprintf() |
| 84 */ |
79 */ |
| 85 cx_attr_nonnull cx_attr_cstr_arg(3) |
80 CX_EXTERN CX_NONNULL CX_CSTR_ARG(3) |
| 86 CX_EXPORT int cx_vfprintf(void *stream, cx_write_func wfc, const char *fmt, va_list ap); |
81 int cx_vfprintf(void *stream, cx_write_func wfc, const char *fmt, va_list ap); |
| 87 |
82 |
| 88 /** |
83 /** |
| 89 * An @c asprintf like function which allocates space for a string |
84 * An @c asprintf like function which allocates space for a string |
| 90 * the result is written to. |
85 * the result is written to. |
| 91 * |
86 * |
| 97 * @param fmt format string |
92 * @param fmt format string |
| 98 * @param ... additional arguments |
93 * @param ... additional arguments |
| 99 * @return the formatted string |
94 * @return the formatted string |
| 100 * @see cx_strfree_a() |
95 * @see cx_strfree_a() |
| 101 */ |
96 */ |
| 102 cx_attr_nonnull_arg(1, 2) cx_attr_printf(2, 3) cx_attr_cstr_arg(2) |
97 CX_EXTERN CX_NONNULL_ARG(1, 2) CX_PRINTF_ARGS(2, 3) CX_CSTR_ARG(2) |
| 103 CX_EXPORT cxmutstr cx_asprintf_a(const CxAllocator *allocator, const char *fmt, ...); |
98 cxmutstr cx_asprintf_a(const CxAllocator *allocator, const char *fmt, ...); |
| 104 |
99 |
| 105 /** |
100 /** |
| 106 * An @c asprintf like function which allocates space for a string |
101 * An @c asprintf like function which allocates space for a string |
| 107 * the result is written to. |
102 * the result is written to. |
| 108 * |
103 * |
| 129 * @param fmt format string |
124 * @param fmt format string |
| 130 * @param ap argument list |
125 * @param ap argument list |
| 131 * @return the formatted string |
126 * @return the formatted string |
| 132 * @see cx_asprintf_a() |
127 * @see cx_asprintf_a() |
| 133 */ |
128 */ |
| 134 cx_attr_nonnull cx_attr_cstr_arg(2) |
129 CX_EXTERN CX_NONNULL CX_CSTR_ARG(2) |
| 135 CX_EXPORT cxmutstr cx_vasprintf_a(const CxAllocator *allocator, const char *fmt, va_list ap); |
130 cxmutstr cx_vasprintf_a(const CxAllocator *allocator, const char *fmt, va_list ap); |
| 136 |
131 |
| 137 /** |
132 /** |
| 138 * A @c vasprintf like function which allocates space for a string |
133 * A @c vasprintf like function which allocates space for a string |
| 139 * the result is written to. |
134 * the result is written to. |
| 140 * |
135 * |
| 191 * @param len a pointer to the length of the buffer |
186 * @param len a pointer to the length of the buffer |
| 192 * @param fmt the format string |
187 * @param fmt the format string |
| 193 * @param ... additional arguments |
188 * @param ... additional arguments |
| 194 * @return the length of the produced string or an error code from stdlib printf implementation |
189 * @return the length of the produced string or an error code from stdlib printf implementation |
| 195 */ |
190 */ |
| 196 cx_attr_nonnull_arg(1, 2, 3, 4) cx_attr_printf(4, 5) cx_attr_cstr_arg(4) |
191 CX_EXTERN CX_NONNULL_ARG(1, 2, 3, 4) CX_PRINTF_ARGS(4, 5) CX_CSTR_ARG(4) |
| 197 CX_EXPORT int cx_sprintf_a(const CxAllocator *alloc, char **str, size_t *len, const char *fmt, ...); |
192 int cx_sprintf_a(const CxAllocator *alloc, char **str, size_t *len, const char *fmt, ...); |
| 198 |
193 |
| 199 |
194 |
| 200 /** |
195 /** |
| 201 * An @c sprintf like function which reallocates the string when the buffer is not large enough. |
196 * An @c sprintf like function which reallocates the string when the buffer is not large enough. |
| 202 * |
197 * |
| 226 * @param len a pointer to the length of the buffer |
221 * @param len a pointer to the length of the buffer |
| 227 * @param fmt the format string |
222 * @param fmt the format string |
| 228 * @param ap argument list |
223 * @param ap argument list |
| 229 * @return the length of the produced string or an error code from stdlib printf implementation |
224 * @return the length of the produced string or an error code from stdlib printf implementation |
| 230 */ |
225 */ |
| 231 cx_attr_nonnull cx_attr_cstr_arg(4) cx_attr_access_rw(2) cx_attr_access_rw(3) |
226 CX_EXTERN CX_NONNULL CX_CSTR_ARG(4) CX_ACCESS_RW(2) CX_ACCESS_RW(3) |
| 232 CX_EXPORT int cx_vsprintf_a(const CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap); |
227 int cx_vsprintf_a(const CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap); |
| 233 |
228 |
| 234 |
229 |
| 235 /** |
230 /** |
| 236 * An @c sprintf like function which allocates a new string when the buffer is not large enough. |
231 * An @c sprintf like function which allocates a new string when the buffer is not large enough. |
| 237 * |
232 * |
| 273 * @param str a pointer where the location of the result shall be stored |
268 * @param str a pointer where the location of the result shall be stored |
| 274 * @param fmt the format string |
269 * @param fmt the format string |
| 275 * @param ... additional arguments |
270 * @param ... additional arguments |
| 276 * @return the length of the produced string or an error code from stdlib printf implementation |
271 * @return the length of the produced string or an error code from stdlib printf implementation |
| 277 */ |
272 */ |
| 278 cx_attr_nonnull_arg(1, 2, 4, 5) cx_attr_printf(5, 6) cx_attr_cstr_arg(5) |
273 CX_EXTERN CX_NONNULL_ARG(1, 2, 4, 5) CX_PRINTF_ARGS(5, 6) CX_CSTR_ARG(5) |
| 279 cx_attr_access_rw(2) cx_attr_access_rw(3) cx_attr_access_rw(4) |
274 CX_ACCESS_RW(2) CX_ACCESS_RW(3) CX_ACCESS_RW(4) |
| 280 CX_EXPORT int cx_sprintf_sa(const CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ...); |
275 int cx_sprintf_sa(const CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ...); |
| 281 |
276 |
| 282 /** |
277 /** |
| 283 * An @c sprintf like function which allocates a new string when the buffer is not large enough. |
278 * An @c sprintf like function which allocates a new string when the buffer is not large enough. |
| 284 * |
279 * |
| 285 * The size of the buffer will be updated in @p len when necessary. |
280 * The size of the buffer will be updated in @p len when necessary. |
| 320 * @param str a pointer where the location of the result shall be stored |
315 * @param str a pointer where the location of the result shall be stored |
| 321 * @param fmt the format string |
316 * @param fmt the format string |
| 322 * @param ap argument list |
317 * @param ap argument list |
| 323 * @return the length of the produced string or an error code from stdlib printf implementation |
318 * @return the length of the produced string or an error code from stdlib printf implementation |
| 324 */ |
319 */ |
| 325 cx_attr_nonnull cx_attr_cstr_arg(5) |
320 CX_EXTERN CX_NONNULL CX_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); |
321 int cx_vsprintf_sa(const CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap); |
| 327 |
|
| 328 |
|
| 329 #ifdef __cplusplus |
|
| 330 } // extern "C" |
|
| 331 #endif |
|
| 332 |
322 |
| 333 #endif //UCX_PRINTF_H |
323 #endif //UCX_PRINTF_H |