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);
/**
* 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:

View file

@ -1,9 +1,9 @@
/* ConsolePrintFormat.cc
/* PrintFormat.cc
* vim: set tw=80:
* 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>
@ -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 */

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 */