Move printFormat() to kstd

This commit is contained in:
Eryn Wells 2016-03-29 11:53:16 -04:00
parent 27fd989245
commit 7e266a2837
3 changed files with 45 additions and 30 deletions

View file

@ -62,13 +62,9 @@ struct Console
void printString(const char *str); void printString(const char *str);
/** /**
* Write a format string to the current cursor position. Returns the number * Set the current cursor color. Subsequent characters will be written in
* of characters printed. * this color.
*/ */
int printFormat(const char *format, ...) PRINTF(2, 3);
int printFormat(const char *format, va_list args);
void setColor(Color fg, Color bg); void setColor(Color fg, Color bg);
private: private:

View file

@ -1,9 +1,9 @@
/* ConsolePrintFormat.cc /* PrintFormat.cc
* vim: set tw=80: * vim: set tw=80:
* Eryn Wells <eryn@erynwells.me> * Eryn Wells <eryn@erynwells.me>
*/ */
/** /**
* Implementation of Console::printFormat, a printf style method. * Declares printFormat(), for writing formatted strings to the appropriate channel.
*/ */
#include <stdarg.h> #include <stdarg.h>
@ -168,25 +168,26 @@ Spec::print(kernel::Console& console)
inline bool inline bool
is_size(char c) isSize(char c)
{ {
return c == 'h' || c == 'l'; return c == 'h' || c == 'l';
} }
inline bool inline bool
is_specifier(char c) isSpecifier(char c)
{ {
return c == 'd' || c == 'x' || c == 'X' || c == 'c' || c == 's'; return c == 'd' || c == 'x' || c == 'X' || c == 'c' || c == 's';
} }
} /* anonymous namespace */ } /* anonymous namespace */
namespace kernel {
namespace kstd {
int int
Console::printFormat(const char* format, printFormat(const char* format,
va_list args) va_list args)
{ {
enum { enum {
Default = 0, Default = 0,
@ -214,33 +215,33 @@ Console::printFormat(const char* format,
state = Default; state = Default;
printChar(*p); printChar(*p);
nchars++; nchars++;
} else if (kstd::Char::isDigit(*p)) { } else if (Char::isDigit(*p)) {
if (*p == '0' && !spec.zeroPadded) { if (*p == '0' && !spec.zeroPadded) {
spec.zeroPadded = true; spec.zeroPadded = true;
} else { } else {
state = Width; state = Width;
spec.width = *p - '0'; spec.width = *p - '0';
} }
} else if (is_size(*p)) { } else if (isSize(*p)) {
goto state_size; goto state_size;
} else if (is_specifier(*p)) { } else if (isSpecifier(*p)) {
goto state_specifier; goto state_specifier;
} }
break; break;
case Width: case Width:
if (kstd::Char::isDigit(*p)) { if (Char::isDigit(*p)) {
spec.width = 10 * spec.width + (*p - '0'); spec.width = 10 * spec.width + (*p - '0');
} else if (is_size(*p)) { } else if (isSize(*p)) {
state = Size; state = Size;
goto state_size; goto state_size;
} else if (is_specifier(*p)) { } else if (isSpecifier(*p)) {
goto state_specifier; goto state_specifier;
} }
break; break;
case Size: case Size:
if (is_size(*p)) { if (isSize(*p)) {
goto state_size; goto state_size;
} else if (is_specifier(*p)) { } else if (isSpecifier(*p)) {
goto state_specifier; goto state_specifier;
} }
break; break;
@ -253,13 +254,9 @@ Console::printFormat(const char* format,
state_size: state_size:
if (*p == 'h') { if (*p == 'h') {
spec.size = (spec.size == Spec::Size::Short) spec.size = (spec.size == Spec::Size::Short) ? Spec::Size::DoubleShort : Spec::Size::Short;
? Spec::Size::DoubleShort
: Spec::Size::Short;
} else if (*p == 'l') { } else if (*p == 'l') {
spec.size = (spec.size == Spec::Size::Long) spec.size = (spec.size == Spec::Size::Long) ? Spec::Size::DoubleLong : Spec::Size::Long;
? Spec::Size::DoubleLong
: Spec::Size::Long;
} }
continue; continue;
@ -328,8 +325,8 @@ Console::printFormat(const char* format,
int int
Console::printFormat(const char* format, printFormat(const char* format,
...) ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@ -338,4 +335,4 @@ Console::printFormat(const char* format,
return nchars; return nchars;
} }
} /* namespace kernel */ } /* namespace kstd */

22
src/kstd/PrintFormat.hh Normal file
View file

@ -0,0 +1,22 @@
/* PrintFormat.hh
* vim: set tw=80:
* Eryn Wells <eryn@erynwells.me>
*/
/**
* Declares printFormat(), for writing formatted strings to the appropriate channel.
*/
#include <stdarg.h>
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 */