summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-12-14 15:33:06 +0100
committerAnton Kling <anton@kling.gg>2024-12-14 15:33:06 +0100
commit0495bfb0d64bb2b318b629fd1c821dc8e2c8647d (patch)
tree960e72aadc18f3ec0899dd69781c9ba55b5ef944
parent6179308e3f6e4c64013e5db5da9b30a22abca704 (diff)
sb/printf: Add ksbprintf
Easier to add formatted strings to the string builder without using intermediate buffers.
-rw-r--r--kernel/lib/sv.h1
-rw-r--r--kernel/libc/include/stdio.h2
-rw-r--r--kernel/libc/stdio/print.c41
-rw-r--r--kernel/timer.c10
4 files changed, 32 insertions, 22 deletions
diff --git a/kernel/lib/sv.h b/kernel/lib/sv.h
index 40f33ca..9a70fb3 100644
--- a/kernel/lib/sv.h
+++ b/kernel/lib/sv.h
@@ -2,6 +2,7 @@
#define SV_H
#include "sb.h"
#include <stddef.h>
+#include <stdint.h>
#define SB_TO_SV(_sb) \
(struct sv) { \
diff --git a/kernel/libc/include/stdio.h b/kernel/libc/include/stdio.h
index 60808dd..111be17 100644
--- a/kernel/libc/include/stdio.h
+++ b/kernel/libc/include/stdio.h
@@ -2,6 +2,7 @@
#define STDIO_H
#include <stdarg.h>
#include <stddef.h>
+#include <lib/sb.h>
void putc(const char c);
void delete_characther(void);
@@ -9,5 +10,6 @@ int kprintf(const char *format, ...);
int vkprintf(const char *format, va_list list);
int ksnprintf(char *out, size_t size, const char *format, ...);
int kbnprintf(char *out, size_t size, const char *format, ...);
+int ksbprintf(struct sb *ctx, const char *format, ...);
#endif
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;
}
diff --git a/kernel/timer.c b/kernel/timer.c
index 6321cda..2cfd714 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -47,11 +47,13 @@ u64 timer_get_ms(void) {
}
int clock_read(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
- if (0 != offset) {
- return 0;
- }
+ struct sb ctx;
+ sb_init_buffer(&ctx, buffer, len);
+ sb_set_ignore(&ctx, offset);
+
u64 r = timer_get_ms();
- return min(len, (u64)kbnprintf(buffer, len, "%llu", r));
+ (void)ksbprintf(&ctx, "%llu", r);
+ return sv_length(SB_TO_SV(ctx));
}
int clock_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {