banan-os/libc/stdio/printf.cpp

191 lines
3.4 KiB
C++
Raw Normal View History

2022-11-12 23:36:39 +02:00
#include <limits.h>
#include <stdarg.h>
2022-11-13 00:33:45 +02:00
#include <stddef.h>
#include <stdio.h>
2022-11-15 09:27:11 +02:00
#include <stdint.h>
2022-11-12 23:36:39 +02:00
#include <string.h>
2022-11-12 23:36:39 +02:00
static bool print(const char* data, size_t len)
{
2022-11-14 00:27:11 +02:00
const unsigned char* bytes = reinterpret_cast<const unsigned char*>(data);
2022-11-12 23:36:39 +02:00
for(size_t i = 0; i < len; i++)
if (putchar(bytes[i]) == EOF)
return false;
return true;
}
2022-11-15 09:27:11 +02:00
template<typename T>
static bool print_integer(T value, size_t& out_len)
2022-11-12 23:36:39 +02:00
{
2022-11-15 09:27:11 +02:00
char buffer[32] {};
int len = 0;
2022-11-12 23:36:39 +02:00
bool sign = false;
if (value < 0)
{
sign = true;
2022-11-15 09:27:11 +02:00
buffer[len++] = ((value % 10 + 10) % 10) + '0';
value = -(value / 10);
2022-11-12 23:36:39 +02:00
}
while (value)
{
2022-11-15 09:27:11 +02:00
buffer[len++] = (value % 10) + '0';
2022-11-12 23:36:39 +02:00
value /= 10;
}
2022-11-15 21:42:14 +02:00
2022-11-12 23:36:39 +02:00
if (sign)
2022-11-15 09:27:11 +02:00
buffer[len++] = '-';
if (len == 0)
buffer[len++] = '0';
for (int i = 0; i < len / 2; i++)
2022-11-12 23:36:39 +02:00
{
2022-11-15 09:27:11 +02:00
char temp = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = temp;
2022-11-12 23:36:39 +02:00
}
2022-11-15 09:27:11 +02:00
if (!print(buffer, len))
2022-11-12 23:36:39 +02:00
return false;
2022-11-14 00:27:11 +02:00
out_len = len;
2022-11-12 23:36:39 +02:00
return true;
}
2022-11-15 21:42:14 +02:00
static char bits_to_hex(int bits, bool upper_case)
2022-11-13 00:33:45 +02:00
{
if (bits < 10)
return bits + '0';
2022-11-15 21:42:14 +02:00
return bits - 10 + (upper_case ? 'A' : 'a');
2022-11-13 00:33:45 +02:00
}
2022-11-15 21:42:14 +02:00
template<typename T>
static bool print_hex(T value, bool upper_case, size_t& out_len)
2022-11-13 00:33:45 +02:00
{
2022-11-15 21:42:14 +02:00
char buffer[16] {};
int len = 0;
2022-11-13 00:33:45 +02:00
2022-11-15 21:42:14 +02:00
while (value)
{
buffer[len++] = bits_to_hex(value & 0xF, upper_case);
value >>= 4;
}
2022-11-13 00:33:45 +02:00
2022-11-15 21:42:14 +02:00
if (len == 0)
buffer[len++] = '0';
2022-11-13 00:33:45 +02:00
2022-11-15 21:42:14 +02:00
for (int i = 0; i < len / 2; i++)
2022-11-13 00:33:45 +02:00
{
2022-11-15 21:42:14 +02:00
char temp = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = temp;
2022-11-13 00:33:45 +02:00
}
2022-11-15 21:42:14 +02:00
if (!print(buffer, len))
2022-11-13 00:33:45 +02:00
return false;
2022-11-15 21:42:14 +02:00
out_len = len;
2022-11-13 00:33:45 +02:00
return true;
}
2022-11-14 00:27:11 +02:00
int printf(const char* __restrict format, ...)
2022-11-12 23:36:39 +02:00
{
va_list args;
va_start(args, format);
int written = 0;
while (*format)
{
size_t max_rem = INT_MAX - written;
if (format[0] != '%' || format[1] == '%')
{
if (format[0] == '%')
format++;
size_t len = 1;
while (format[len] && format[len] != '%')
len++;
if (!print(format, len))
return -1;
format += len;
written += len;
continue;
}
const char* format_start = format++;
if (*format == 'c')
{
format++;
char c = (char)va_arg(args, int);
if (!max_rem) // FIXME: EOVERFLOW
return -1;
if (!print(&c, sizeof(c)))
return -1;
written++;
}
else if (*format == 's')
{
format++;
const char* str = va_arg(args, const char*);
size_t len = strlen(str);
if (max_rem < len) // FIXME: EOVERFLOW
return -1;
if (!print(str, len))
return -1;
written += len;
}
else if (*format == 'd' || *format == 'i')
{
format++;
int value = va_arg(args, int);
2022-11-13 00:33:45 +02:00
size_t len;
2022-11-15 09:27:11 +02:00
if (!print_integer<int>(value, len))
return -1;
written += len;
}
else if (*format == 'u')
{
format++;
unsigned int value = va_arg(args, unsigned int);
size_t len;
if (!print_integer<unsigned int>(value, len))
2022-11-12 23:36:39 +02:00
return -1;
written += len;
}
2022-11-15 21:42:14 +02:00
else if (*format == 'x' || *format == 'X')
{
format++;
unsigned int value = va_arg(args, unsigned int);
size_t len;
if (!print_hex<unsigned int>(value, *(format - 1) == 'X', len))
return -1;
written += len;
}
2022-11-13 00:33:45 +02:00
else if (*format == 'p')
{
format++;
2022-11-15 21:42:14 +02:00
void* ptr = va_arg(args, void*);
2022-11-13 00:33:45 +02:00
size_t len;
2022-11-15 21:42:14 +02:00
if (!print("0x", 2) || !print_hex<ptrdiff_t>((ptrdiff_t)ptr, false, len))
2022-11-13 00:33:45 +02:00
return -1;
written += len;
}
2022-11-12 23:36:39 +02:00
else
{
format = format_start;
size_t len = strlen(format);
if (max_rem < len) // FIXME: EOVERFLOW
return -1;
if (!print(format, len))
return -1;
written += len;
format += len;
}
}
va_end(args);
return written;
}