jankos/stlio.c
2019-11-29 16:50:33 -08:00

90 lines
1.8 KiB
C

#include "types.h"
#include "kbd.h"
#include "stlio.h"
extern char kbd_prev_key;
extern int kbd_prev_active;
static u8 COLOR_FG = Green;
static u8 COLOR_BG = White;
#define write_char(c) write_cell_fb(c, COLOR_FG, COLOR_BG)
// We are assuming null-terminated strings here
u32 strlen(const char* buffer) {
u32 i = 0;
char c = buffer[i];
while(c != '\0') {
i++;
c = buffer[i];
}
return i;
}
u32 write(const char* buffer, const u32 size) {
u32 i;
for(i = 0; i < size; i++) {
// cheesy but whatever
if(buffer[i] == '\n') {
frame_buffer_newline();
}
else {
write_char(buffer[i]);
}
}
return i;
}
u32 read(char* buffer, u32 size) {
// Read up to size-1 characters or until a \n is written
// once we reacch size -1 bytes read then we change the last char to a \0
kbd_prev_active = false; // we only do this to discard the leftovers from
u32 bytes_read = 0;
while(bytes_read < size-1) {
if(kbd_prev_active) {
bytes_read++;
if(kbd_prev_key == '\n') {
break;
}
else {
buffer[bytes_read] = kbd_prev_key;
}
}
}
printf("You entered: "); printf(buffer);
return bytes_read;
}
void printf(const char* fmt) {
// Variadic fuller version of print on seperate branch but its nowhere near stable/ready/working
u32 size = strlen(fmt);
for(u32 i = 0; i < size;i++) {
if(fmt[i] == '\n') {
frame_buffer_newline();
}
else {
write_char(fmt[i]);
}
}
}
void printhex(u32 num) {
// Prints our hex version of whatever is given
const char _chars[] = "0123456789abcdef";
u32 idx = 0; // used as hash into the _chars mapping
// iterate over each nibble
for(u32 i = 0 ;i < 8;i++) {
idx = (num << (i * 4)) & 0xf0000000;
idx = idx >> 28;
putch(_chars[idx]);
}
}
void putch(const char c) {
if(c == '\n') {
frame_buffer_newline();
}
else {
write_char(c);
}
}