X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ffmt.cpp;h=e06f71ab456f40d55825345ea413bc6981d10dd6;hp=0c1531d8315dfe8c19560be5b28ed2b8374cf9e7;hb=5b1368cb791cab043f0435628cacbaff36e39b7b;hpb=36f9e78ae75f5e14b132f37d249340ad3480b8ce diff --git a/source/fmt.cpp b/source/fmt.cpp index 0c1531d..e06f71a 100644 --- a/source/fmt.cpp +++ b/source/fmt.cpp @@ -12,47 +12,6 @@ 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=AUTOFLT; - spoint=false; - align=RIGHT; - ucase=false; - type=STR; - - 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; @@ -61,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; } @@ -86,47 +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; } } - type=NUM; + type = NUM; if(*f=='d' || *f=='u') - base=DEC; + base = DEC; else if(*f=='x' || *f=='X') - base=HEX; + base = HEX; else if(*f=='o') - base=OCT; + base = OCT; else if(*f=='b') - base=BIN; + 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=AUTOFLT; + fmode = AUTOFLT; else if(*f=='p' || *f=='P') { - base=HEX; - sbase=true; + base = HEX; + sbase = true; } else if(*f=='c') - type=CHAR; + type = CHAR; else if(*f=='s') - type=STR; + type = STR; else if(*f=='i') - base=AUTOBASE; + base = AUTOBASE; else throw InvalidParameterValue("Invalid conversion specifier"); if(*f=='E' || *f=='F' || *f=='G' || *f=='X' || *f=='P') - ucase=true; + ucase = true; ++f; @@ -134,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