diff options
| author | LLLL Colonq <llll@colonq> | 2026-09-04 16:29:31 -0400 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2026-09-04 16:29:31 -0400 |
| commit | 843ea34efc2c5ea10edc4d534304f8287f97fbbc (patch) | |
| tree | 65c2b676b650413a6154ffa7b319d4d0b6f9cff4 /pit/src/utils.c | |
| parent | c01f847b68a7fcd48eb0f3d456aa993a6f69ea8d (diff) | |
pit: printf
Diffstat (limited to 'pit/src/utils.c')
| -rw-r--r-- | pit/src/utils.c | 25 |
1 files changed, 22 insertions, 3 deletions
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); |
