]> git.tdb.fi Git - libs/core.git/blob - source/fmt.h
c4b46b5e3d86f599853c964ca02e5eb073e0e32d
[libs/core.git] / source / fmt.h
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2007 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.  In particular, the d, i, u, c and s
27 conversions are no-ops.
28 */
29 class Fmt
30 {
31 public:
32         Fmt()                        { reset(); }
33         Fmt(const char *f)           { reset(); parse(f); }
34         Fmt(const std::string &f)    { reset(); parse(f.c_str()); }
35         Fmt  &width(unsigned w)      { wd=w; return *this; }
36         Fmt  &precision(unsigned p)  { prec=p; return *this; }
37         Fmt  &showpos(bool s=true)   { spos=s; return *this; }
38         Fmt  &fill(wchar_t f)        { fillc=f; return *this; }
39         Fmt  &fixed()                { fmode=FIXED; return *this; }
40         Fmt  &scientific()           { fmode=SCI; return *this; }
41         Fmt  &showpoint(bool s=true) { spoint=s; return *this; }
42         Fmt  &showbase(bool s=true)  { sbase=s; return *this; }
43         Fmt  &left()                 { align=LEFT; return *this; }
44         Fmt  &right()                { align=RIGHT; return *this; }
45         Fmt  &dec()                  { base=DEC; return *this; }
46         Fmt  &hex()                  { base=HEX; return *this; }
47         Fmt  &oct()                  { base=OCT; return *this; }
48         Fmt  &uppercase(bool u=true) { ucase=u; return *this; }
49         Fmt  &reset();
50         void apply(std::ostream &) const;
51 private:
52         enum Base
53         {
54                 DEC,
55                 HEX,
56                 OCT
57         };
58
59         enum FloatMode
60         {
61                 FIXED,
62                 EXP,
63                 SCI
64         };
65
66         enum Align
67         {
68                 LEFT,
69                 RIGHT
70         };
71
72         unsigned  wd;
73         unsigned  prec;
74         bool      spos;
75         wchar_t   fillc;
76         Base      base;
77         bool      sbase;
78         FloatMode fmode;
79         bool      spoint;
80         Align     align;
81         bool      ucase;
82
83         void parse(const char *);
84 };
85
86 inline std::ostream &operator<<(std::ostream &o, const Fmt &f)
87 { f.apply(o); return o; }
88
89 } // namespace Msp
90
91 #endif