From a4350fa686a29cbbed738a8b969b6a9228074aa9 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 7 Sep 2024 00:37:47 +0200 Subject: [PATCH] add: kprintf, kvprintf --- headers/kprintf.h | 19 +++++++++++++++++++ src/kprint/kprintf.c | 18 ++++++++++++++++++ src/kprint/kvprintf.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 headers/kprintf.h create mode 100644 src/kprint/kprintf.c create mode 100644 src/kprint/kvprintf.c diff --git a/headers/kprintf.h b/headers/kprintf.h new file mode 100644 index 0000000..c90ec5f --- /dev/null +++ b/headers/kprintf.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +enum print_level { + KERN_EMERG, + KERN_ALERT, + KERN_CRIT, + KERN_ERR, + KERN_WARNING, + KERN_NOTICE, + KERN_INFO, + KERN_DEBUG, + KERN_DEFAULT, + KERN_CONT +} + +int kprintf(enum print_level level, const char *restrict format, ...); +int vprintf(enum print_level level, const char *restrict format, va_list ap); \ No newline at end of file diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c new file mode 100644 index 0000000..4c3d19c --- /dev/null +++ b/src/kprint/kprintf.c @@ -0,0 +1,18 @@ +#include "../../headers/kprint.h" +#include "../../headers/string.h" +#include "../../headers/terminal.h" +#include "../../headers/ctype.h" + +#include + +int kprintf(int level, const char *restrict format, ...) +{ + va_list va; + int i; + + va_start(va, format); + i = ft_vdprintf(level, format, va); + va_end(va); + + return (i); +} \ No newline at end of file diff --git a/src/kprint/kvprintf.c b/src/kprint/kvprintf.c new file mode 100644 index 0000000..b4dcfc0 --- /dev/null +++ b/src/kprint/kvprintf.c @@ -0,0 +1,41 @@ +#include "../../headers/kprint.h" +#include "../../headers/string.h" +#include "../../headers/terminal.h" +#include "../../headers/ctype.h" + +#include + +static int print_flag(char flag, va_list ap) +{ + switch (flag) { + case 'i': + return terminal_writelong(va_arg(ap, int)); + case 'd': + return terminal_writelong(va_arg(ap, int)); + case 'c': + return terminal_putchar(va_arg(ap, int)); + case 's': + return terminal_writestring(va_arg(ap, char *)); + } +} + +int kvprintf(int level, const char *restrict format, va_list ap) +{ + const char *start = format; + const char *flag; + long padding; + + while (*start != '\0') { + flag = strchr(start, '%'); + if (flag != NULL) { + padding = atoll(start + 1); + for (; isdigit(*padding); padding++); + print_flag(*padding, ap); + start = padding + 1; + } + else { + terminal_writestring(start); + start += strlen(start); + } + } +} \ No newline at end of file