docs/Writerside/topics/printf.h.md

Sat, 15 Feb 2025 17:43:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 15 Feb 2025 17:43:21 +0100
changeset 1210
2ad0cf0f314b
parent 1172
1a6aa0301226
permissions
-rw-r--r--

complete the printf documentation and fix code formatting

relates to #451

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Formatting
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
3 In the `printf.h` header you can find various useful `printf()`-like functions that can write the formatted output
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
4 directly to an arbitrary stream or [buffer](buffer.h.md), or to memory allocated by an [allocator](allocator.h.md).
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
5
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
6 With the help of these convenience functions, you do not need the libc `snprintf` to print your string to a temporary buffer anymore,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
7 plus you do not need to worry about too small buffer sizes, because the functions will automatically allocate enough memory to contain the entire formatted string.
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
8
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
9 > Although UCX usually uses `size_t` for sizes, the return type of all `printf`-like functions
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
10 > (except for the `cx_asprintf()` family of functions) is `int`, consistent with the stdio `printf` return type.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
11 {style="note"}
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
12
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
13 ## Print to Streams and Buffers
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
14
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
15 ```C
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
16 #include <cx/printf.h>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
17
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
18 int cx_fprintf(void *stream, cx_write_func wfc,
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
19 const char *fmt, ...);
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
20
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
21 int cx_vfprintf(void *stream, cx_write_func wfc,
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
22 const char *fmt, va_list ap);
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
23
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
24 int cx_bprintf(CxBuffer *buf, const char *fmt, ...);
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
25 ```
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
26
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
27 The `cx_fprintf()` function uses the stdio `snprintf()` to prepare a formatted string
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
28 which is then written to the `stream` using the write function `wfc`.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
29 The return value is the number of bytes written or a value less than zero when an error occurred.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
30 If the resulting string is short enough, no additional memory is allocated on the heap.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
31 See the Section about [](#small-buffer-optimization) for more details.
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
32
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
33 The `cx_vfprintf()` function is equivalent to `cx_fprintf()`,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
34 except that instead of being called with a variable number of arguments,
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
35 it is called with an argument list as defined by `<stdarg.h>`.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
36
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
37 The `cx_bprintf()` function is implemented as macro for `cx_fprintf()` with the
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
38 `CxBuffer` as `stream` and the `cxBufferWriteFunc` as write function.
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
39
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
40 ## Print to Freshly Allocated Memory
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
41
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
42 ```C
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
43 #include <cx/printf.h>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
44
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
45 cxmutstr cx_asprintf(const char *fmt, ...);
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
46
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
47 cxmutstr cx_asprintf_a(const CxAllocator *allocator,
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
48 const char *fmt, ...);
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
49
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
50 cxmutstr cx_vasprintf(const char *fmt, va_list ap);
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
51
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
52 cxmutstr cx_vasprintf_a(const CxAllocator *allocator,
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
53 const char *fmt, va_list ap);
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
54 ```
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
55
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
56 The `cx_asprintf()` and `cx_asprintf_a()` functions print the formatted output directly to a freshly allocated
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
57 string which is then returned from the function.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
58 On platforms (or when using allocators) where allocation can fail,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
59 the returned string may be empty and the `ptr` field set to `NULL`.
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
60
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
61 The `cx_vasprintf()` and `cx_vasprintf_a()` functions are equivalent to `cx_asprintf()` and `cx_asprintf_a()`,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
62 except that instead of being called with a variable number of arguments,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
63 they are called with an argument list as defined by `<stdarg.h>`.
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
64
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
65 ## Print to Existing Memory
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
66
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
67 ```C
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
68 #include <cx/printf.h>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
69
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
70 int cx_sprintf(char **str, size_t *len,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
71 const char *fmt, ...);
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
72
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
73 int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
74 const char *fmt, ...);
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
75
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
76 int cx_sprintf_s(char *buf, size_t *len, char **str,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
77 const char *fmt, ...);
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
78
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
79 int cx_sprintf_sa(CxAllocator *alloc,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
80 char *buf, size_t *len, char **str,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
81 const char *fmt, ...);
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
82
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
83 int cx_vsprintf(char **str, size_t *len,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
84 const char *fmt, va_list ap);
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
85
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
86 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
87 const char *fmt, va_list ap);
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
88
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
89 int cx_vsprintf_s(char *buf, size_t *len, char **str,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
90 const char *fmt, ...);
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
91
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
92 int cx_vsprintf_sa(CxAllocator *alloc,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
93 char *buf, size_t *len, char **str,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
94 const char *fmt, va_list ap);
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
95 ```
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
96
1210
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
97 The `cx_sprintf()` and `cx_sprintf_a()` functions take a pointer `str` to a pointer to a pre-allocated buffer,
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
98 as well as a pointer `len` to `*str`'s length.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
99 If the formatted output would not fit into this buffer, it is reallocated
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
100 and the new pointer to the buffer and the new length are written back to the variables pointed to by `str` and `len`.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
101
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
102 The `cx_sprintf_s()` and `cx_sprintf_sa()` functions differ from the previous function in that they take
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
103 the pointer to the pre-allocated buffer in the `buf` argument and not in the `str` argument
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
104 (which in this case is only used for storing the resulting pointer), and _always_ allocate an entirely new buffer
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
105 when the length is insufficient.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
106 This is particularly useful when you want to print the formatted string to a buffer allocated on the stack, but also
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
107 want the option to switch to heap-allocated memory when necessary.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
108
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
109 In other words: when the formatted output fits into the buffer pointed to by `buf`, `buf` will be written to `*str`.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
110 Otherwise, the pointer to the freshly allocated buffer is written to `*str`.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
111
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
112 > When using `cx_sprintf()` or `cx_sprintf_a()` you should always make sure that the string pointed to by `*str`
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
113 > was allocated by a matching allocator.
2ad0cf0f314b complete the printf documentation and fix code formatting
Mike Becker <universe@uap-core.de>
parents: 1172
diff changeset
114 > This restriction does not apply for `cx_sprintf_s()` or `cx_sprintf_sa()` which would allocate a fresh buffer when needed.
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
115
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
116 The `cx_vsprintf()`, `cx_vsprintf_a()`, `cx_vsprintf_s()`, and `cx_vsprintf_sa()` functions are equivalent
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
117 to `cx_sprintf()`, `cx_sprintf_a()`, `cx_sprintf_s()`, and `cx_sprintf_sa()`,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
118 except that instead of being called with a variable number of arguments,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
119 they are called with an argument list as defined by `<stdarg.h>`.
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
120
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
121 ## Small Buffer Optimization
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
122
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
123 All functions that allocate internal temporary memory use small buffer optimization to avoid a heap allocation
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
124 if the expected string length is smaller than `cx_printf_sbo_size`.
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
125 This size cannot be changed at runtime, but modified by defining the `CX_PRINTF_SBO_SIZE` macro when [building](install.md#small-buffer-optimizations) the library.
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
126
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
127 <seealso>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
128 <category ref="apidoc">
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
129 <a href="https://ucx.sourceforge.io/api/printf_8h.html">printf.h</a>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
130 </category>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
131 </seealso>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
132

mercurial