From 843ea34efc2c5ea10edc4d534304f8287f97fbbc Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Fri, 4 Sep 2026 16:29:31 -0400 Subject: pit: printf --- pit/src/utils.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'pit/src/utils.c') diff --git a/pit/src/utils.c b/pit/src/utils.c index 75d59a6..ac8087c 100644 --- a/pit/src/utils.c +++ b/pit/src/utils.c @@ -11,8 +11,10 @@ enum vsnprintf_mode { #define SCRATCH_LEN 256 #define WRITE(c) { buf[idx++] = c; if (idx >= len - 1) goto done; } #define WRITE_SCRATCH(c) { scratch[sidx++] = c; if (sidx >= SCRATCH_LEN) goto error; } -int pit_libc_string_vsnprintf(char *buf, size_t len, char *format, va_list ap) { - // vsnprintf +int pit_vsnprintf_helper( + char *buf, size_t len, char *format, va_list ap, + pit_printf_handler *handlers, i64 handlers_len, void *data +) { size_t idx = 0; size_t flen = pit_libc_string_strlen(format); size_t fidx = 0; @@ -81,8 +83,21 @@ int pit_libc_string_vsnprintf(char *buf, size_t len, char *format, va_list ap) { mode = VSNPRINTF_MODE_NORMAL; break; } - default: goto error; + default: + if (handlers) { + for (i64 i = 0; i < handlers_len; ++i) { + if (c == handlers[i].specifier) { + i64 res = handlers[i].callback(ap, buf + idx, len - idx, data); + if (res < 0) goto error; + idx += (size_t) res; + mode = VSNPRINTF_MODE_NORMAL; + goto handled; + } + } + } + goto error; } + handled: break; } } @@ -92,6 +107,10 @@ done: error: return -1; } +int pit_libc_string_vsnprintf(char *buf, size_t len, char *format, va_list ap) { + // vsnprintf + return pit_vsnprintf_helper(buf, len, format, ap, NULL, 0, NULL); +} int pit_libc_string_snprintf(char *buf, size_t len, char *format, ...) { va_list ap; va_start(ap, format); -- cgit v1.3.1