changeset 88038cdbb9e1 in /z/repo/m5 details: http://repo.m5sim.org/m5?cmd=changeset;node=88038cdbb9e1 description: cprintf: support a configurable width and precision ("*" in printf)
diffstat: 3 files changed, 16 insertions(+), 2 deletions(-) src/base/cprintf.hh | 13 ++++++++++++- src/base/cprintf_formats.hh | 1 + src/unittest/cprintftest.cc | 4 +++- diffs (149 lines): diff -r 8ef4ad572a6b -r 88038cdbb9e1 src/base/cprintf.cc --- a/src/base/cprintf.cc Wed Dec 03 04:57:54 2008 -0800 +++ b/src/base/cprintf.cc Wed Dec 03 04:57:54 2008 -0800 @@ -40,7 +40,7 @@ namespace cp { Print::Print(std::ostream &stream, const std::string &format) - : stream(stream), format(format.c_str()), ptr(format.c_str()) + : stream(stream), format(format.c_str()), ptr(format.c_str()), cont(false) { saved_flags = stream.flags(); saved_fill = stream.fill(); @@ -48,7 +48,7 @@ } Print::Print(std::ostream &stream, const char *format) - : stream(stream), format(format), ptr(format) + : stream(stream), format(format), ptr(format), cont(false) { saved_flags = stream.flags(); saved_fill = stream.fill(); @@ -60,8 +60,10 @@ } void -Print::process(Format &fmt) +Print::process() { + fmt.clear(); + size_t len; while (*ptr) { @@ -221,8 +223,15 @@ number = number * 10 + (*ptr - '0'); break; + case '*': + if (have_precision) + fmt.get_precision = true; + else + fmt.get_width = true; + break; + case '%': - assert("we shouldn't get here"); + assert(false && "we shouldn't get here"); break; default: diff -r 8ef4ad572a6b -r 88038cdbb9e1 src/base/cprintf.hh --- a/src/base/cprintf.hh Wed Dec 03 04:57:54 2008 -0800 +++ b/src/base/cprintf.hh Wed Dec 03 04:57:54 2008 -0800 @@ -51,24 +51,53 @@ std::ostream &stream; const char *format; const char *ptr; + bool cont; std::ios::fmtflags saved_flags; char saved_fill; int saved_precision; - void process(Format &fmt); + Format fmt; + void process(); public: Print(std::ostream &stream, const std::string &format); Print(std::ostream &stream, const char *format); ~Print(); + int + get_number(int data) + { + return data; + } + + template <typename T> + int + get_number(const T& data) + { + return 0; + } + template <typename T> void add_arg(const T &data) { - Format fmt; - process(fmt); + if (!cont) + process(); + + if (fmt.get_width) { + fmt.get_width = false; + cont = true; + fmt.width = get_number(data); + return; + } + + if (fmt.get_precision) { + fmt.get_precision = false; + cont = true; + fmt.precision = get_number(data); + return; + } switch (fmt.format) { case Format::character: diff -r 8ef4ad572a6b -r 88038cdbb9e1 src/base/cprintf_formats.hh --- a/src/base/cprintf_formats.hh Wed Dec 03 04:57:54 2008 -0800 +++ b/src/base/cprintf_formats.hh Wed Dec 03 04:57:54 2008 -0800 @@ -50,6 +50,8 @@ enum { best, fixed, scientific } float_format; int precision; int width; + bool get_precision; + bool get_width; Format() { clear(); } @@ -65,6 +67,8 @@ format = none; precision = -1; width = 0; + get_precision = false; + get_width = false; } }; diff -r 8ef4ad572a6b -r 88038cdbb9e1 src/unittest/cprintftest.cc --- a/src/unittest/cprintftest.cc Wed Dec 03 04:57:54 2008 -0800 +++ b/src/unittest/cprintftest.cc Wed Dec 03 04:57:54 2008 -0800 @@ -167,6 +167,13 @@ cprintf("%c %c\n", 'c', 65); - cout << '9'; + cout << '9' << endl; + + cout << endl; + + cprintf("%08.4f\n", 99.99); + cprintf("%0*.*f\n", 8, 4, 99.99); + cprintf("%07.*f\n", 4, 1.234); + cprintf("%#0*x\n", 9, 123412); return 0; } _______________________________________________ m5-dev mailing list m5-dev@m5sim.org http://m5sim.org/mailman/listinfo/m5-dev