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/Makefile | 2 +- lib/stdio.h | 26 ++++++++++++++++-------- lib/stdio/vfprintf.c | 54 -------------------------------------------------- lib/stdio/vfprintf.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/string.h | 6 ++++++ 5 files changed, 79 insertions(+), 63 deletions(-) delete mode 100644 lib/stdio/vfprintf.c create mode 100644 lib/stdio/vfprintf.cpp (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile index 68d1e74..b3e1f66 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,6 +1,6 @@ include ../Makefile.config -libk.SRCS = stdio/printf.c stdio/fprintf.c stdio/vfprintf.c \ +libk.SRCS = stdio/printf.c stdio/fprintf.c stdio/vfprintf.cpp \ stdlib/memcpy.c stdlib/memset.c \ string/itoa.c diff --git a/lib/stdio.h b/lib/stdio.h index f7c1846..9a0c41e 100644 --- a/lib/stdio.h +++ b/lib/stdio.h @@ -2,16 +2,20 @@ #include +#ifdef __cplusplus /** An object type used for streams */ -typedef struct kIoDevice { - int id; - /** Functions that prints a character to the stream */ - void (*putc)(const struct kIoDevice *, char); +struct kIoDevice { + /** Function that prints a character to the stream */ + virtual void putc(char) = 0; /** Function that prints a string to the stream */ - int (*puts)(const struct kIoDevice *, const char *, int); - /** Flush all buffers */ - void (*flush)(const struct kIoDevice *); -} FILE; + virtual int puts(const char *, int) = 0; + /** Flush write buffers */ + virtual void flush() = 0; +}; +typedef kIoDevice FILE; +#else +typedef void FILE; +#endif /** A FILE value corresponding to stdin, the keyboard buffer */ extern FILE *stdin; @@ -20,6 +24,9 @@ extern FILE *stdout; /** A FILE value corresponding to stderr, the uart */ extern FILE *stderr; +#ifdef __cplusplus +extern "C" { +#endif /** * Write the formatted string to stdout * @@ -36,3 +43,6 @@ int fprintf(FILE *restrict stream, const char *restrict format, ...); * Write the formatted string to stream; see printf */ int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap); +#ifdef __cplusplus +} +#endif diff --git a/lib/stdio/vfprintf.c b/lib/stdio/vfprintf.c deleted file mode 100644 index d24e43e..0000000 --- a/lib/stdio/vfprintf.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include - -static char buffer[3 * sizeof(int) + 2]; - -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(stream, &format[s], l); - s = i + 2; - ++i; - - switch (format[i]) { - case 's': { - const char *arg = va_arg(params, const char *); - written += stream->puts(stream, arg, -1); - } break; - case 'c': { - const int arg = va_arg(params, int); - stream->putc(stream, arg); - ++written; - } break; - case 'd': { - const char *arg = itoa(buffer, va_arg(params, int), 10); - written += stream->puts(stream, arg, -1); - } break; - case 'u': { - const char *arg = utoa(buffer, va_arg(params, unsigned int), 10); - written += stream->puts(stream, arg, -1); - } break; - case 'x': { - const char *arg = utoa(buffer, va_arg(params, unsigned int), 16); - written += stream->puts(stream, arg, -1); - } break; - } - - l = 0; - } - - else - ++l; - } - - if (l > 0) { written += stream->puts(stream, &format[s], l); } - - stream->flush(stream); - return written; -} 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; +} diff --git a/lib/string.h b/lib/string.h index 1eb3da2..460057c 100644 --- a/lib/string.h +++ b/lib/string.h @@ -4,6 +4,9 @@ #define DECIMAL 10 #define HEX 16 +#ifdef __cplusplus +extern "C" { +#endif /** * Convert int into a string */ @@ -12,3 +15,6 @@ char *itoa(char *p, int x, int base); * Convert unsigned int into a string */ char *utoa(char *p, unsigned x, int base); +#ifdef __cplusplus +} +#endif -- cgit v1.2.1