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