diff options
author | Anton Kling <anton@kling.gg> | 2024-12-14 15:33:06 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-12-14 15:33:06 +0100 |
commit | 0495bfb0d64bb2b318b629fd1c821dc8e2c8647d (patch) | |
tree | 960e72aadc18f3ec0899dd69781c9ba55b5ef944 /kernel/libc/stdio | |
parent | 6179308e3f6e4c64013e5db5da9b30a22abca704 (diff) |
sb/printf: Add ksbprintf
Easier to add formatted strings to the string builder without
using intermediate buffers.
Diffstat (limited to 'kernel/libc/stdio')
-rw-r--r-- | kernel/libc/stdio/print.c | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/kernel/libc/stdio/print.c b/kernel/libc/stdio/print.c index 56b55ae..fdfd00f 100644 --- a/kernel/libc/stdio/print.c +++ b/kernel/libc/stdio/print.c @@ -1,5 +1,6 @@ #include <assert.h> #include <kmalloc.h> +#include <lib/sb.h> #include <log.h> #include <math.h> #include <stdio.h> @@ -286,32 +287,36 @@ int vkcprintf(struct print_context *ctx, const char *fmt, va_list ap) { return rc; } -struct bn_context { - char *out; - int size; -}; - -void bn_write(struct print_context *_ctx, const char *s, int l) { - struct bn_context *ctx = (struct bn_context *)_ctx->data; +void sb_write(struct print_context *_ctx, const char *s, int l) { + struct sb *ctx = (struct sb *)_ctx->data; assert(ctx); - size_t k = min(l, ctx->size); - memcpy(ctx->out, s, k); - ctx->out += k; - ctx->size -= k; + sb_append_buffer(ctx, s, l); } -int kbnprintf(char *out, size_t size, const char *format, ...) { +int vksbprintf(struct sb *ctx, const char *format, va_list ap) { struct print_context context; - struct bn_context ctx; - context.data = &ctx; - ctx.out = out; - ctx.size = size; - context.write = bn_write; + context.data = ctx; + context.write = sb_write; + return vkcprintf(&context, format, ap); +} + +int ksbprintf(struct sb *ctx, const char *format, ...) { va_list list; va_start(list, format); - int rc = vkcprintf(&context, format, list); + int rc = vksbprintf(ctx, format, list); + va_end(list); + return rc; +} + +int kbnprintf(char *out, size_t size, const char *format, ...) { + struct sb ctx; + sb_init_buffer(&ctx, out, size); + + va_list list; + va_start(list, format); + int rc = vksbprintf(&ctx, format, list); va_end(list); return rc; } |