add: kprintf, kvprintf

This commit is contained in:
starnakin 2024-09-07 00:37:47 +02:00
parent bdaec88e8b
commit a4350fa686
3 changed files with 78 additions and 0 deletions

19
headers/kprintf.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <stdarg.h>
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);

18
src/kprint/kprintf.c Normal file
View File

@ -0,0 +1,18 @@
#include "../../headers/kprint.h"
#include "../../headers/string.h"
#include "../../headers/terminal.h"
#include "../../headers/ctype.h"
#include <stdarg.h>
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);
}

41
src/kprint/kvprintf.c Normal file
View File

@ -0,0 +1,41 @@
#include "../../headers/kprint.h"
#include "../../headers/string.h"
#include "../../headers/terminal.h"
#include "../../headers/ctype.h"
#include <stdarg.h>
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);
}
}
}