X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ffmt.cpp;h=e06f71ab456f40d55825345ea413bc6981d10dd6;hp=05f9be5a2decaaa558a5ba30c61b806d41eb5639;hb=5b1368cb791cab043f0435628cacbaff36e39b7b;hpb=9da6abdcabec59f4845da256a8ad75a810ed1589 diff --git a/source/fmt.cpp b/source/fmt.cpp index 05f9be5..e06f71a 100644 --- a/source/fmt.cpp +++ b/source/fmt.cpp @@ -1,57 +1,17 @@ /* $Id$ This file is part of libmspstrings -Copyright © 2006-2007 Mikko Rasa +Copyright © 2006-2008 Mikko Rasa Distributed under the LGPL */ -#include +#include #include "fmt.h" using namespace std; namespace Msp { -/** -Resets the format to the default. Mainly used by constructors. -*/ -Fmt &Fmt::reset() -{ - wd=0; - prec=6; - spos=false; - fillc=' '; - base=DEC; - sbase=false; - fmode=EXP; - spoint=false; - align=RIGHT; - ucase=false; - - return *this; -} - -/** -Applies the format to the given ostream. All existing formatting information -is overwritten. -*/ -void Fmt::apply(ostream &out) const -{ - out.flags(((base==HEX) ? ios_base::hex : (base==OCT) ? ios_base::oct : ios_base::dec) - | ((fmode==SCI) ? ios_base::scientific : (fmode==FIXED) ? ios_base::fixed : ios_base::fmtflags(0)) - | (fillc=='0' ? ios_base::internal : (align==LEFT) ? ios_base::left : ios_base::right) - | (sbase ? ios_base::showbase : ios_base::fmtflags(0)) - | (spoint ? ios_base::showpoint : ios_base::fmtflags(0)) - | (spos ? ios_base::showpos : ios_base::fmtflags(0)) - | (ucase ? ios_base::uppercase : ios_base::fmtflags(0))); - out.fill(fillc); - out.width(wd); - out.precision(prec); -} - -/** -Parses a printf-style conversion specification. Called from constructors. -*/ void Fmt::parse(const char *f) { if(*f=='%') ++f; @@ -60,24 +20,24 @@ void Fmt::parse(const char *f) { if(*f=='#') { - sbase=true; - spoint=true; + sbase = true; + spoint = true; } else if(*f=='0') - fillc='0'; + fillc = '0'; else if(*f=='-') - align=LEFT; + align = LEFT; else if(*f=='+') - spos=true; + spos = true; else break; } - wd=0; + wd = 0; for(; *f; ++f) { if(*f>='0' && *f<='9') - wd=wd*10+(*f-'0'); + wd = wd*10+(*f-'0'); else break; } @@ -85,38 +45,47 @@ void Fmt::parse(const char *f) if(*f=='.') { ++f; - prec=0; + prec = 0; for(; *f; ++f) { if(*f>='0' && *f<='9') - prec=prec*10+(*f-'0'); + prec = prec*10+(*f-'0'); else break; } } - if(*f=='x' || *f=='X') - base=HEX; + type = NUM; + if(*f=='d' || *f=='u') + base = DEC; + else if(*f=='x' || *f=='X') + base = HEX; else if(*f=='o') - base=OCT; + base = OCT; + else if(*f=='b') + base = BIN; else if(*f=='e' || *f=='E') - fmode=SCI; + fmode = SCI; else if(*f=='f' || *f=='F') - fmode=FIXED; + fmode = FIXED; else if(*f=='g' || *f=='G') - fmode=EXP; - else if(*f=='p') + fmode = AUTOFLT; + else if(*f=='p' || *f=='P') { - base=HEX; - sbase=true; + base = HEX; + sbase = true; } - else if(*f=='d' || *f=='i' || *f=='u' || *f=='c' || *f=='s') - ; + else if(*f=='c') + type = CHAR; + else if(*f=='s') + type = STR; + else if(*f=='i') + base = AUTOBASE; else throw InvalidParameterValue("Invalid conversion specifier"); - if(*f=='E' || *f=='F' || *f=='G' || *f=='X') - ucase=true; + if(*f=='E' || *f=='F' || *f=='G' || *f=='X' || *f=='P') + ucase = true; ++f; @@ -124,4 +93,35 @@ void Fmt::parse(const char *f) throw InvalidParameterValue("Extra characters in conversion specification"); } +Fmt &Fmt::reset() +{ + wd = 0; + prec = 6; + spos = false; + fillc = ' '; + base = DEC; + sbase = false; + fmode = AUTOFLT; + spoint = false; + align = RIGHT; + ucase = false; + type = STR; + + return *this; +} + +void Fmt::apply(ostream &out) const +{ + out.flags(((base==HEX) ? ios_base::hex : (base==OCT) ? ios_base::oct : ios_base::dec) + | ((fmode==SCI) ? ios_base::scientific : (fmode==FIXED) ? ios_base::fixed : ios_base::fmtflags(0)) + | (fillc=='0' ? ios_base::internal : (align==LEFT) ? ios_base::left : ios_base::right) + | (sbase ? ios_base::showbase : ios_base::fmtflags(0)) + | (spoint ? ios_base::showpoint : ios_base::fmtflags(0)) + | (spos ? ios_base::showpos : ios_base::fmtflags(0)) + | (ucase ? ios_base::uppercase : ios_base::fmtflags(0))); + out.fill(fillc); + out.width(wd); + out.precision(prec); +} + } // namespace Msp