]> git.tdb.fi Git - libs/core.git/blob - source/fmt.h
Fix compile erorrs on 64-bit systems
[libs/core.git] / source / fmt.h
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2008 Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_STRINGS_FMT_H_
9 #define MSP_STRINGS_FMT_H_
10
11 #include <ostream>
12 #include <string>
13
14 namespace Msp {
15
16 /**
17 Stores formatting information for converting variables into strings.  Can be
18 applied to an std::ostream or fed to lexical_cast.  Also used internally by
19 Formatter.
20
21 Formats can be constructed from printf-style conversion specifications, by
22 chaining calls to the various setter functions, or with a mixture of both.
23
24 Since type information for conversions is acquired through templates, the
25 meaning of the conversion specifier character is reduced to only specifying
26 what the conversion should look like.  Of special note is the s conversion,
27 which will result in a default conversion for any data type.  Size modifiers
28 are not supported and there is no difference between signed and unsigned
29 conversions.
30
31 Some new conversions are supported:
32
33   b/B  Binary integer conversion
34   P    Uppercase pointer conversion (like %#X)
35 */
36 class Fmt
37 {
38 public:
39         enum Type
40         {
41                 NUM,
42                 CHAR,
43                 STR
44         };
45
46         enum Base
47         {
48                 AUTOBASE = 0,
49                 DEC = 10,
50                 HEX = 16,
51                 OCT = 8,
52                 BIN = 2
53         };
54
55         enum FloatMode
56         {
57                 FIXED,
58                 AUTOFLT,
59                 SCI
60         };
61
62         enum Align
63         {
64                 LEFT,
65                 RIGHT
66         };
67
68 private:
69         unsigned  wd;
70         unsigned  prec;
71         bool      spos;
72         wchar_t   fillc;
73         Base      base;
74         bool      sbase;
75         FloatMode fmode;
76         bool      spoint;
77         Align     align;
78         bool      ucase;
79         Type      type;
80
81 public:
82         Fmt()                     { reset(); }
83         Fmt(const char *f)        { reset(); parse(f); }
84         Fmt(const std::string &f) { reset(); parse(f.c_str()); }
85
86         Fmt &width(unsigned w)      { wd=w; return *this; }
87         Fmt &precision(unsigned p)  { prec=p; return *this; }
88         Fmt &showpos(bool s=true)   { spos=s; return *this; }
89         Fmt &fill(wchar_t f)        { fillc=f; return *this; }
90         Fmt &fixed()                { fmode=FIXED; return *this; }
91         Fmt &scientific()           { fmode=SCI; return *this; }
92         Fmt &showpoint(bool s=true) { spoint=s; return *this; }
93         Fmt &showbase(bool s=true)  { sbase=s; return *this; }
94         Fmt &left()                 { align=LEFT; return *this; }
95         Fmt &right()                { align=RIGHT; return *this; }
96         Fmt &dec()                  { base=DEC; return *this; }
97         Fmt &hex()                  { base=HEX; return *this; }
98         Fmt &oct()                  { base=OCT; return *this; }
99         Fmt &bin()                  { base=BIN; return *this; }
100         Fmt &uppercase(bool u=true) { ucase=u; return *this; }
101         Fmt &numeric()              { type=NUM; return *this; }
102         Fmt &character()            { type=CHAR; return *this; }
103         Fmt &string()               { type=STR; return *this; }
104         Fmt &reset();
105
106         unsigned  get_width() const     { return wd; }
107         unsigned  get_precision() const { return prec; }
108         bool      get_showpos() const   { return spos; }
109         wchar_t   get_fill() const      { return fillc; }
110         Base      get_base() const      { return base; }
111         bool      get_showbase() const  { return sbase; }
112         FloatMode get_floatmode() const { return fmode; }
113         bool      get_showpoint() const { return spoint; }
114         Align     get_align() const     { return align; }
115         bool      get_uppercase() const { return ucase; }
116         Type      get_type() const      { return type; }
117
118         void apply(std::ostream &) const;
119 private:
120         void parse(const char *);
121 };
122
123 inline std::ostream &operator<<(std::ostream &o, const Fmt &f)
124 { f.apply(o); return o; }
125
126 } // namespace Msp
127
128 #endif