Merge branch 'main' of git.chauvet.pro:starnakin/42_KFS

This commit is contained in:
0x35c 2024-09-20 12:41:25 +02:00
commit 0467c45bf0
4 changed files with 46 additions and 20 deletions

6
headers/base.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#define BASE_BIN "01"
#define BASE_OCTA BASE_BIN "234567"
#define BASE_DECA BASE_OCTA "89"
#define BASE_HEXA BASE_DECA "abcdef"

View File

@ -1,7 +1,11 @@
#include "base.h"
#include "ctype.h"
#include "terminal.h"
#include <stdarg.h>
#include <stdint.h>
int print_int_base(int32_t number, const char *prefix, const char *base);
static int get_level(const char *str)
{
@ -16,13 +20,19 @@ static int print_flag(char flag, va_list *ap)
case '%':
return terminal_putchar('%');
case 'i':
return terminal_writelong(va_arg(*ap, int));
return print_int_base(va_arg(*ap, int), NULL, BASE_DECA);
case 'd':
return terminal_writelong(va_arg(*ap, int));
return print_int_base(va_arg(*ap, int), NULL, BASE_DECA);
case 'c':
return terminal_putchar(va_arg(*ap, int));
case 's':
return terminal_writestring(va_arg(*ap, char *));
case 'p':
return print_int_base(va_arg(*ap, int), "0x", BASE_HEXA);
case 'x':
return print_int_base(va_arg(*ap, int), "0x", BASE_HEXA);
case 'u':
return print_int_base(va_arg(*ap, uint32_t), NULL, BASE_DECA);
}
return 0;
}

View File

@ -0,0 +1,28 @@
#include <stdint.h>
#include "string.h"
#include "terminal.h"
int print_int_base(int32_t number, const char *prefix, const char *base)
{
const int base_size = strlen(base);
uint64_t div = 1;
uint32_t tmp;
int rv = 0;
if (prefix)
terminal_writestring(prefix);
if (number < 0) {
rv += terminal_putchar('-');
tmp = -1 * number;
} else {
tmp = number;
}
while (div <= tmp / base_size)
div *= base_size;
while (div > 0) {
rv += terminal_putchar(base[tmp / div % base_size]);
div /= base_size;
}
return rv;
}

View File

@ -118,24 +118,6 @@ int terminal_writestring(const char *data)
return len;
}
int terminal_writelong(long n)
{
long div = 10;
int rv = 0;
if (n < 0) {
rv += terminal_putchar('-');
n *= -1;
}
div = 1;
while (div <= n / 10)
div *= 10;
while (div > 0) {
rv += terminal_putchar('0' + n / div % 10);
div /= 10;
}
return rv;
}
void update_cursor(void)
{
uint16_t pos = screen->row * VGA_WIDTH + screen->column;