docs/Writerside/topics/printf.h.md

Sun, 09 Feb 2025 22:00:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 09 Feb 2025 22:00:17 +0100
branch
docs/3.1
changeset 1172
1a6aa0301226
parent 1146
151c057faf7c
permissions
-rw-r--r--

basic structure for printf documentation

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
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
9 ## Print to Streams and Buffers
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
10
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
11 ```C
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
12 #include <cx/printf.h>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
13
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
14 int cx_fprintf(void *stream, cx_write_func wfc,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
15 const char *fmt, ...);
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
16
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
17 int cx_vfprintf(void *stream, cx_write_func wfc,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
18 const char *fmt, va_list ap);
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
19
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
20 int cx_bprintf(CxBuffer *buf, const char *fmt, ...);
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
21 ```
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
22
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
23 > TODO: document
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
24 {style="warning"}
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
25
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
26 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
27 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
28 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
29
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
30 ## Print to Freshly Allocated Memory
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
31
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
32 ```C
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
33 #include <cx/printf.h>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
34
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
35 cxmutstr cx_asprintf(const char *fmt, ...);
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
36
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
37 cxmutstr cx_asprintf_a(const CxAllocator *allocator,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
38 const char *fmt, ...);
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 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
41
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
42 cxmutstr cx_vasprintf_a(const CxAllocator *allocator,
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
43 const char *fmt, va_list ap);
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
44 ```
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
45
1172
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
46 > TODO: document
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
47 {style="warning"}
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
48
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
49 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
50 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
51 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
52
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
53 ## Print to Existing Memory
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
54
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
55 ```C
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
56 #include <cx/printf.h>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
57
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
58 cx_sprintf
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
59 cx_sprintf_a
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
60 cx_sprintf_s
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
61 cx_sprintf_sa
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
62 cx_vsprintf
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
63 cx_vsprintf_a
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
64 cx_vsprintf_s
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
65 cx_vsprintf_sa
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
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
68 > TODO: document
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
69 {style="warning"}
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
70
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
71 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
72 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
73 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
74 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
75
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
76 ## Small Buffer Optimization
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
77
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
78 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
79 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
80 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
81
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
82 <seealso>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
83 <category ref="apidoc">
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
84 <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
85 </category>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
86 </seealso>
1a6aa0301226 basic structure for printf documentation
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
87

mercurial