42_KFS/src/kprint/kvprintf.c

62 lines
1.3 KiB
C
Raw Normal View History

2024-09-19 12:00:18 -04:00
#include "base.h"
2024-09-07 08:40:46 -04:00
#include "ctype.h"
#include "terminal.h"
#include <stdarg.h>
2024-09-19 12:00:18 -04:00
#include <stdint.h>
2024-10-09 09:33:46 -04:00
int print_int_base(int64_t number, const char *prefix, const char *base);
2024-09-07 08:40:46 -04:00
static int get_level(const char *str)
{
if (!str || !isdigit(str[0]))
return 0;
return str[0] - '0';
}
2024-09-07 08:40:46 -04:00
static int print_flag(char flag, va_list *ap)
{
switch (flag) {
case '%':
return terminal_putchar('%');
case 'i':
2024-10-09 09:33:46 -04:00
return print_int_base(va_arg(*ap, int32_t), NULL, BASE_DECA);
2024-09-07 08:40:46 -04:00
case 'd':
2024-10-09 09:33:46 -04:00
return print_int_base(va_arg(*ap, int32_t), NULL, BASE_DECA);
2024-09-07 08:40:46 -04:00
case 'c':
return terminal_putchar(va_arg(*ap, int));
case 's':
return terminal_writestring(va_arg(*ap, char *));
2024-09-19 12:00:18 -04:00
case 'p':
2024-10-09 09:33:46 -04:00
return print_int_base(va_arg(*ap, uint32_t), "0x", BASE_HEXA);
2024-09-19 12:00:18 -04:00
case 'x':
2024-10-09 09:33:46 -04:00
return print_int_base(va_arg(*ap, int32_t), "0x", BASE_HEXA);
2024-09-19 12:00:18 -04:00
case 'u':
return print_int_base(va_arg(*ap, uint32_t), NULL, BASE_DECA);
2024-09-07 08:40:46 -04:00
}
return 0;
}
int kvprintf(const char *restrict format, va_list *ap)
2024-09-07 08:40:46 -04:00
{
const char *start = format;
int ret = 0;
const int level = get_level(format);
set_color_level(level);
if (level)
start++;
2024-09-07 08:40:46 -04:00
while (*start != '\0') {
if (*start == '%' && *(start + 1) != '\0') {
ret += print_flag(*(start + 1), ap);
2024-09-07 08:40:46 -04:00
start++;
} else {
ret += terminal_putchar(*start);
}
start++;
}
set_color_level(0);
update_cursor();
2024-09-07 08:40:46 -04:00
return ret;
}