diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-02 20:44:59 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-02 20:44:59 -0600 |
commit | ff88c4cc9bfdc91c3af390ab6a7588f5a8ade40a (patch) | |
tree | ae18e6d4576fa594be94e8278877fbdedfa1d4ba /libfmt/libmisc.c | |
parent | 13b8cafb7e28784f037ecd24876c225ddcf48d76 (diff) | |
parent | 8cc87f8c1f25c9d3fec00561237891650a91b47a (diff) |
Diffstat (limited to 'libfmt/libmisc.c')
-rw-r--r-- | libfmt/libmisc.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/libfmt/libmisc.c b/libfmt/libmisc.c new file mode 100644 index 0000000..4586c30 --- /dev/null +++ b/libfmt/libmisc.c @@ -0,0 +1,59 @@ +/* libfmt/libmisc.c - Integrate pico-fmt with libmisc + * + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <stdarg.h> /* for va_list, va_start(), va_end() */ +#include <stdio.h> /* for vprintf(), putchar() */ +#if LIB_PICO_STDIO +#include <pico/stdio.h> /* for stdio_putchar_raw() */ +#endif + +#include <libmisc/macro.h> /* for LM_UNUSED() */ +#include <libmisc/_intercept.h> /* for __lm_printf() and __lm_light_printf() */ + +#include <libfmt/fmt.h> /* for fmt_vfctprintf() */ + +#if LIB_PICO_STDIO +static void libfmt_light_fct(char character, void *LM_UNUSED(arg)) { + stdio_putchar_raw(character); +} +#else +static void libfmt_libc_fct(char character, void *LM_UNUSED(arg)) { + putchar(character); +} +#endif + +int __lm_printf(const char *format, ...) { + va_list va; + va_start(va, format); +#if LIB_PICO_STDIO + /* pico_stdio has already intercepted vprintf for us, and + * their stdio_buffered_printer() is better than our + * libfmt_libc_fct() because buffering. */ + int ret = vprintf(format, va); +#else + int ret = fmt_vfctprintf(libfmt_libc_fct, NULL, format, va); +#endif + va_end(va); + return ret; +} + +int __lm_light_printf(const char *format, ...) { + va_list va; + va_start(va, format); +#if LIB_PICO_STDIO + /* libfmt_light_fct() and stdio_buffered_printer() both use 68 + * bytes of stack; but the buffer lives on the stack of + * stdio.c:__wrap_vprintf(); so that's where you'll see the + * numbers be different if you're analyzing it. (Also, being + * able to skip the stdio_stack_buffer_flush() call.) */ + int ret = fmt_vfctprintf(libfmt_light_fct, NULL, format, va); + stdio_flush(); +#else + int ret = fmt_vfctprintf(libfmt_libc_fct, NULL, format, va); +#endif + va_end(va); + return ret; +} |