#include "stdio.h"
#include "string.h"
#include "stdarg.h"
#include "types.h"
#include "al/io.h"

static inline void print(char* str)
{
  if (!str)
    return;

  uint32_t length = 0;
  while (str[length] != '\0')
    length++;

  printc(str, length);
  
  return;
}

void printk(const char *str, ...)
{
  char buf[32];
  size_t i;
  
  
  va_list ap;
  
  va_start(ap, str);
  char c;
  char *tmp;

  while(*str != '\0')
  {
    for (i=0;i<32;i++)
      buf[i] = '0';
    switch(*str)
    {
      case '%':
        str++;
        switch(*str)
        {
          case '%':
            print("%%");
            str++;
            break;

          case 'b':
            print("0b");
            print(itoa(va_arg(ap, int), buf, 2));
            str++;
            break;

          case 'c':
            c = va_arg(ap, int);
            printc(&c, 1);
            str++;
            break;

          case 'd':
            print(itoa(va_arg(ap, int), buf, 10));
            str++;
            break;

          case 'p':
            print("0x");
            print(itoa(va_arg(ap, int), buf, 16));
            str++;
            break;

          case 's':
            print(va_arg(ap, char*));
            str++;
            break;

          case 'X':
            tmp = itoa(va_arg(ap, int), buf, 16);
            tmp = &buf[29];
            print(tmp);
            str++;
            break;
          
          case 'Y':
            tmp = itoa(va_arg(ap, int), buf, 16);
            tmp = &buf[24];
            print(tmp);
            str++;
            break;

          case 'x':
            print(itoa(va_arg(ap, int), buf, 16));
            str++;
            break;

          default:
            printc(str, 1);
            str++;
        }
        break;

      default:
        printc(str, 1);
        str++;
    }
  }

  va_end(ap);
  return;
}
