Debian Codegraph @

vendor/printf/README.md

raw
1 # A printf / sprintf Implementation for Embedded Systems
2
3 [![Build Status](https://travis-ci.org/mpaland/printf.svg?branch=master)](https://travis-ci.org/mpaland/printf)
4 [![codecov](https://codecov.io/gh/mpaland/printf/branch/master/graph/badge.svg)](https://codecov.io/gh/mpaland/printf)
5 [![Coverity Status](https://img.shields.io/coverity/scan/14180.svg)](https://scan.coverity.com/projects/mpaland-printf)
6 [![Github Issues](https://img.shields.io/github/issues/mpaland/printf.svg)](http://github.com/mpaland/printf/issues)
7 [![Github Releases](https://img.shields.io/github/release/mpaland/printf.svg)](https://github.com/mpaland/printf/releases)
8 [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/mpaland/avl_array/master/LICENSE)
9
10 This is a tiny but **fully loaded** printf, sprintf and (v)snprintf implementation.
11 Primarily designed for usage in embedded systems, where printf is not available due to memory issues or in avoidance of linking against libc.
12 Using the standard libc printf may pull **a lot** of unwanted library stuff and can bloat code size about 20k or is not 100% thread safe. In this cases the following implementation can be used.
13 Absolutely **NO dependencies** are required, *printf.c* brings all necessary routines, even its own fast `ftoa` (floating point), `ntoa` (decimal) conversion.
14
15 If memory footprint is really a critical issue, floating point, exponential and 'long long' support and can be turned off via the `PRINTF_DISABLE_SUPPORT_FLOAT`, `PRINTF_DISABLE_SUPPORT_EXPONENTIAL` and `PRINTF_DISABLE_SUPPORT_LONG_LONG` compiler switches.
16 When using printf (instead of sprintf/snprintf) you have to provide your own `_putchar()` low level function as console/serial output.
17
18
19 ## 2020 announcement
20 This project is not dead! I just had no time in 2019 for sufficient support, sorry.
21 Within the next weeks, I will have a look to all PRs and open issues.
22 Thank you all for supporting this project.
23
24
25 ## Highlights and Design Goals
26
27 There is a boatload of so called 'tiny' printf implementations around. So why this one?
28 I've tested many implementations, but most of them have very limited flag/specifier support, a lot of other dependencies or are just not standard compliant and failing most of the test suite.
29 Therefore I decided to write an own, final implementation which meets the following items:
30
31 - Very small implementation (around 600 code lines)
32 - NO dependencies, no libs, just one module file
33 - Support of all important flags, width and precision sub-specifiers (see below)
34 - Support of decimal/floating number representation (with an own fast itoa/ftoa)
35 - Reentrant and thread-safe, malloc free, no static vars/buffers
36 - LINT and compiler L4 warning free, mature, coverity clean, automotive ready
37 - Extensive test suite (> 400 test cases) passing
38 - Simply the best *printf* around the net
39 - MIT license
40
41
42 ## Usage
43
44 Add/link *printf.c* to your project and include *printf.h*. That's it.
45 Implement your low level output function needed for `printf()`:
46 ```C
47 void _putchar(char character)
48 {
49 // send char to console etc.
50 }
51 ```
52
53 Usage is 1:1 like the according stdio.h library version:
54 ```C
55 int printf(const char* format, ...);
56 int sprintf(char* buffer, const char* format, ...);
57 int snprintf(char* buffer, size_t count, const char* format, ...);
58 int vsnprintf(char* buffer, size_t count, const char* format, va_list va);
59
60 // use output function (instead of buffer) for streamlike interface
61 int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);
62 ```
63
64 **Due to general security reasons it is highly recommended to prefer and use `snprintf` (with the max buffer size as `count` parameter) instead of `sprintf`.**
65 `sprintf` has no buffer limitation, so when needed - use it really with care!
66
67 ### Streamlike Usage
68 Besides the regular standard `printf()` functions, this module also provides `fctprintf()`, which takes an output function as first parameter to build a streamlike output like `fprintf()`:
69 ```C
70 // define the output function
71 void my_stream_output(char character, void* arg)
72 {
73 // opt. evaluate the argument and send the char somewhere
74 }
75
76 {
77 // in your code
78 void* arg = (void*)100; // this argument is passed to the output function
79 fctprintf(&my_stream_output, arg, "This is a test: %X", 0xAA);
80 fctprintf(&my_stream_output, nullptr, "Send to null dev");
81 }
82 ```
83
84 ## Format Specifiers
85
86 A format specifier follows this prototype: `%[flags][width][.precision][length]type`
87 The following format specifiers are supported:
88
89
90 ### Supported Types
91
92 | Type | Output |
93 |--------|--------|
94 | d or i | Signed decimal integer |
95 | u | Unsigned decimal integer |
96 | b | Unsigned binary |
97 | o | Unsigned octal |
98 | x | Unsigned hexadecimal integer (lowercase) |
99 | X | Unsigned hexadecimal integer (uppercase) |
100 | f or F | Decimal floating point |
101 | e or E | Scientific-notation (exponential) floating point |
102 | g or G | Scientific or decimal floating point |
103 | c | Single character |
104 | s | String of characters |
105 | p | Pointer address |
106 | % | A % followed by another % character will write a single % |
107
108
109 ### Supported Flags
110
111 | Flags | Description |
112 |-------|-------------|
113 | - | Left-justify within the given field width; Right justification is the default. |
114 | + | Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers.<br>By default, only negative numbers are preceded with a - sign. |
115 | (space) | If no sign is going to be written, a blank space is inserted before the value. |
116 | # | Used with o, b, x or X specifiers the value is preceded with 0, 0b, 0x or 0X respectively for values different than zero.<br>Used with f, F it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written. |
117 | 0 | Left-pads the number with zeros (0) instead of spaces when padding is specified (see width sub-specifier). |
118
119
120 ### Supported Width
121
122 | Width | Description |
123 |----------|-------------|
124 | (number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger. |
125 | * | The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
126
127
128 ### Supported Precision
129
130 | Precision | Description |
131 |-----------|-------------|
132 | .number | For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.<br>For f and F specifiers: this is the number of digits to be printed after the decimal point. **By default, this is 6, maximum is 9**.<br>For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.<br>If the period is specified without an explicit value for precision, 0 is assumed. |
133 | .* | The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
134
135
136 ### Supported Length
137
138 The length sub-specifier modifies the length of the data type.
139
140 | Length | d i | u o x X |
141 |--------|------|---------|
142 | (none) | int | unsigned int |
143 | hh | char | unsigned char |
144 | h | short int | unsigned short int |
145 | l | long int | unsigned long int |
146 | ll | long long int | unsigned long long int (if PRINTF_SUPPORT_LONG_LONG is defined) |
147 | j | intmax_t | uintmax_t |
148 | z | size_t | size_t |
149 | t | ptrdiff_t | ptrdiff_t (if PRINTF_SUPPORT_PTRDIFF_T is defined) |
150
151
152 ### Return Value
153
154 Upon successful return, all functions return the number of characters written, _excluding_ the terminating null character used to end the string.
155 Functions `snprintf()` and `vsnprintf()` don't write more than `count` bytes, _including_ the terminating null byte ('\0').
156 Anyway, if the output was truncated due to this limit, the return value is the number of characters that _could_ have been written.
157 Notice that a value equal or larger than `count` indicates a truncation. Only when the returned value is non-negative and less than `count`,
158 the string has been completely written.
159 If any error is encountered, `-1` is returned.
160
161 If `buffer` is set to `NULL` (`nullptr`) nothing is written and just the formatted length is returned.
162 ```C
163 int length = sprintf(NULL, "Hello, world"); // length is set to 12
164 ```
165
166
167 ## Compiler Switches/Defines
168
169 | Name | Default value | Description |
170 |------|---------------|-------------|
171 | PRINTF_INCLUDE_CONFIG_H | undefined | Define this as compiler switch (e.g. `gcc -DPRINTF_INCLUDE_CONFIG_H`) to include a "printf_config.h" definition file |
172 | PRINTF_NTOA_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack |
173 | PRINTF_FTOA_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack |
174 | PRINTF_DEFAULT_FLOAT_PRECISION | 6 | Define the default floating point precision |
175 | PRINTF_MAX_FLOAT | 1e9 | Define the largest suitable value to be printed with %f, before using exponential representation |
176 | PRINTF_DISABLE_SUPPORT_FLOAT | undefined | Define this to disable floating point (%f) support |
177 | PRINTF_DISABLE_SUPPORT_EXPONENTIAL | undefined | Define this to disable exponential floating point (%e) support |
178 | PRINTF_DISABLE_SUPPORT_LONG_LONG | undefined | Define this to disable long long (%ll) support |
179 | PRINTF_DISABLE_SUPPORT_PTRDIFF_T | undefined | Define this to disable ptrdiff_t (%t) support |
180
181
182 ## Caveats
183 None anymore (finally).
184
185
186 ## Test Suite
187 For testing just compile, build and run the test suite located in `test/test_suite.cpp`. This uses the [catch](https://github.com/catchorg/Catch2) framework for unit-tests, which is auto-adding main().
188 Running with the `--wait-for-keypress exit` option waits for the enter key after test end.
189
190
191 ## Projects Using printf
192 - [turnkeyboard](https://github.com/mpaland/turnkeyboard) uses printf as log and generic tty (formatting) output.
193 - printf is part of [embeddedartistry/libc](https://github.com/embeddedartistry/libc), a libc targeted for embedded systems usage.
194 - The [Hatchling Platform]( https://github.com/adrian3git/HatchlingPlatform) uses printf.
195
196 (Just send me a mail/issue/PR to get *your* project listed here)
197
198
199 ## Contributing
200
201 0. Give this project a :star:
202 1. Create an issue and describe your idea
203 2. [Fork it](https://github.com/mpaland/printf/fork)
204 3. Create your feature branch (`git checkout -b my-new-feature`)
205 4. Commit your changes (`git commit -am 'Add some feature'`)
206 5. Publish the branch (`git push origin my-new-feature`)
207 6. Create a new pull request
208 7. Profit! :heavy_check_mark:
209
210
211 ## License
212 printf is written under the [MIT license](http://www.opensource.org/licenses/MIT).