From c6467e20d84987852e06863e91cc4c8794a2db47 Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 13 Apr 2021 00:20:45 -0500 Subject: [PATCH] Wrote partial printf implementation --- src/stdio.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/src/stdio.c b/src/stdio.c index 4a95c3c..1e2165d 100644 --- a/src/stdio.c +++ b/src/stdio.c @@ -1,11 +1,100 @@ #include "stdio.h" +#include +#include + +enum format_flags_t +{ + FORMAT_PADDING = '0', + FORMAT_WIDTH = '*', + + FORMAT_SIGNED_DECIMAL = 'i', + FORMAT_UNSIGNED_DECIMAL = 'u', + FORMAT_UNSIGNED_OCTAL = 'o', + FORMAT_UNSIGNED_HEX = 'x', + FORMAT_STRING = 's', + FORMAT_COUNT = 'n', + FORMAT_PERCENT = '%' + +}; + +char *itoa(unsigned int n, unsigned int base, unsigned int width) +{ + if (base < 2 || base > 16) + { + return NULL; + } + static const char *digits = "0123456789abcdef"; + static char buffer[65]; + char *s = &buffer[64]; + unsigned int count = 0; + do + { + *--s = digits[n % base]; + n /= base; + count++; + } while (count < width || n != 0); + return s; +} int printf(const char *format, ...) { - + va_list valist; + va_start(valist, format); + while (*format) + { + if (*format == '%') + { + size_t width = 0; + bool padding = false; + switch (*++format) + { + case FORMAT_PADDING: + padding = true; + format++; + break; + } + while (*format >= '0' && *format <= '9') + { + width = (width * 10) + *format - '0'; + format++; + } + switch (*format) + { + case FORMAT_SIGNED_DECIMAL:; + int n = va_arg(valist, int); + if (n < 0) + { + putchar('-'); + n *= -1; + } + puts(itoa((unsigned int)n, 10, width)); + break; + case FORMAT_UNSIGNED_DECIMAL: + puts(itoa(va_arg(valist, unsigned int), 10, width)); + break; + case FORMAT_UNSIGNED_OCTAL: + puts(itoa(va_arg(valist, unsigned int), 8, width)); + case FORMAT_UNSIGNED_HEX: + puts(itoa(va_arg(valist, unsigned int), 16, width)); + break; + case FORMAT_STRING: + puts(va_arg(valist, const char *)); + break; + case FORMAT_PERCENT: + putchar('%'); + break; + } + } + else + { + putchar(*format); + } + format++; + } + va_end(valist); + return 0; } int sprintf(char *str, const char *format, ...) { - } \ No newline at end of file