Merge branch 'master' into branch-weiyu
[c11tester.git] / printf.h
1 ///////////////////////////////////////////////////////////////////////////////\r
2 // \author (c) Marco Paland (info@paland.com)\r
3 //             2014-2019, PALANDesign Hannover, Germany\r
4 //\r
5 // \license The MIT License (MIT)\r
6 //\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
8 // of this software and associated documentation files (the "Software"), to deal\r
9 // in the Software without restriction, including without limitation the rights\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
11 // copies of the Software, and to permit persons to whom the Software is\r
12 // furnished to do so, subject to the following conditions:\r
13 //\r
14 // The above copyright notice and this permission notice shall be included in\r
15 // all copies or substantial portions of the Software.\r
16 //\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
23 // THE SOFTWARE.\r
24 //\r
25 // \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on\r
26 //        embedded systems with a very limited resources.\r
27 //        Use this instead of bloated standard/newlib printf.\r
28 //        These routines are thread safe and reentrant.\r
29 //\r
30 ///////////////////////////////////////////////////////////////////////////////\r
31 \r
32 #ifndef _PRINTF_H_\r
33 #define _PRINTF_H_\r
34 \r
35 #include <stdarg.h>\r
36 #include <stddef.h>\r
37 \r
38 \r
39 #ifdef __cplusplus\r
40 extern "C" {\r
41 #endif\r
42 \r
43 \r
44 /**\r
45  * Output a character to a custom device like UART, used by the printf() function\r
46  * This function is declared here only. You have to write your custom implementation somewhere\r
47  * \param character Character to output\r
48  */\r
49 //void _putchar(char character);\r
50 \r
51 \r
52 /**\r
53  * Tiny printf implementation\r
54  * You have to implement _putchar if you use printf()\r
55  * To avoid conflicts with the regular printf() API it is overridden by macro defines\r
56  * and internal underscore-appended functions like printf_() are used\r
57  * \param format A string that specifies the format of the output\r
58  * \return The number of characters that are written into the array, not counting the terminating null character\r
59  */\r
60 int printf_(const char* format, ...);\r
61 \r
62 \r
63 /**\r
64  * Tiny sprintf implementation\r
65  * Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD!\r
66  * \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output!\r
67  * \param format A string that specifies the format of the output\r
68  * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character\r
69  */\r
70 int sprintf_(char* buffer, const char* format, ...);\r
71 \r
72 \r
73 /**\r
74  * Tiny snprintf/vsnprintf implementation\r
75  * \param buffer A pointer to the buffer where to store the formatted string\r
76  * \param count The maximum number of characters to store in the buffer, including a terminating null character\r
77  * \param format A string that specifies the format of the output\r
78  * \param va A value identifying a variable arguments list\r
79  * \return The number of characters that COULD have been written into the buffer, not counting the terminating\r
80  *         null character. A value equal or larger than count indicates truncation. Only when the returned value\r
81  *         is non-negative and less than count, the string has been completely written.\r
82  */\r
83 int  snprintf_(char* buffer, size_t count, const char* format, ...);\r
84 int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);\r
85 \r
86 \r
87 /**\r
88  * Tiny vprintf implementation\r
89  * \param format A string that specifies the format of the output\r
90  * \param va A value identifying a variable arguments list\r
91  * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character\r
92  */\r
93 int vprintf_(const char* format, va_list va);\r
94 \r
95 \r
96 /**\r
97  * printf with output function\r
98  * You may use this as dynamic alternative to printf() with its fixed _putchar() output\r
99  * \param out An output function which takes one character and an argument pointer\r
100  * \param arg An argument pointer for user data passed to output function\r
101  * \param format A string that specifies the format of the output\r
102  * \return The number of characters that are sent to the output function, not counting the terminating null character\r
103  */\r
104 int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);\r
105 \r
106 \r
107 #ifdef __cplusplus\r
108 }\r
109 #endif\r
110 \r
111 \r
112 #endif  // _PRINTF_H_\r