aboutsummaryrefslogtreecommitdiff
path: root/src/formatter.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-02-10 22:26:46 +0000
committerMike Buland <eichlan@xagasoft.com>2009-02-10 22:26:46 +0000
commit292ae9453e7fdb2f1023ed9dfc99cbcd751f8b90 (patch)
treee7cdb8620f4a16f114058dd245b51a8858eacbb1 /src/formatter.h
parent1a0f10f17a85afef20a2610104d590fb1e41cffa (diff)
downloadlibbu++-292ae9453e7fdb2f1023ed9dfc99cbcd751f8b90.tar.gz
libbu++-292ae9453e7fdb2f1023ed9dfc99cbcd751f8b90.tar.bz2
libbu++-292ae9453e7fdb2f1023ed9dfc99cbcd751f8b90.tar.xz
libbu++-292ae9453e7fdb2f1023ed9dfc99cbcd751f8b90.zip
Hey, got the formatter working, that's something. I really like it so far,
lets see how nice we can really make it.
Diffstat (limited to 'src/formatter.h')
-rw-r--r--src/formatter.h158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/formatter.h b/src/formatter.h
new file mode 100644
index 0000000..168f48f
--- /dev/null
+++ b/src/formatter.h
@@ -0,0 +1,158 @@
1#ifndef BU_FORMATTER_H
2#define BU_FORMATTER_H
3
4#include "stream.h"
5
6namespace Bu
7{
8 class Formatter
9 {
10 public:
11 Formatter( Stream &rOut );
12 virtual ~Formatter();
13
14 typedef struct Fmt
15 {
16 enum Alignment
17 {
18 Left = 0,
19 Center = 1,
20 Right = 2
21 };
22 Fmt() :
23 uMinWidth( 0 ),
24 uRadix( 10 ),
25 uAlign( Right ),
26 bPlus( false ),
27 bCaps( false )
28 {
29 }
30
31 Fmt( unsigned int uMinWidth, unsigned int uRadix=10,
32 Alignment a=Right, bool bPlus=false ) :
33 uMinWidth( uMinWidth ),
34 uRadix( uRadix ),
35 uAlign( a ),
36 bPlus( bPlus ),
37 bCaps( false )
38 {
39 }
40
41 unsigned int uMinWidth : 8;
42 unsigned int uRadix : 6;
43 unsigned int uAlign : 2;
44 unsigned int bPlus : 1;
45 unsigned int bCaps : 1;
46 } Fmt;
47
48 void write( const Bu::FString &sStr );
49 void write( const char *sStr, int iLen );
50 void writeAligned( const Bu::FString &sStr );
51 void writeAligned( const char *sStr, int iLen );
52
53 void setFormat( const Fmt &f )
54 {
55 fLast = f;
56 bTempFmt = false;
57 }
58
59 void setTempFormat( const Fmt &f )
60 {
61 fLast = f;
62 bTempFmt = true;
63 }
64
65 void usedFormat()
66 {
67 if( bTempFmt )
68 fLast = Fmt();
69 }
70
71 template<typename type>
72 void ifmt( type i )
73 {
74 // This code is taken from Nango, hopefully we can make it better.
75 bool bNeg = i<0;
76 char buf[sizeof(type)*8+1];
77 if( bNeg ) i = -i;
78 if( fLast.uRadix < 2 || fLast.uRadix > 36 )
79 {
80 usedFormat();
81 return;
82 }
83
84 for( int j = sizeof(type)*8; j >= 0; j-- )
85 {
86 int c = i%fLast.uRadix;
87 i /= fLast.uRadix;
88 buf[j] = (char)((c<10)?('0'+c):('A'+c-10));
89 if( i == 0 )
90 {
91 if( bNeg ) buf[--j] = '-';
92 else if( fLast.bPlus ) buf[--j] = '+';
93 writeAligned( buf+j, sizeof(type)*8-j+1 );
94
95 return;
96 }
97 }
98 usedFormat();
99 }
100
101 template<typename type>
102 void ufmt( type i )
103 {
104 // This code is taken from Nango, hopefully we can make it better.
105 char buf[sizeof(type)*8+1];
106 if( fLast.uRadix < 2 || fLast.uRadix > 36 )
107 {
108 usedFormat();
109 return;
110 }
111
112 for( int j = sizeof(type)*8; j >= 0; j-- )
113 {
114 int c = i%fLast.uRadix;
115 i /= fLast.uRadix;
116 buf[j] = (char)((c<10)?('0'+c):('A'+c-10));
117 if( i == 0 )
118 {
119 if( fLast.bPlus ) buf[--j] = '+';
120 writeAligned( buf+j, sizeof(type)*8-j+1 );
121
122 return;
123 }
124 }
125 usedFormat();
126 }
127
128 enum Special
129 {
130 nl
131 };
132
133 private:
134 Stream &rOut;
135 Fmt fLast;
136 bool bTempFmt;
137 };
138
139 typedef Formatter::Fmt Fmt;
140
141 Formatter &operator<<( Formatter &rOut, const Formatter::Fmt &f );
142 Formatter &operator<<( Formatter &rOut, Formatter::Special s );
143 Formatter &operator<<( Formatter &rOut, const char *sStr );
144 Formatter &operator<<( Formatter &rOut, const Bu::FString &sStr );
145 Formatter &operator<<( Formatter &rOut, signed char c );
146 Formatter &operator<<( Formatter &rOut, char c );
147 Formatter &operator<<( Formatter &rOut, unsigned char c );
148 Formatter &operator<<( Formatter &rOut, signed short i );
149 Formatter &operator<<( Formatter &rOut, unsigned short i );
150 Formatter &operator<<( Formatter &rOut, signed int i );
151 Formatter &operator<<( Formatter &rOut, unsigned int i );
152 Formatter &operator<<( Formatter &rOut, signed long i );
153 Formatter &operator<<( Formatter &rOut, unsigned long i );
154 Formatter &operator<<( Formatter &rOut, signed long long i );
155 Formatter &operator<<( Formatter &rOut, unsigned long long i );
156};
157
158#endif