diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-12-10 20:56:57 +0200 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-12-11 11:32:51 +0200 |
commit | 9b2a78fa52249ab481493550490aa5f37872dcf6 (patch) | |
tree | 127031268b9b42b0fbdae3d87684d9c045983677 /lib | |
parent | Rename CCFLAGS to CFLAGS (diff) | |
download | kernel-9b2a78fa52249ab481493550490aa5f37872dcf6.tar.xz |
Rewrite drivers/uart and drivers/vga in cpp
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile | 2 | ||||
-rw-r--r-- | lib/stdio.h | 26 | ||||
-rw-r--r-- | lib/stdio/vfprintf.cpp (renamed from lib/stdio/vfprintf.c) | 18 | ||||
-rw-r--r-- | lib/string.h | 6 |
4 files changed, 34 insertions, 18 deletions
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 <stdarg.h> +#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.cpp index d24e43e..aa9256d 100644 --- a/lib/stdio/vfprintf.c +++ b/lib/stdio/vfprintf.cpp @@ -3,7 +3,7 @@ static char buffer[3 * sizeof(int) + 2]; -int +extern "C" int vfprintf(FILE *restrict stream, const char *restrict format, va_list params) { int written = 0; @@ -12,31 +12,31 @@ vfprintf(FILE *restrict stream, const char *restrict format, va_list params) int l = 0; for (int i = 0; format[i] != '\0'; ++i) { if (format[i] == '%') { - written += stream->puts(stream, &format[s], l); + 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(stream, arg, -1); + written += stream->puts(arg, -1); } break; case 'c': { const int arg = va_arg(params, int); - stream->putc(stream, arg); + stream->putc(arg); ++written; } break; case 'd': { const char *arg = itoa(buffer, va_arg(params, int), 10); - written += stream->puts(stream, arg, -1); + written += stream->puts(arg, -1); } break; case 'u': { const char *arg = utoa(buffer, va_arg(params, unsigned int), 10); - written += stream->puts(stream, arg, -1); + written += stream->puts(arg, -1); } break; case 'x': { const char *arg = utoa(buffer, va_arg(params, unsigned int), 16); - written += stream->puts(stream, arg, -1); + written += stream->puts(arg, -1); } break; } @@ -47,8 +47,8 @@ vfprintf(FILE *restrict stream, const char *restrict format, va_list params) ++l; } - if (l > 0) { written += stream->puts(stream, &format[s], l); } + if (l > 0) { written += stream->puts(&format[s], l); } - stream->flush(stream); + 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 |