src/cx/printf.h

changeset 1426
3a89b31f0724
parent 1424
563033aa998c
equal deleted inserted replaced
1425:83284b289430 1426:3a89b31f0724
54 54
55 55
56 /** 56 /**
57 * The maximum string length that fits into stack memory. 57 * The maximum string length that fits into stack memory.
58 */ 58 */
59 cx_attr_export 59 CX_EXPORT extern const unsigned cx_printf_sbo_size;
60 extern const unsigned cx_printf_sbo_size;
61 60
62 /** 61 /**
63 * 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
64 * using a write_func. 63 * using a write_func.
65 * 64 *
67 * @param wfc the write function 66 * @param wfc the write function
68 * @param fmt format string 67 * @param fmt format string
69 * @param ... additional arguments 68 * @param ... additional arguments
70 * @return the total number of bytes written or an error code from stdlib printf implementation 69 * @return the total number of bytes written or an error code from stdlib printf implementation
71 */ 70 */
72 cx_attr_nonnull_arg(1, 2, 3) 71 cx_attr_nonnull_arg(1, 2, 3) cx_attr_printf(3, 4) cx_attr_cstr_arg(3)
73 cx_attr_printf(3, 4) 72 CX_EXPORT int cx_fprintf(void *stream, cx_write_func wfc, const char *fmt, ...);
74 cx_attr_cstr_arg(3)
75 cx_attr_export
76 int cx_fprintf(
77 void *stream,
78 cx_write_func wfc,
79 const char *fmt,
80 ...
81 );
82 73
83 /** 74 /**
84 * A @c vfprintf like function which writes the output to a stream by 75 * A @c vfprintf like function which writes the output to a stream by
85 * using a write_func. 76 * using a write_func.
86 * 77 *
89 * @param fmt format string 80 * @param fmt format string
90 * @param ap argument list 81 * @param ap argument list
91 * @return the total number of bytes written or an error code from stdlib printf implementation 82 * @return the total number of bytes written or an error code from stdlib printf implementation
92 * @see cx_fprintf() 83 * @see cx_fprintf()
93 */ 84 */
94 cx_attr_nonnull 85 cx_attr_nonnull cx_attr_cstr_arg(3)
95 cx_attr_cstr_arg(3) 86 CX_EXPORT int cx_vfprintf(void *stream, cx_write_func wfc, const char *fmt, va_list ap);
96 cx_attr_export
97 int cx_vfprintf(
98 void *stream,
99 cx_write_func wfc,
100 const char *fmt,
101 va_list ap
102 );
103 87
104 /** 88 /**
105 * An @c asprintf like function which allocates space for a string 89 * An @c asprintf like function which allocates space for a string
106 * the result is written to. 90 * the result is written to.
107 * 91 *
113 * @param fmt format string 97 * @param fmt format string
114 * @param ... additional arguments 98 * @param ... additional arguments
115 * @return the formatted string 99 * @return the formatted string
116 * @see cx_strfree_a() 100 * @see cx_strfree_a()
117 */ 101 */
118 cx_attr_nonnull_arg(1, 2) 102 cx_attr_nonnull_arg(1, 2) cx_attr_printf(2, 3) cx_attr_cstr_arg(2)
119 cx_attr_printf(2, 3) 103 CX_EXPORT cxmutstr cx_asprintf_a(const CxAllocator *allocator, const char *fmt, ...);
120 cx_attr_cstr_arg(2)
121 cx_attr_export
122 cxmutstr cx_asprintf_a(
123 const CxAllocator *allocator,
124 const char *fmt,
125 ...
126 );
127 104
128 /** 105 /**
129 * An @c asprintf like function which allocates space for a string 106 * An @c asprintf like function which allocates space for a string
130 * the result is written to. 107 * the result is written to.
131 * 108 *
136 * @param fmt (@c char*) format string 113 * @param fmt (@c char*) format string
137 * @param ... additional arguments 114 * @param ... additional arguments
138 * @return (@c cxmutstr) the formatted string 115 * @return (@c cxmutstr) the formatted string
139 * @see cx_strfree() 116 * @see cx_strfree()
140 */ 117 */
141 #define cx_asprintf(fmt, ...) \ 118 #define cx_asprintf(fmt, ...) cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
142 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
143 119
144 /** 120 /**
145 * A @c vasprintf like function which allocates space for a string 121 * A @c vasprintf like function which allocates space for a string
146 * the result is written to. 122 * the result is written to.
147 * 123 *
153 * @param fmt format string 129 * @param fmt format string
154 * @param ap argument list 130 * @param ap argument list
155 * @return the formatted string 131 * @return the formatted string
156 * @see cx_asprintf_a() 132 * @see cx_asprintf_a()
157 */ 133 */
158 cx_attr_nonnull 134 cx_attr_nonnull cx_attr_cstr_arg(2)
159 cx_attr_cstr_arg(2) 135 CX_EXPORT cxmutstr cx_vasprintf_a(const CxAllocator *allocator, const char *fmt, va_list ap);
160 cx_attr_export
161 cxmutstr cx_vasprintf_a(
162 const CxAllocator *allocator,
163 const char *fmt,
164 va_list ap
165 );
166 136
167 /** 137 /**
168 * A @c vasprintf like function which allocates space for a string 138 * A @c vasprintf like function which allocates space for a string
169 * the result is written to. 139 * the result is written to.
170 * 140 *
187 * @param ... additional arguments 157 * @param ... additional arguments
188 * @return (@c int) the total number of bytes written or an error code from stdlib printf implementation 158 * @return (@c int) the total number of bytes written or an error code from stdlib printf implementation
189 * @see cx_fprintf() 159 * @see cx_fprintf()
190 * @see cxBufferWrite() 160 * @see cxBufferWrite()
191 */ 161 */
192 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((void*)buffer, \ 162 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((void*)buffer, cxBufferWriteFunc, fmt, __VA_ARGS__)
193 cxBufferWriteFunc, fmt, __VA_ARGS__)
194 163
195 164
196 /** 165 /**
197 * An @c sprintf like function which reallocates the string when the buffer is not large enough. 166 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
198 * 167 *
222 * @param len a pointer to the length of the buffer 191 * @param len a pointer to the length of the buffer
223 * @param fmt the format string 192 * @param fmt the format string
224 * @param ... additional arguments 193 * @param ... additional arguments
225 * @return the length of the produced string or an error code from stdlib printf implementation 194 * @return the length of the produced string or an error code from stdlib printf implementation
226 */ 195 */
227 cx_attr_nonnull_arg(1, 2, 3, 4) 196 cx_attr_nonnull_arg(1, 2, 3, 4) cx_attr_printf(4, 5) cx_attr_cstr_arg(4)
228 cx_attr_printf(4, 5) 197 CX_EXPORT int cx_sprintf_a(const CxAllocator *alloc, char **str, size_t *len, const char *fmt, ...);
229 cx_attr_cstr_arg(4)
230 cx_attr_export
231 int cx_sprintf_a(
232 const CxAllocator *alloc,
233 char **str,
234 size_t *len,
235 const char *fmt,
236 ...
237 );
238 198
239 199
240 /** 200 /**
241 * An @c sprintf like function which reallocates the string when the buffer is not large enough. 201 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
242 * 202 *
266 * @param len a pointer to the length of the buffer 226 * @param len a pointer to the length of the buffer
267 * @param fmt the format string 227 * @param fmt the format string
268 * @param ap argument list 228 * @param ap argument list
269 * @return the length of the produced string or an error code from stdlib printf implementation 229 * @return the length of the produced string or an error code from stdlib printf implementation
270 */ 230 */
271 cx_attr_nonnull 231 cx_attr_nonnull cx_attr_cstr_arg(4) cx_attr_access_rw(2) cx_attr_access_rw(3)
272 cx_attr_cstr_arg(4) 232 CX_EXPORT int cx_vsprintf_a(const CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap);
273 cx_attr_access_rw(2)
274 cx_attr_access_rw(3)
275 cx_attr_export
276 int cx_vsprintf_a(
277 const CxAllocator *alloc,
278 char **str,
279 size_t *len,
280 const char *fmt,
281 va_list ap
282 );
283 233
284 234
285 /** 235 /**
286 * An @c sprintf like function which allocates a new string when the buffer is not large enough. 236 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
287 * 237 *
323 * @param str a pointer where the location of the result shall be stored 273 * @param str a pointer where the location of the result shall be stored
324 * @param fmt the format string 274 * @param fmt the format string
325 * @param ... additional arguments 275 * @param ... additional arguments
326 * @return the length of the produced string or an error code from stdlib printf implementation 276 * @return the length of the produced string or an error code from stdlib printf implementation
327 */ 277 */
328 cx_attr_nonnull_arg(1, 2, 4, 5) 278 cx_attr_nonnull_arg(1, 2, 4, 5) cx_attr_printf(5, 6) cx_attr_cstr_arg(5)
329 cx_attr_printf(5, 6) 279 cx_attr_access_rw(2) cx_attr_access_rw(3) cx_attr_access_rw(4)
330 cx_attr_cstr_arg(5) 280 CX_EXPORT int cx_sprintf_sa(const CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ...);
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(
336 const CxAllocator *alloc,
337 char *buf,
338 size_t *len,
339 char **str,
340 const char *fmt,
341 ...
342 );
343 281
344 /** 282 /**
345 * An @c sprintf like function which allocates a new string when the buffer is not large enough. 283 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
346 * 284 *
347 * The size of the buffer will be updated in @p len when necessary. 285 * The size of the buffer will be updated in @p len when necessary.
382 * @param str a pointer where the location of the result shall be stored 320 * @param str a pointer where the location of the result shall be stored
383 * @param fmt the format string 321 * @param fmt the format string
384 * @param ap argument list 322 * @param ap argument list
385 * @return the length of the produced string or an error code from stdlib printf implementation 323 * @return the length of the produced string or an error code from stdlib printf implementation
386 */ 324 */
387 cx_attr_nonnull 325 cx_attr_nonnull cx_attr_cstr_arg(5)
388 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);
389 cx_attr_export
390 int cx_vsprintf_sa(
391 const CxAllocator *alloc,
392 char *buf,
393 size_t *len,
394 char **str,
395 const char *fmt,
396 va_list ap
397 );
398 327
399 328
400 #ifdef __cplusplus 329 #ifdef __cplusplus
401 } // extern "C" 330 } // extern "C"
402 #endif 331 #endif

mercurial