]> git.tdb.fi Git - libs/core.git/blobdiff - source/fmt.cpp
Rewrite lexical_cast to use internal conversion routines
[libs/core.git] / source / fmt.cpp
index c9f5d6c70eac16ff071acaf9208d9001925f2993..0c1531d8315dfe8c19560be5b28ed2b8374cf9e7 100644 (file)
@@ -1,4 +1,11 @@
-#include <msp/error.h>
+/* $Id$
+
+This file is part of libmspstrings
+Copyright © 2006-2008 Mikko Rasa
+Distributed under the LGPL
+*/
+
+#include <msp/core/except.h>
 #include "fmt.h"
 
 using namespace std;
@@ -16,10 +23,11 @@ Fmt &Fmt::reset()
        fillc=' ';
        base=DEC;
        sbase=false;
-       fmode=EXP;
+       fmode=AUTOFLT;
        spoint=false;
        align=RIGHT;
        ucase=false;
+       type=STR;
 
        return *this;
 }
@@ -32,7 +40,7 @@ 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 ? ios_base::internal : (align==LEFT) ? ios_base::left : ios_base::right)
+               | (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))
@@ -88,27 +96,36 @@ void Fmt::parse(const char *f)
                }
        }
 
-       if(*f=='x' || *f=='X')
+       type=NUM;
+       if(*f=='d' || *f=='u')
+               base=DEC;
+       else if(*f=='x' || *f=='X')
                base=HEX;
        else if(*f=='o')
                base=OCT;
+       else if(*f=='b')
+               base=BIN;
        else if(*f=='e' || *f=='E')
-               fmode=EXP;
+               fmode=SCI;
        else if(*f=='f' || *f=='F')
                fmode=FIXED;
        else if(*f=='g' || *f=='G')
-               fmode=SCI;
-       else if(*f=='p')
+               fmode=AUTOFLT;
+       else if(*f=='p' || *f=='P')
        {
                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')
+       if(*f=='E' || *f=='F' || *f=='G' || *f=='X' || *f=='P')
                ucase=true;
 
        ++f;