]> git.tdb.fi Git - libs/core.git/blob - source/fmt.cpp
Add copyright notices and Id tags
[libs/core.git] / source / fmt.cpp
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #include <msp/error.h>
9 #include "fmt.h"
10
11 using namespace std;
12
13 namespace Msp {
14
15 /**
16 Resets the format to the default.  Mainly used by constructors.
17 */
18 Fmt &Fmt::reset()
19 {
20         wd=0;
21         prec=6;
22         spos=false;
23         fillc=' ';
24         base=DEC;
25         sbase=false;
26         fmode=EXP;
27         spoint=false;
28         align=RIGHT;
29         ucase=false;
30
31         return *this;
32 }
33
34 /**
35 Applies the format to the given ostream.  All existing formatting information
36 is overwritten.
37 */
38 void Fmt::apply(ostream &out) const
39 {
40         out.flags(((base==HEX) ? ios_base::hex : (base==OCT) ? ios_base::oct : ios_base::dec)
41                 | ((fmode==SCI) ? ios_base::scientific : (fmode==FIXED) ? ios_base::fixed : ios_base::fmtflags(0))
42                 | (fillc=='0' ? ios_base::internal : (align==LEFT) ? ios_base::left : ios_base::right)
43                 | (sbase ? ios_base::showbase : ios_base::fmtflags(0))
44                 | (spoint ? ios_base::showpoint : ios_base::fmtflags(0))
45                 | (spos ? ios_base::showpos : ios_base::fmtflags(0))
46                 | (ucase ? ios_base::uppercase : ios_base::fmtflags(0)));
47         out.fill(fillc);
48         out.width(wd);
49         out.precision(prec);
50 }
51
52 /**
53 Parses a printf-style conversion specification.  Called from constructors.
54 */
55 void Fmt::parse(const char *f)
56 {
57         if(*f=='%') ++f;
58
59         for(; *f; ++f)
60         {
61                 if(*f=='#')
62                 {
63                         sbase=true;
64                         spoint=true;
65                 }
66                 else if(*f=='0')
67                         fillc='0';
68                 else if(*f=='-')
69                         align=LEFT;
70                 else if(*f=='+')
71                         spos=true;
72                 else
73                         break;
74         }
75
76         wd=0;
77         for(; *f; ++f)
78         {
79                 if(*f>='0' && *f<='9')
80                         wd=wd*10+(*f-'0');
81                 else
82                         break;
83         }
84
85         if(*f=='.')
86         {
87                 ++f;
88                 prec=0;
89                 for(; *f; ++f)
90                 {
91                         if(*f>='0' && *f<='9')
92                                 prec=prec*10+(*f-'0');
93                         else
94                                 break;
95                 }
96         }
97
98         if(*f=='x' || *f=='X')
99                 base=HEX;
100         else if(*f=='o')
101                 base=OCT;
102         else if(*f=='e' || *f=='E')
103                 fmode=SCI;
104         else if(*f=='f' || *f=='F')
105                 fmode=FIXED;
106         else if(*f=='g' || *f=='G')
107                 fmode=EXP;
108         else if(*f=='p')
109         {
110                 base=HEX;
111                 sbase=true;
112         }
113         else if(*f=='d' || *f=='i' || *f=='u' || *f=='c' || *f=='s')
114                 ;
115         else
116                 throw InvalidParameterValue("Invalid conversion specifier");
117
118         if(*f=='E' || *f=='F' || *f=='G' || *f=='X')
119                 ucase=true;
120
121         ++f;
122
123         if(*f)
124                 throw InvalidParameterValue("Extra characters in conversion specification");
125 }
126
127 } // namespace Msp