src/cx/printf.h

changeset 1105
5c16dc362684
parent 985
68754c7de906
child 1112
22dc2163fffd
equal deleted inserted replaced
1104:b03aee2cbb7f 1105:5c16dc362684
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file printf.h 29 * @file printf.h
30 * \brief Wrapper for write functions with a printf-like interface. 30 * @brief Wrapper for write functions with a printf-like interface.
31 * \author Mike Becker 31 * @author Mike Becker
32 * \author Olaf Wintermann 32 * @author Olaf Wintermann
33 * \copyright 2-Clause BSD License 33 * @copyright 2-Clause BSD License
34 */ 34 */
35 35
36 #ifndef UCX_PRINTF_H 36 #ifndef UCX_PRINTF_H
37 #define UCX_PRINTF_H 37 #define UCX_PRINTF_H
38 38
57 * The maximum string length that fits into stack memory. 57 * The maximum string length that fits into stack memory.
58 */ 58 */
59 extern const unsigned cx_printf_sbo_size; 59 extern const unsigned cx_printf_sbo_size;
60 60
61 /** 61 /**
62 * 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
63 * using a write_func. 63 * using a write_func.
64 * 64 *
65 * @param stream the stream the data is written to 65 * @param stream the stream the data is written to
66 * @param wfc the write function 66 * @param wfc the write function
67 * @param fmt format string 67 * @param fmt format string
68 * @param ... additional arguments 68 * @param ... additional arguments
69 * @return the total number of bytes written 69 * @return the total number of bytes written or an error code from stdlib printf implementation
70 */ 70 */
71 cx_attr_nonnull_arg(1, 2, 3) 71 cx_attr_nonnull_arg(1, 2, 3)
72 cx_attr_printf(3, 4) 72 cx_attr_printf(3, 4)
73 cx_attr_cstr_arg(3) 73 cx_attr_cstr_arg(3)
74 int cx_fprintf( 74 int cx_fprintf(
77 const char *fmt, 77 const char *fmt,
78 ... 78 ...
79 ); 79 );
80 80
81 /** 81 /**
82 * 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
83 * using a write_func. 83 * using a write_func.
84 * 84 *
85 * @param stream the stream the data is written to 85 * @param stream the stream the data is written to
86 * @param wfc the write function 86 * @param wfc the write function
87 * @param fmt format string 87 * @param fmt format string
88 * @param ap argument list 88 * @param ap argument list
89 * @return the total number of bytes written 89 * @return the total number of bytes written or an error code from stdlib printf implementation
90 * @see cx_fprintf() 90 * @see cx_fprintf()
91 */ 91 */
92 cx_attr_nonnull 92 cx_attr_nonnull
93 cx_attr_cstr_arg(3) 93 cx_attr_cstr_arg(3)
94 int cx_vfprintf( 94 int cx_vfprintf(
97 const char *fmt, 97 const char *fmt,
98 va_list ap 98 va_list ap
99 ); 99 );
100 100
101 /** 101 /**
102 * A \c asprintf like function which allocates space for a string 102 * A @c asprintf like function which allocates space for a string
103 * the result is written to. 103 * the result is written to.
104 * 104 *
105 * \note The resulting string is guaranteed to be zero-terminated. 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.
106 * 108 *
107 * @param allocator the CxAllocator used for allocating the string 109 * @param allocator the CxAllocator used for allocating the string
108 * @param fmt format string 110 * @param fmt format string
109 * @param ... additional arguments 111 * @param ... additional arguments
110 * @return the formatted string 112 * @return the formatted string
118 const char *fmt, 120 const char *fmt,
119 ... 121 ...
120 ); 122 );
121 123
122 /** 124 /**
123 * A \c asprintf like function which allocates space for a string 125 * A @c asprintf like function which allocates space for a string
124 * the result is written to. 126 * the result is written to.
125 * 127 *
126 * \note The resulting string is guaranteed to be zero-terminated. 128 * @note The resulting string is guaranteed to be zero-terminated,
127 * 129 * unless there was an error, in which case the string's pointer
128 * @param fmt format string 130 * will be @c NULL.
129 * @param ... additional arguments 131 *
130 * @return the formatted string 132 * @param fmt (@c char*) format string
133 * @param ... additional arguments
134 * @return (@c cxmutstr) the formatted string
131 * @see cx_strfree() 135 * @see cx_strfree()
132 */ 136 */
133 #define cx_asprintf(fmt, ...) \ 137 #define cx_asprintf(fmt, ...) \
134 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__) 138 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
135 139
136 /** 140 /**
137 * A \c vasprintf like function which allocates space for a string 141 * A @c vasprintf like function which allocates space for a string
138 * the result is written to. 142 * the result is written to.
139 * 143 *
140 * \note The resulting string is guaranteed to be zero-terminated. 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.
141 * 147 *
142 * @param allocator the CxAllocator used for allocating the string 148 * @param allocator the CxAllocator used for allocating the string
143 * @param fmt format string 149 * @param fmt format string
144 * @param ap argument list 150 * @param ap argument list
145 * @return the formatted string 151 * @return the formatted string
152 const char *fmt, 158 const char *fmt,
153 va_list ap 159 va_list ap
154 ); 160 );
155 161
156 /** 162 /**
157 * A \c vasprintf like function which allocates space for a string 163 * A @c vasprintf like function which allocates space for a string
158 * the result is written to. 164 * the result is written to.
159 * 165 *
160 * \note The resulting string is guaranteed to be zero-terminated. 166 * @note The resulting string is guaranteed to be zero-terminated,
161 * 167 * unless there was in error, in which case the string's pointer
162 * @param fmt format string 168 * will be @c NULL.
163 * @param ap argument list 169 *
164 * @return the formatted string 170 * @param fmt (@c char*) format string
171 * @param ap (@c va_list) argument list
172 * @return (@c cxmutstr) the formatted string
165 * @see cx_asprintf() 173 * @see cx_asprintf()
166 */ 174 */
167 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap) 175 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
168 176
169 /** 177 /**
170 * A \c printf like function which writes the output to a CxBuffer. 178 * A @c printf like function which writes the output to a CxBuffer.
171 * 179 *
172 * @param buffer a pointer to the buffer the data is written to 180 * @param buffer (@c CxBuffer*) a pointer to the buffer the data is written to
173 * @param fmt the format string 181 * @param fmt (@c char*) the format string
174 * @param ... additional arguments 182 * @param ... additional arguments
175 * @return the total number of bytes written 183 * @return (@c int) the total number of bytes written or an error code from stdlib printf implementation
176 * @see ucx_fprintf() 184 * @see cx_fprintf()
177 */ 185 * @see cxBufferWrite()
178 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((CxBuffer*)buffer, \ 186 */
187 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((void*)buffer, \
179 (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__) 188 (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__)
180 189
181 190
182 /** 191 /**
183 * An \c sprintf like function which reallocates the string when the buffer is not large enough. 192 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
184 * 193 *
185 * The size of the buffer will be updated in \p len when necessary. 194 * The size of the buffer will be updated in @p len when necessary.
186 * 195 *
187 * \note The resulting string is guaranteed to be zero-terminated. 196 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
188 * 197 *
189 * @param str a pointer to the string buffer 198 * @param str (@c char**) a pointer to the string buffer
190 * @param len a pointer to the length of the buffer 199 * @param len (@c size_t*) a pointer to the length of the buffer
191 * @param fmt the format string 200 * @param fmt (@c char*) the format string
192 * @param ... additional arguments 201 * @param ... additional arguments
193 * @return the length of produced string 202 * @return (@c int) the length of produced string or an error code from stdlib printf implementation
194 */ 203 */
195 #define cx_sprintf(str, len, fmt, ...) cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__) 204 #define cx_sprintf(str, len, fmt, ...) cx_sprintf_a(cxDefaultAllocator, str, len, fmt, __VA_ARGS__)
196 205
197 /** 206 /**
198 * An \c sprintf like function which reallocates the string when the buffer is not large enough. 207 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
199 * 208 *
200 * The size of the buffer will be updated in \p len when necessary. 209 * The size of the buffer will be updated in @p len when necessary.
201 * 210 *
202 * \note The resulting string is guaranteed to be zero-terminated. 211 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
203 * 212 *
204 * \attention The original buffer MUST have been allocated with the same allocator! 213 * @attention The original buffer MUST have been allocated with the same allocator!
205 * 214 *
206 * @param alloc the allocator to use 215 * @param alloc the allocator to use
207 * @param str a pointer to the string buffer 216 * @param str a pointer to the string buffer
208 * @param len a pointer to the length of the buffer 217 * @param len a pointer to the length of the buffer
209 * @param fmt the format string 218 * @param fmt the format string
210 * @param ... additional arguments 219 * @param ... additional arguments
211 * @return the length of produced string 220 * @return the length of produced string or an error code from stdlib printf implementation
212 */ 221 */
213 cx_attr_nonnull_arg(1, 2, 3, 4) 222 cx_attr_nonnull_arg(1, 2, 3, 4)
214 cx_attr_printf(4, 5) 223 cx_attr_printf(4, 5)
215 cx_attr_cstr_arg(4) 224 cx_attr_cstr_arg(4)
216 int cx_sprintf_a( 225 int cx_sprintf_a(
221 ... 230 ...
222 ); 231 );
223 232
224 233
225 /** 234 /**
226 * An \c sprintf like function which reallocates the string when the buffer is not large enough. 235 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
227 * 236 *
228 * The size of the buffer will be updated in \p len when necessary. 237 * The size of the buffer will be updated in @p len when necessary.
229 * 238 *
230 * \note The resulting string is guaranteed to be zero-terminated. 239 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
231 * 240 *
232 * @param str a pointer to the string buffer 241 * @param str (@c char**) a pointer to the string buffer
233 * @param len a pointer to the length of the buffer 242 * @param len (@c size_t*) a pointer to the length of the buffer
234 * @param fmt the format string 243 * @param fmt (@c char*) the format string
235 * @param ap argument list 244 * @param ap (@c va_list) argument list
236 * @return the length of produced string 245 * @return (@c int) the length of produced string or an error code from stdlib printf implementation
237 */ 246 */
238 #define cx_vsprintf(str, len, fmt, ap) cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap) 247 #define cx_vsprintf(str, len, fmt, ap) cx_vsprintf_a(cxDefaultAllocator, str, len, fmt, ap)
239 248
240 /** 249 /**
241 * An \c sprintf like function which reallocates the string when the buffer is not large enough. 250 * An @c sprintf like function which reallocates the string when the buffer is not large enough.
242 * 251 *
243 * The size of the buffer will be updated in \p len when necessary. 252 * The size of the buffer will be updated in @p len when necessary.
244 * 253 *
245 * \note The resulting string is guaranteed to be zero-terminated. 254 * @note The resulting string is guaranteed to be zero-terminated.
246 * 255 *
247 * \attention The original buffer MUST have been allocated with the same allocator! 256 * @attention The original buffer MUST have been allocated with the same allocator!
248 * 257 *
249 * @param alloc the allocator to use 258 * @param alloc the allocator to use
250 * @param str a pointer to the string buffer 259 * @param str a pointer to the string buffer
251 * @param len a pointer to the length of the buffer 260 * @param len a pointer to the length of the buffer
252 * @param fmt the format string 261 * @param fmt the format string
253 * @param ap argument list 262 * @param ap argument list
254 * @return the length of produced string 263 * @return the length of produced string or an error code from stdlib printf implementation
255 */ 264 */
256 cx_attr_nonnull 265 cx_attr_nonnull
257 cx_attr_cstr_arg(4) 266 cx_attr_cstr_arg(4)
267 cx_attr_access_rw(2)
268 cx_attr_access_rw(3)
258 int cx_vsprintf_a( 269 int cx_vsprintf_a(
259 CxAllocator *alloc, 270 CxAllocator *alloc,
260 char **str, 271 char **str,
261 size_t *len, 272 size_t *len,
262 const char *fmt, 273 const char *fmt,
263 va_list ap 274 va_list ap
264 ); 275 );
265 276
266 277
267 /** 278 /**
268 * An \c sprintf like function which allocates a new string when the buffer is not large enough. 279 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
269 * 280 *
270 * The size of the buffer will be updated in \p len when necessary. 281 * The size of the buffer will be updated in @p len when necessary.
271 * 282 *
272 * The location of the resulting string will \em always be stored to \p str. When the buffer 283 * The location of the resulting string will @em always be stored to @p str. When the buffer
273 * was sufficiently large, \p buf itself will be stored to the location of \p str. 284 * was sufficiently large, @p buf itself will be stored to the location of @p str.
274 * 285 *
275 * \note The resulting string is guaranteed to be zero-terminated. 286 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
276 * 287 *
277 * \remark When a new string needed to be allocated, the contents of \p buf will be 288 * @remark When a new string needed to be allocated, the contents of @p buf will be
278 * poisoned after the call, because this function tries to produce the string in \p buf, first. 289 * poisoned after the call, because this function tries to produce the string in @p buf, first.
279 * 290 *
280 * @param buf a pointer to the buffer 291 * @param buf (@c char*) a pointer to the buffer
281 * @param len a pointer to the length of the buffer 292 * @param len (@c size_t*) a pointer to the length of the buffer
282 * @param str a pointer to the location 293 * @param str (@c char**) a pointer where the location of the result shall be stored
283 * @param fmt the format string 294 * @param fmt (@c char*) the format string
284 * @param ... additional arguments 295 * @param ... additional arguments
285 * @return the length of produced string 296 * @return (@c int) the length of produced string or an error code from stdlib printf implementation
286 */ 297 */
287 #define cx_sprintf_s(buf, len, str, fmt, ...) cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__) 298 #define cx_sprintf_s(buf, len, str, fmt, ...) cx_sprintf_sa(cxDefaultAllocator, buf, len, str, fmt, __VA_ARGS__)
288 299
289 /** 300 /**
290 * An \c sprintf like function which allocates a new string when the buffer is not large enough. 301 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
291 * 302 *
292 * The size of the buffer will be updated in \p len when necessary. 303 * The size of the buffer will be updated in @p len when necessary.
293 * 304 *
294 * The location of the resulting string will \em always be stored to \p str. When the buffer 305 * The location of the resulting string will @em always be stored to @p str. When the buffer
295 * was sufficiently large, \p buf itself will be stored to the location of \p str. 306 * was sufficiently large, @p buf itself will be stored to the location of @p str.
296 * 307 *
297 * \note The resulting string is guaranteed to be zero-terminated. 308 * @note The resulting string, if successful, is guaranteed to be zero-terminated.
298 * 309 *
299 * \remark When a new string needed to be allocated, the contents of \p buf will be 310 * @remark When a new string needed to be allocated, the contents of @p buf will be
300 * poisoned after the call, because this function tries to produce the string in \p buf, first. 311 * poisoned after the call, because this function tries to produce the string in @p buf, first.
301 * 312 *
302 * @param alloc the allocator to use 313 * @param alloc the allocator to use
303 * @param buf a pointer to the buffer 314 * @param buf a pointer to the buffer
304 * @param len a pointer to the length of the buffer 315 * @param len a pointer to the length of the buffer
305 * @param str a pointer to the location 316 * @param str a pointer where the location of the result shall be stored
306 * @param fmt the format string 317 * @param fmt the format string
307 * @param ... additional arguments 318 * @param ... additional arguments
308 * @return the length of produced string 319 * @return the length of produced string or an error code from stdlib printf implementation
309 */ 320 */
310 __attribute__((__nonnull__(1, 2, 4, 5), __format__(printf, 5, 6)))
311 cx_attr_nonnull_arg(1, 2, 4, 5) 321 cx_attr_nonnull_arg(1, 2, 4, 5)
312 cx_attr_printf(5, 6) 322 cx_attr_printf(5, 6)
313 cx_attr_cstr_arg(5) 323 cx_attr_cstr_arg(5)
324 cx_attr_access_rw(2)
325 cx_attr_access_rw(3)
326 cx_attr_access_rw(4)
314 int cx_sprintf_sa( 327 int cx_sprintf_sa(
315 CxAllocator *alloc, 328 CxAllocator *alloc,
316 char *buf, 329 char *buf,
317 size_t *len, 330 size_t *len,
318 char **str, 331 char **str,
319 const char *fmt, 332 const char *fmt,
320 ... 333 ...
321 ); 334 );
322 335
323 /** 336 /**
324 * An \c sprintf like function which allocates a new string when the buffer is not large enough. 337 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
325 * 338 *
326 * The size of the buffer will be updated in \p len when necessary. 339 * The size of the buffer will be updated in @p len when necessary.
327 * 340 *
328 * The location of the resulting string will \em always be stored to \p str. When the buffer 341 * The location of the resulting string will @em always be stored to @p str. When the buffer
329 * was sufficiently large, \p buf itself will be stored to the location of \p str. 342 * was sufficiently large, @p buf itself will be stored to the location of @p str.
330 * 343 *
331 * \note The resulting string is guaranteed to be zero-terminated. 344 * @note The resulting string is guaranteed to be zero-terminated.
332 * 345 *
333 * \remark When a new string needed to be allocated, the contents of \p buf will be 346 * @remark When a new string needed to be allocated, the contents of @p buf will be
334 * poisoned after the call, because this function tries to produce the string in \p buf, first. 347 * poisoned after the call, because this function tries to produce the string in @p buf, first.
335 * 348 *
336 * @param buf a pointer to the buffer 349 * @param buf (@c char*) a pointer to the buffer
337 * @param len a pointer to the length of the buffer 350 * @param len (@c size_t*) a pointer to the length of the buffer
338 * @param str a pointer to the location 351 * @param str (@c char**) a pointer where the location of the result shall be stored
339 * @param fmt the format string 352 * @param fmt (@c char*) the format string
340 * @param ap argument list 353 * @param ap (@c va_list) argument list
341 * @return the length of produced string 354 * @return (@c int) the length of produced string or an error code from stdlib printf implementation
342 */ 355 */
343 #define cx_vsprintf_s(buf, len, str, fmt, ap) cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap) 356 #define cx_vsprintf_s(buf, len, str, fmt, ap) cx_vsprintf_sa(cxDefaultAllocator, buf, len, str, fmt, ap)
344 357
345 /** 358 /**
346 * An \c sprintf like function which allocates a new string when the buffer is not large enough. 359 * An @c sprintf like function which allocates a new string when the buffer is not large enough.
347 * 360 *
348 * The size of the buffer will be updated in \p len when necessary. 361 * The size of the buffer will be updated in @p len when necessary.
349 * 362 *
350 * The location of the resulting string will \em always be stored to \p str. When the buffer 363 * The location of the resulting string will @em always be stored to @p str. When the buffer
351 * was sufficiently large, \p buf itself will be stored to the location of \p str. 364 * was sufficiently large, @p buf itself will be stored to the location of @p str.
352 * 365 *
353 * \note The resulting string is guaranteed to be zero-terminated. 366 * @note The resulting string is guaranteed to be zero-terminated.
354 * 367 *
355 * \remark When a new string needed to be allocated, the contents of \p buf will be 368 * @remark When a new string needed to be allocated, the contents of @p buf will be
356 * poisoned after the call, because this function tries to produce the string in \p buf, first. 369 * poisoned after the call, because this function tries to produce the string in @p buf, first.
357 * 370 *
358 * @param alloc the allocator to use 371 * @param alloc the allocator to use
359 * @param buf a pointer to the buffer 372 * @param buf a pointer to the buffer
360 * @param len a pointer to the length of the buffer 373 * @param len a pointer to the length of the buffer
361 * @param str a pointer to the location 374 * @param str a pointer where the location of the result shall be stored
362 * @param fmt the format string 375 * @param fmt the format string
363 * @param ap argument list 376 * @param ap argument list
364 * @return the length of produced string 377 * @return the length of produced string or an error code from stdlib printf implementation
365 */ 378 */
366 cx_attr_nonnull 379 cx_attr_nonnull
367 cx_attr_cstr_arg(5) 380 cx_attr_cstr_arg(5)
368 int cx_vsprintf_sa( 381 int cx_vsprintf_sa(
369 CxAllocator *alloc, 382 CxAllocator *alloc,

mercurial