#include "printf.h" #include "types.h" #include "framebuffer.h" int printf(const char* format, ...) { int32_t* argp = (int32_t*)&format; int num; while(*format) { if(*format != '%') { framebuffer_putchar(*format++); num++; continue; } format++; bool is_signed = false; bool zero_pad = false; int radix = 16; int min_len = 0; if(*format == '0') { zero_pad = true; } while(*format >= '0' && *format <= '9') { min_len = min_len * 10 + *format++ - '0'; } switch(*format++) { case '%': framebuffer_putchar('%'); num++; break; case 'c': framebuffer_putchar((char)*++argp); num++; break; case 's': ; char* str = (char*)*++argp; while(*str) { framebuffer_putchar(*str++); num++; } break; case 'd': is_signed = true; case 'u': radix = 10; case 'x': ; uint32_t x = (uint32_t)*(++argp); if(is_signed && (int32_t)x < 0) { x = -x; framebuffer_putchar('-'); min_len--; } char buf[11]; char* bufp = &buf[sizeof(buf)]; *--bufp = 0; do { int d = x % radix; *--bufp = d < 10 ? '0' + d : 'a' - 10 + d; x /= radix; min_len--; } while(x); while(min_len > 0) { framebuffer_putchar(zero_pad ? '0' : ' '); num++; min_len--; } while(*bufp) { framebuffer_putchar(*bufp++); num++; } break; } } framebuffer_move_cursor(); return num; }