1 .\" This file is part of the Public Domain C Library (PDCLib).
2 .\" Permission is granted to use, modify, and / or redistribute at will.
13 .Nd formatted output conversion by callback
17 .Fn "int _cbprintf" "void *p" "size_t (*cb)(void *p, const char *buf, size_t size)" "const char *fmt" "..."
18 .Fn "int _vcbprintf" "void *p" "size_t (*cb)(void *p, const char *buf, size_t size)" "const char *fmt" "va_list ap"
21 .Fn "int _cbwprintf" "void *p" "size_t (*cb)(void *p, const wchar_t *buf, size_t size)" "const wchar_t *fmt" "..."
22 .Fn "int _vcbwprintf" "void *p" "size_t (*cb)(void *p, const wchar_t *buf, size_t size)" "const wchar_t *fmt" "va_list ap"
25 These functions permit the
27 string formatting functionality to be reused outside the C standard library,
28 without the limitation of using the
30 function for this process, that the whole formatted string must exist in memory
34 These functions shall exhibit the same behaviour and conversion specifiers as
37 function, except they shall perform their output by calling the
39 callback, passing the characters to be output as the
41 parameter, and the count of such characters as
45 The implementation is permitted to invoke
47 with a non-zero number of characters as many times as it deems necessary
48 necessary. That is, the implementation may decide to call it only once it has
49 finished conversion of the entire string, or it may call it multiple times as it
50 incrementally performs a conversion (it would be legal for an implementation to
51 invoke the callback for every character produced).
54 During all invocations, the callback will be passed as
56 the same value as was passed to the function.
59 The callback shall return
61 on success, or any other value on failure
64 The functions shall return the number of characters produced by the conversion
65 on success, or a negative number on failure. If the number of characters
76 .Bd -literal -offset indent
77 static size_t do_output(void *p, const char *buf, size_t size)
79 return fwrite(buf, 1, size, (FILE *) p);
82 int myprintf(const char *fmt, ...)
86 return _vcbprintf(stdout, do_output, ap);
92 This function shall not affect errno, however the callbacks it invokes may
98 Sensible implementations of the ISO C standard library implement an analogous
99 system internally, permitting them to share their implementation of formatting
104 Therefore, implementing a callback based variant is not of substantial
107 These functions permit the reuse of this functionality by applications and
108 libraries (for example, a logging library) without the need to reimplement it,
109 and without the aforementioned memory limitation imposed by
113 This nonstandard extension was first defined by PDCLib.