|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef ZEPHYR_INCLUDE_SYS_CBPRINTF_H_ |
| 8 | +#define ZEPHYR_INCLUDE_SYS_CBPRINTF_H_ |
| 9 | + |
| 10 | +#include <stdarg.h> |
| 11 | +#include <stddef.h> |
| 12 | +#include <toolchain.h> |
| 13 | + |
| 14 | +#ifdef __cplusplus |
| 15 | +extern "C" { |
| 16 | +#endif |
| 17 | + |
| 18 | +/** |
| 19 | + * @defgroup cbprintf_apis Formatted Output APIs |
| 20 | + * @ingroup support_apis |
| 21 | + * @{ |
| 22 | + */ |
| 23 | + |
| 24 | +/** @brief Signature for a cbprintf callback function. |
| 25 | + * |
| 26 | + * This function expects two parameters: |
| 27 | + * |
| 28 | + * * @p c a character to output. The output behavior should be as if |
| 29 | + * this was cast to an unsigned char. |
| 30 | + * * @p ctx a pointer to an object that provides context for the |
| 31 | + * output operation. |
| 32 | + * |
| 33 | + * The declaration does not specify the parameter types. This allows a |
| 34 | + * function like @c fputc to be used without requiring all context pointers to |
| 35 | + * be to a @c FILE object. |
| 36 | + * |
| 37 | + * @return the value of @p c cast to an unsigned char then back to |
| 38 | + * int, or a negative error code that will be returned from |
| 39 | + * cbprintf(). |
| 40 | + */ |
| 41 | +typedef int (*cbprintf_cb)(/* int c, void *ctx */); |
| 42 | + |
| 43 | +/** @brief *printf-like output through a callback. |
| 44 | + * |
| 45 | + * This is essentially printf() except the output is generated |
| 46 | + * character-by-character using the provided @p out function. This allows |
| 47 | + * formatting text of unbounded length without incurring the cost of a |
| 48 | + * temporary buffer. |
| 49 | + * |
| 50 | + * All formatting specifiers of C99 are recognized, and most are supported if |
| 51 | + * the functionality is enabled. |
| 52 | + * |
| 53 | + * @note The functionality of this function is significantly reduced |
| 54 | + * when `CONFIG_CBPRINTF_NANO` is selected. |
| 55 | + * |
| 56 | + * @param out the function used to emit each generated character. |
| 57 | + * |
| 58 | + * @param ctx context provided when invoking out |
| 59 | + * |
| 60 | + * @param format a standard ISO C format string with characters and conversion |
| 61 | + * specifications. |
| 62 | + * |
| 63 | + * @param ... arguments corresponding to the conversion specifications found |
| 64 | + * within @p format. |
| 65 | + * |
| 66 | + * @return the number of characters printed, or a negative error value |
| 67 | + * returned from invoking @p out. |
| 68 | + */ |
| 69 | +__printf_like(3, 4) |
| 70 | +int cbprintf(cbprintf_cb out, void *ctx, const char *format, ...); |
| 71 | + |
| 72 | +/** @brief Calculate the number of words required for arguments to a cbprintf |
| 73 | + * format specification. |
| 74 | + * |
| 75 | + * This can be used in cases where the arguments must be copied off the stack |
| 76 | + * into separate storage for processing the conversion in another context. |
| 77 | + * |
| 78 | + * @note The length returned does not count bytes. It counts native words |
| 79 | + * defined as the size of an `int`. |
| 80 | + * |
| 81 | + * @note If `CONFIG_CBPRINTF_NANO` is selected the count will be incorrect if |
| 82 | + * any passed arguments require more than one `int`. |
| 83 | + * |
| 84 | + * @param format a standard ISO C format string with characters and conversion |
| 85 | + * specifications. |
| 86 | + * |
| 87 | + * @return the number of `int` elements required to provide all arguments |
| 88 | + * required for the conversion. |
| 89 | + */ |
| 90 | +size_t cbprintf_arglen(const char *format); |
| 91 | + |
| 92 | +/** @brief varargs-aware *printf-like output through a callback. |
| 93 | + * |
| 94 | + * This is essentially vsprintf() except the output is generated |
| 95 | + * character-by-character using the provided @p out function. This allows |
| 96 | + * formatting text of unbounded length without incurring the cost of a |
| 97 | + * temporary buffer. |
| 98 | + * |
| 99 | + * @note This function is available only when `CONFIG_CBPRINTF_LIBC_SUBSTS` is |
| 100 | + * selected. |
| 101 | + * |
| 102 | + * @note The functionality of this function is significantly reduced when |
| 103 | + * `CONFIG_CBPRINTF_NANO` is selected. |
| 104 | + * |
| 105 | + * @param out the function used to emit each generated character. |
| 106 | + * |
| 107 | + * @param ctx context provided when invoking out |
| 108 | + * |
| 109 | + * @param format a standard ISO C format string with characters and conversion |
| 110 | + * specifications. |
| 111 | + * |
| 112 | + * @param ap a reference to the values to be converted. |
| 113 | + * |
| 114 | + * @return the number of characters generated, or a negative error value |
| 115 | + * returned from invoking @p out. |
| 116 | + */ |
| 117 | +int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap); |
| 118 | + |
| 119 | +/** @brief snprintf using Zephyrs cbprintf infrastructure. |
| 120 | + * |
| 121 | + * @note The functionality of this function is significantly reduced |
| 122 | + * when `CONFIG_CBPRINTF_NANO` is selected. |
| 123 | + * |
| 124 | + * @param str where the formatted content should be written |
| 125 | + * |
| 126 | + * @param size maximum number of chaacters for the formatted output, |
| 127 | + * including the terminating null byte. |
| 128 | + * |
| 129 | + * @param format a standard ISO C format string with characters and |
| 130 | + * conversion specifications. |
| 131 | + * |
| 132 | + * @param ... arguments corresponding to the conversion specifications found |
| 133 | + * within @p format. |
| 134 | + * |
| 135 | + * return The number of characters that would have been written to @p |
| 136 | + * str, excluding the terminating null byte. This is greater than the |
| 137 | + * number actually written if @p size is too small. |
| 138 | + */ |
| 139 | +__printf_like(3, 4) |
| 140 | +int snprintfcb(char *str, size_t size, const char *format, ...); |
| 141 | + |
| 142 | +/** @brief vsnprintf using Zephyrs cbprintf infrastructure. |
| 143 | + * |
| 144 | + * @note This function is available only when `CONFIG_CBPRINTF_LIBC_SUBSTS` is |
| 145 | + * selected. |
| 146 | + * |
| 147 | + * @note The functionality of this function is significantly reduced when |
| 148 | + * `CONFIG_CBPRINTF_NANO` is selected. |
| 149 | + * |
| 150 | + * @param str where the formatted content should be written |
| 151 | + * |
| 152 | + * @param size maximum number of chaacters for the formatted output, including |
| 153 | + * the terminating null byte. |
| 154 | + * |
| 155 | + * @param format a standard ISO C format string with characters and conversion |
| 156 | + * specifications. |
| 157 | + * |
| 158 | + * @param ... arguments corresponding to the conversion specifications found |
| 159 | + * within @p format. |
| 160 | + * |
| 161 | + * @param ap a reference to the values to be converted. |
| 162 | + * |
| 163 | + * return The number of characters that would have been written to @p |
| 164 | + * str, excluding the terminating null byte. This is greater than the |
| 165 | + * number actually written if @p size is too small. |
| 166 | + */ |
| 167 | +int vsnprintfcb(char *str, size_t size, const char *format, va_list ap); |
| 168 | + |
| 169 | +/** |
| 170 | + * @} |
| 171 | + */ |
| 172 | + |
| 173 | +#ifdef __cplusplus |
| 174 | +} |
| 175 | +#endif |
| 176 | + |
| 177 | +#endif /* ZEPHYR_INCLUDE_SYS_CBPRINTF_H_ */ |
0 commit comments