1 /* 2 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef _PRINTF_H_ 16 #define _PRINTF_H_ 17 18 #include <stdarg.h> 19 #include <stddef.h> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 26 /** 27 * Tiny printf implementation 28 * You have to implement _putchar if you use printf() 29 * To avoid conflicts with the regular printf() API it is overridden by macro defines 30 * and internal underscore-appended functions like printf_() are used 31 * \param format A string that specifies the format of the output 32 * \return The number of characters that are written into the array, not counting the terminating null character 33 */ 34 #define printf printf_ 35 int printf_(const char* format, ...); 36 37 38 /** 39 * Tiny snprintf/vsnprintf implementation 40 * \param buffer A pointer to the buffer where to store the formatted string 41 * \param count The maximum number of characters to store in the buffer, including a terminating null character 42 * \param format A string that specifies the format of the output 43 * \param va A value identifying a variable arguments list 44 * \return The number of characters that COULD have been written into the buffer, not counting the terminating 45 * null character. A value equal or larger than count indicates truncation. Only when the returned value 46 * is non-negative and less than count, the string has been completely written. 47 */ 48 #define snprintf vsnprintf_ 49 #define vsnprintf vsnprintf_ 50 int snprintf_(char* buffer, size_t count, const char* format, ...); 51 int vsnprintf_(char* buffer, size_t count, const char* format, va_list va); 52 53 54 /** 55 * printf with output function 56 * You may use this as dynamic alternative to printf() with its fixed _putchar() output 57 * \param out An output function which takes one character and an argument pointer 58 * \param arg An argument pointer for user data passed to output function 59 * \param format A string that specifies the format of the output 60 * \return The number of characters that are sent to the output function, not counting the terminating null character 61 */ 62 int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...); 63 64 65 #ifdef __cplusplus 66 } 67 #endif 68 69 70 #endif // _PRINTF_H_