From 7e266a283784079f912f87f27a59a27f67986624 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 29 Mar 2016 11:53:16 -0400 Subject: [PATCH] Move printFormat() to kstd --- src/Console.hh | 8 +--- .../PrintFormat.cc} | 45 +++++++++---------- src/kstd/PrintFormat.hh | 22 +++++++++ 3 files changed, 45 insertions(+), 30 deletions(-) rename src/{ConsolePrintFormat.cc => kstd/PrintFormat.cc} (89%) create mode 100644 src/kstd/PrintFormat.hh diff --git a/src/Console.hh b/src/Console.hh index 647e661..e444b70 100644 --- a/src/Console.hh +++ b/src/Console.hh @@ -62,13 +62,9 @@ struct Console void printString(const char *str); /** - * Write a format string to the current cursor position. Returns the number - * of characters printed. + * Set the current cursor color. Subsequent characters will be written in + * this color. */ - int printFormat(const char *format, ...) PRINTF(2, 3); - int printFormat(const char *format, va_list args); - - void setColor(Color fg, Color bg); private: diff --git a/src/ConsolePrintFormat.cc b/src/kstd/PrintFormat.cc similarity index 89% rename from src/ConsolePrintFormat.cc rename to src/kstd/PrintFormat.cc index 637ba49..b1bded5 100644 --- a/src/ConsolePrintFormat.cc +++ b/src/kstd/PrintFormat.cc @@ -1,9 +1,9 @@ -/* ConsolePrintFormat.cc +/* PrintFormat.cc * vim: set tw=80: * Eryn Wells */ /** - * Implementation of Console::printFormat, a printf style method. + * Declares printFormat(), for writing formatted strings to the appropriate channel. */ #include @@ -168,25 +168,26 @@ Spec::print(kernel::Console& console) inline bool -is_size(char c) +isSize(char c) { return c == 'h' || c == 'l'; } inline bool -is_specifier(char c) +isSpecifier(char c) { return c == 'd' || c == 'x' || c == 'X' || c == 'c' || c == 's'; } } /* anonymous namespace */ -namespace kernel { + +namespace kstd { int -Console::printFormat(const char* format, - va_list args) +printFormat(const char* format, + va_list args) { enum { Default = 0, @@ -214,33 +215,33 @@ Console::printFormat(const char* format, state = Default; printChar(*p); nchars++; - } else if (kstd::Char::isDigit(*p)) { + } else if (Char::isDigit(*p)) { if (*p == '0' && !spec.zeroPadded) { spec.zeroPadded = true; } else { state = Width; spec.width = *p - '0'; } - } else if (is_size(*p)) { + } else if (isSize(*p)) { goto state_size; - } else if (is_specifier(*p)) { + } else if (isSpecifier(*p)) { goto state_specifier; } break; case Width: - if (kstd::Char::isDigit(*p)) { + if (Char::isDigit(*p)) { spec.width = 10 * spec.width + (*p - '0'); - } else if (is_size(*p)) { + } else if (isSize(*p)) { state = Size; goto state_size; - } else if (is_specifier(*p)) { + } else if (isSpecifier(*p)) { goto state_specifier; } break; case Size: - if (is_size(*p)) { + if (isSize(*p)) { goto state_size; - } else if (is_specifier(*p)) { + } else if (isSpecifier(*p)) { goto state_specifier; } break; @@ -253,13 +254,9 @@ Console::printFormat(const char* format, state_size: if (*p == 'h') { - spec.size = (spec.size == Spec::Size::Short) - ? Spec::Size::DoubleShort - : Spec::Size::Short; + spec.size = (spec.size == Spec::Size::Short) ? Spec::Size::DoubleShort : Spec::Size::Short; } else if (*p == 'l') { - spec.size = (spec.size == Spec::Size::Long) - ? Spec::Size::DoubleLong - : Spec::Size::Long; + spec.size = (spec.size == Spec::Size::Long) ? Spec::Size::DoubleLong : Spec::Size::Long; } continue; @@ -328,8 +325,8 @@ Console::printFormat(const char* format, int -Console::printFormat(const char* format, - ...) +printFormat(const char* format, + ...) { va_list args; va_start(args, format); @@ -338,4 +335,4 @@ Console::printFormat(const char* format, return nchars; } -} /* namespace kernel */ +} /* namespace kstd */ diff --git a/src/kstd/PrintFormat.hh b/src/kstd/PrintFormat.hh new file mode 100644 index 0000000..d82b855 --- /dev/null +++ b/src/kstd/PrintFormat.hh @@ -0,0 +1,22 @@ +/* PrintFormat.hh + * vim: set tw=80: + * Eryn Wells + */ +/** + * Declares printFormat(), for writing formatted strings to the appropriate channel. + */ + +#include + +namespace kstd { + +/** + * Write a format string to the appropriate output channel. + * @return Number of characters printed + * @{ + */ +int printFormat(const char* format, ...) PRINTF(2,3); +int printFormat(const char* format, va_list args); +/** @} */ + +} /* namespace kstd */