From 9b2a78fa52249ab481493550490aa5f37872dcf6 Mon Sep 17 00:00:00 2001 From: aqua Date: Sat, 10 Dec 2022 20:56:57 +0200 Subject: Rewrite drivers/uart and drivers/vga in cpp --- lib/stdio/vfprintf.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lib/stdio/vfprintf.cpp (limited to 'lib/stdio/vfprintf.cpp') diff --git a/lib/stdio/vfprintf.cpp b/lib/stdio/vfprintf.cpp new file mode 100644 index 0000000..aa9256d --- /dev/null +++ b/lib/stdio/vfprintf.cpp @@ -0,0 +1,54 @@ +#include +#include + +static char buffer[3 * sizeof(int) + 2]; + +extern "C" int +vfprintf(FILE *restrict stream, const char *restrict format, va_list params) +{ + int written = 0; + + int s = 0; + int l = 0; + for (int i = 0; format[i] != '\0'; ++i) { + if (format[i] == '%') { + written += stream->puts(&format[s], l); + s = i + 2; + ++i; + + switch (format[i]) { + case 's': { + const char *arg = va_arg(params, const char *); + written += stream->puts(arg, -1); + } break; + case 'c': { + const int arg = va_arg(params, int); + stream->putc(arg); + ++written; + } break; + case 'd': { + const char *arg = itoa(buffer, va_arg(params, int), 10); + written += stream->puts(arg, -1); + } break; + case 'u': { + const char *arg = utoa(buffer, va_arg(params, unsigned int), 10); + written += stream->puts(arg, -1); + } break; + case 'x': { + const char *arg = utoa(buffer, va_arg(params, unsigned int), 16); + written += stream->puts(arg, -1); + } break; + } + + l = 0; + } + + else + ++l; + } + + if (l > 0) { written += stream->puts(&format[s], l); } + + stream->flush(); + return written; +} -- cgit v1.2.1