diff options
Diffstat (limited to '')
-rw-r--r-- | src/formatter.h | 101 |
1 files changed, 7 insertions, 94 deletions
diff --git a/src/formatter.h b/src/formatter.h index 49507de..f87a9be 100644 --- a/src/formatter.h +++ b/src/formatter.h | |||
@@ -8,101 +8,19 @@ | |||
8 | #ifndef BU_FORMATTER_H | 8 | #ifndef BU_FORMATTER_H |
9 | #define BU_FORMATTER_H | 9 | #define BU_FORMATTER_H |
10 | 10 | ||
11 | #include "stream.h" | 11 | #include "bu/string.h" |
12 | #include "bu/fmt.h" | ||
12 | 13 | ||
13 | namespace Bu | 14 | namespace Bu |
14 | { | 15 | { |
16 | class Stream; | ||
17 | |||
15 | class Formatter | 18 | class Formatter |
16 | { | 19 | { |
17 | public: | 20 | public: |
18 | Formatter( Stream &rStream ); | 21 | Formatter( Stream &rStream ); |
19 | virtual ~Formatter(); | 22 | virtual ~Formatter(); |
20 | 23 | ||
21 | typedef struct Fmt | ||
22 | { | ||
23 | enum Alignment | ||
24 | { | ||
25 | Left = 0, | ||
26 | Center = 1, | ||
27 | Right = 2 | ||
28 | }; | ||
29 | Fmt() : | ||
30 | uMinWidth( 0 ), | ||
31 | cFillChar(' '), | ||
32 | uRadix( 10 ), | ||
33 | uAlign( Right ), | ||
34 | bPlus( false ), | ||
35 | bCaps( true ), | ||
36 | bTokenize( true ) | ||
37 | { | ||
38 | } | ||
39 | |||
40 | Fmt( unsigned int uMinWidth, unsigned int uRadix=10, | ||
41 | Alignment a=Right, bool bPlus=false, bool bCaps=true, | ||
42 | char cFill=' ') : | ||
43 | uMinWidth( uMinWidth ), | ||
44 | cFillChar(cFill), | ||
45 | uRadix( uRadix ), | ||
46 | uAlign( a ), | ||
47 | bPlus( bPlus ), | ||
48 | bCaps( bCaps ), | ||
49 | bTokenize( true ) | ||
50 | { | ||
51 | } | ||
52 | Fmt( unsigned int uMinWidth, Alignment a, | ||
53 | unsigned int uRadix=10, bool bPlus=false, bool bCaps=true, | ||
54 | char cFill=' ') : | ||
55 | uMinWidth( uMinWidth ), | ||
56 | cFillChar(cFill), | ||
57 | uRadix( uRadix ), | ||
58 | uAlign( a ), | ||
59 | bPlus( bPlus ), | ||
60 | bCaps( bCaps ), | ||
61 | bTokenize( true ) | ||
62 | { | ||
63 | } | ||
64 | |||
65 | static Fmt hex( unsigned int uWidth=0, bool bCaps=true ) | ||
66 | { | ||
67 | return Fmt( uWidth, 16, Right, false, bCaps, '0' ); | ||
68 | } | ||
69 | |||
70 | static Fmt oct( unsigned int uWidth=0 ) | ||
71 | { | ||
72 | return Fmt( uWidth, 8, Right, false, false, '0' ); | ||
73 | } | ||
74 | |||
75 | static Fmt bin( unsigned int uWidth=0 ) | ||
76 | { | ||
77 | return Fmt( uWidth, 1, Right, false, false, '0' ); | ||
78 | } | ||
79 | |||
80 | static Fmt ptr( bool bCaps=true ) | ||
81 | { | ||
82 | return Fmt( sizeof(ptrdiff_t)*2, 16, Right, false, bCaps, '0' ); | ||
83 | } | ||
84 | |||
85 | Fmt &width( unsigned int uWidth ); | ||
86 | Fmt &fill( char cFill='0' ); | ||
87 | Fmt &radix( unsigned int uRadix ); | ||
88 | Fmt &align( Alignment eAlign ); | ||
89 | Fmt &plus( bool bPlus=true ); | ||
90 | Fmt &caps( bool bCaps=true ); | ||
91 | Fmt &tokenize( bool bTokenize=true ); | ||
92 | |||
93 | Fmt &left(); | ||
94 | Fmt &right(); | ||
95 | Fmt ¢er(); | ||
96 | |||
97 | unsigned char uMinWidth; | ||
98 | char cFillChar; | ||
99 | unsigned short uRadix : 6; | ||
100 | unsigned short uAlign : 2; | ||
101 | unsigned short bPlus : 1; | ||
102 | unsigned short bCaps : 1; | ||
103 | unsigned short bTokenize : 1; | ||
104 | } Fmt; | ||
105 | |||
106 | void write( const Bu::String &sStr ); | 24 | void write( const Bu::String &sStr ); |
107 | void write( const void *sStr, int iLen ); | 25 | void write( const void *sStr, int iLen ); |
108 | void writeAligned( const Bu::String &sStr ); | 26 | void writeAligned( const Bu::String &sStr ); |
@@ -200,7 +118,7 @@ namespace Bu | |||
200 | void ffmt( type f ) | 118 | void ffmt( type f ) |
201 | { | 119 | { |
202 | Bu::String fTmp; | 120 | Bu::String fTmp; |
203 | fTmp.format("%f", f ); | 121 | // fTmp.format("%f", f ); |
204 | // writeAligned("**make floats work**"); | 122 | // writeAligned("**make floats work**"); |
205 | writeAligned( fTmp ); | 123 | writeAligned( fTmp ); |
206 | usedFormat(); | 124 | usedFormat(); |
@@ -269,10 +187,7 @@ namespace Bu | |||
269 | flush | 187 | flush |
270 | }; | 188 | }; |
271 | 189 | ||
272 | void doFlush() | 190 | void doFlush(); |
273 | { | ||
274 | rStream.flush(); | ||
275 | } | ||
276 | 191 | ||
277 | private: | 192 | private: |
278 | Stream &rStream; | 193 | Stream &rStream; |
@@ -282,9 +197,7 @@ namespace Bu | |||
282 | char cIndent; | 197 | char cIndent; |
283 | }; | 198 | }; |
284 | 199 | ||
285 | typedef Formatter::Fmt Fmt; | 200 | Formatter &operator<<( Formatter &f, const Fmt &fmt ); |
286 | |||
287 | Formatter &operator<<( Formatter &f, const Formatter::Fmt &fmt ); | ||
288 | Formatter &operator<<( Formatter &f, Formatter::Special s ); | 201 | Formatter &operator<<( Formatter &f, Formatter::Special s ); |
289 | Formatter &operator<<( Formatter &f, const char *sStr ); | 202 | Formatter &operator<<( Formatter &f, const char *sStr ); |
290 | Formatter &operator<<( Formatter &f, char *sStr ); | 203 | Formatter &operator<<( Formatter &f, char *sStr ); |