src/cx/printf.h

changeset 985
68754c7de906
parent 926
8fdd8d78c14b
equal deleted inserted replaced
984:e8f354a25ac8 985:68754c7de906
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
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 const char *fmt, 77 const char *fmt,
68 ... 78 ...
77 * @param fmt format string 87 * @param fmt format string
78 * @param ap argument list 88 * @param ap argument list
79 * @return the total number of bytes written 89 * @return the total number of bytes written
80 * @see cx_fprintf() 90 * @see cx_fprintf()
81 */ 91 */
82 __attribute__((__nonnull__)) 92 cx_attr_nonnull
93 cx_attr_cstr_arg(3)
83 int cx_vfprintf( 94 int cx_vfprintf(
84 void *stream, 95 void *stream,
85 cx_write_func wfc, 96 cx_write_func wfc,
86 const char *fmt, 97 const char *fmt,
87 va_list ap 98 va_list ap
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 const CxAllocator *allocator, 117 const CxAllocator *allocator,
105 const char *fmt, 118 const char *fmt,
106 ... 119 ...
107 ); 120 );
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 const CxAllocator *allocator, 151 const CxAllocator *allocator,
138 const char *fmt, 152 const char *fmt,
139 va_list ap 153 va_list ap
140 ); 154 );
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

mercurial