Add Type::Pointer to printFormat()

This commit is contained in:
Eryn Wells 2016-04-17 16:16:17 -04:00
parent f641b5fc8d
commit d11120956e

View file

@ -25,11 +25,12 @@ struct Spec {
}; };
enum class Type { enum class Type {
Int,
Unsigned,
Hex,
Char, Char,
String Hex,
Int,
Pointer,
String,
Unsigned,
}; };
bool zeroPadded; bool zeroPadded;
@ -56,6 +57,7 @@ struct Spec {
u64 llx; u64 llx;
char c; char c;
char* s; char* s;
void* p;
} value; } value;
void clear(); void clear();
@ -82,12 +84,9 @@ Spec::print(kernel::Console& console)
int length = 0; int length = 0;
char buf[32]; char buf[32];
char pad = ' '; char pad = ' ';
char *str; char *str = nullptr;
/* // Type::Char is a special case because it will always be a single character in length and there is no \0 to terminate it.
* TypeChar is a special case because it will always be a single character
* in length and there is no \0 to terminate it.
*/
if (type == Type::Char) { if (type == Type::Char) {
if (width == 0) { if (width == 0) {
width = 1; width = 1;
@ -99,8 +98,8 @@ Spec::print(kernel::Console& console)
return width; return width;
} }
if (type == Type::Int || type == Type::Hex) { if (type == Type::Int || type == Type::Hex || type == Type::Pointer || type == Type::Unsigned) {
if (zeroPadded) { if (zeroPadded || type == Type::Pointer) {
pad = '0'; pad = '0';
} }
if (type == Type::Int) { if (type == Type::Int) {
@ -121,7 +120,7 @@ Spec::print(kernel::Console& console)
length = kstd::CString::fromInteger(value.lld, buf, 32); length = kstd::CString::fromInteger(value.lld, buf, 32);
break; break;
} }
} else { } else if (type == Type::Hex || type == Type::Unsigned) {
switch (size) { switch (size) {
case Size::Normal: case Size::Normal:
length = kstd::CString::fromUnsignedInteger(value.x, buf, 32, 16, capitalized); length = kstd::CString::fromUnsignedInteger(value.x, buf, 32, 16, capitalized);
@ -139,6 +138,8 @@ Spec::print(kernel::Console& console)
length = kstd::CString::fromUnsignedInteger(value.llx, buf, 32, 16, capitalized); length = kstd::CString::fromUnsignedInteger(value.llx, buf, 32, 16, capitalized);
break; break;
} }
} else if (type == Type::Pointer) {
length = kstd::CString::fromUnsignedInteger(value.p, buf, 32, 16, true);
} }
if (width < length) { if (width < length) {
width = length; width = length;
@ -178,7 +179,7 @@ isSize(char c)
inline bool inline bool
isSpecifier(char c) isSpecifier(char c)
{ {
return c == 'd' || c == 'x' || c == 'X' || c == 'c' || c == 's'; return c == 'c' || c == 'd' || c == 'i' || c == 'p' || c == 's' || c == 'x' || c == 'X';
} }
} /* anonymous namespace */ } /* anonymous namespace */
@ -266,6 +267,10 @@ printFormat(const char* format,
state_specifier: state_specifier:
state = Default; state = Default;
switch (*p) { switch (*p) {
case 'c':
spec.value.c = va_arg(args, int);
spec.type = Spec::Type::Char;
break;
case 'd': case 'd':
case 'i': case 'i':
switch (spec.size) { switch (spec.size) {
@ -287,6 +292,14 @@ printFormat(const char* format,
} }
spec.type = Spec::Type::Int; spec.type = Spec::Type::Int;
break; break;
case 'p':
spec.value.p = va_arg(args, void*);
spec.type = Spec::Type::Pointer;
break;
case 's':
spec.value.s = va_arg(args, char*);
spec.type = Spec::Type::String;
break;
case 'X': case 'X':
spec.capitalized = true; spec.capitalized = true;
// fall through // fall through
@ -310,14 +323,6 @@ printFormat(const char* format,
} }
spec.type = Spec::Type::Hex; spec.type = Spec::Type::Hex;
break; break;
case 'c':
spec.value.c = va_arg(args, int);
spec.type = Spec::Type::Char;
break;
case 's':
spec.value.s = va_arg(args, char*);
spec.type = Spec::Type::String;
break;
} }
nchars += spec.print(cons); nchars += spec.print(cons);
continue; continue;