diff options
author | Mike Buland <eichlan@xagasoft.com> | 2009-02-10 22:26:46 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2009-02-10 22:26:46 +0000 |
commit | 292ae9453e7fdb2f1023ed9dfc99cbcd751f8b90 (patch) | |
tree | e7cdb8620f4a16f114058dd245b51a8858eacbb1 /src | |
parent | 1a0f10f17a85afef20a2610104d590fb1e41cffa (diff) | |
download | libbu++-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 '')
-rw-r--r-- | src/formatter.cpp | 197 | ||||
-rw-r--r-- | src/formatter.h | 158 | ||||
-rw-r--r-- | src/sio.cpp | 5 | ||||
-rw-r--r-- | src/sio.h | 13 | ||||
-rw-r--r-- | src/tests/stdstream.cpp | 21 |
5 files changed, 394 insertions, 0 deletions
diff --git a/src/formatter.cpp b/src/formatter.cpp new file mode 100644 index 0000000..c6ae3b7 --- /dev/null +++ b/src/formatter.cpp | |||
@@ -0,0 +1,197 @@ | |||
1 | #include "formatter.h" | ||
2 | |||
3 | Bu::Formatter::Formatter( Stream &rOut ) : | ||
4 | rOut( rOut ) | ||
5 | { | ||
6 | } | ||
7 | |||
8 | Bu::Formatter::~Formatter() | ||
9 | { | ||
10 | } | ||
11 | |||
12 | void Bu::Formatter::write( const Bu::FString &sStr ) | ||
13 | { | ||
14 | rOut.write( sStr ); | ||
15 | } | ||
16 | |||
17 | void Bu::Formatter::write( const char *sStr, int iLen ) | ||
18 | { | ||
19 | rOut.write( sStr, iLen ); | ||
20 | } | ||
21 | |||
22 | void Bu::Formatter::writeAligned( const Bu::FString &sStr ) | ||
23 | { | ||
24 | int iLen = sStr.getSize(); | ||
25 | if( iLen > fLast.uMinWidth ) | ||
26 | { | ||
27 | write( sStr ); | ||
28 | } | ||
29 | else | ||
30 | { | ||
31 | int iRem = fLast.uMinWidth - iLen; | ||
32 | switch( fLast.uAlign ) | ||
33 | { | ||
34 | case Fmt::Right: | ||
35 | for( int k = 0; k < iRem; k++ ) | ||
36 | write(" ", 1 ); | ||
37 | write( sStr ); | ||
38 | break; | ||
39 | |||
40 | case Fmt::Center: | ||
41 | { | ||
42 | int iHlf = iRem/2; | ||
43 | for( int k = 0; k < iHlf; k++ ) | ||
44 | write(" ", 1 ); | ||
45 | write( sStr ); | ||
46 | iHlf = iRem-iHlf;; | ||
47 | for( int k = 0; k < iHlf; k++ ) | ||
48 | write(" ", 1 ); | ||
49 | } | ||
50 | break; | ||
51 | |||
52 | case Fmt::Left: | ||
53 | write( sStr ); | ||
54 | for( int k = 0; k < iRem; k++ ) | ||
55 | write(" ", 1 ); | ||
56 | break; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | usedFormat(); | ||
61 | } | ||
62 | |||
63 | void Bu::Formatter::writeAligned( const char *sStr, int iLen ) | ||
64 | { | ||
65 | if( iLen > fLast.uMinWidth ) | ||
66 | { | ||
67 | write( sStr, iLen ); | ||
68 | } | ||
69 | else | ||
70 | { | ||
71 | int iRem = fLast.uMinWidth - iLen; | ||
72 | switch( fLast.uAlign ) | ||
73 | { | ||
74 | case Fmt::Right: | ||
75 | for( int k = 0; k < iRem; k++ ) | ||
76 | write(" ", 1 ); | ||
77 | write( sStr, iLen ); | ||
78 | break; | ||
79 | |||
80 | case Fmt::Center: | ||
81 | { | ||
82 | int iHlf = iRem/2; | ||
83 | for( int k = 0; k < iHlf; k++ ) | ||
84 | write(" ", 1 ); | ||
85 | write( sStr, iLen ); | ||
86 | iHlf = iRem-iHlf;; | ||
87 | for( int k = 0; k < iHlf; k++ ) | ||
88 | write(" ", 1 ); | ||
89 | } | ||
90 | break; | ||
91 | |||
92 | case Fmt::Left: | ||
93 | write( sStr, iLen ); | ||
94 | for( int k = 0; k < iRem; k++ ) | ||
95 | write(" ", 1 ); | ||
96 | break; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | usedFormat(); | ||
101 | } | ||
102 | |||
103 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Formatter::Fmt &f ) | ||
104 | { | ||
105 | rOut.setTempFormat( f ); | ||
106 | return rOut; | ||
107 | } | ||
108 | |||
109 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, Bu::Formatter::Special s ) | ||
110 | { | ||
111 | switch( s ) | ||
112 | { | ||
113 | case Formatter::nl: | ||
114 | rOut.write("\n", 1 ); | ||
115 | break; | ||
116 | } | ||
117 | return rOut; | ||
118 | } | ||
119 | |||
120 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const char *sStr ) | ||
121 | { | ||
122 | rOut.writeAligned( sStr, strlen( sStr ) ); | ||
123 | return rOut; | ||
124 | } | ||
125 | |||
126 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::FString &sStr ) | ||
127 | { | ||
128 | rOut.writeAligned( sStr ); | ||
129 | return rOut; | ||
130 | } | ||
131 | |||
132 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed char c ) | ||
133 | { | ||
134 | rOut.write( (char *)&c, 1 ); | ||
135 | return rOut; | ||
136 | } | ||
137 | |||
138 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, char c ) | ||
139 | { | ||
140 | rOut.write( (char *)&c, 1 ); | ||
141 | return rOut; | ||
142 | } | ||
143 | |||
144 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned char c ) | ||
145 | { | ||
146 | rOut.write( (char *)&c, 1 ); | ||
147 | return rOut; | ||
148 | } | ||
149 | |||
150 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed short i ) | ||
151 | { | ||
152 | rOut.ifmt<signed short>( i ); | ||
153 | return rOut; | ||
154 | } | ||
155 | |||
156 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned short i ) | ||
157 | { | ||
158 | rOut.ufmt<unsigned short>( i ); | ||
159 | return rOut; | ||
160 | } | ||
161 | |||
162 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed int i ) | ||
163 | { | ||
164 | rOut.ifmt<signed int>( i ); | ||
165 | return rOut; | ||
166 | } | ||
167 | |||
168 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned int i ) | ||
169 | { | ||
170 | rOut.ufmt<unsigned int>( i ); | ||
171 | return rOut; | ||
172 | } | ||
173 | |||
174 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long i ) | ||
175 | { | ||
176 | rOut.ifmt<signed long>( i ); | ||
177 | return rOut; | ||
178 | } | ||
179 | |||
180 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long i ) | ||
181 | { | ||
182 | rOut.ufmt<unsigned long>( i ); | ||
183 | return rOut; | ||
184 | } | ||
185 | |||
186 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long long i ) | ||
187 | { | ||
188 | rOut.ifmt<signed long long>( i ); | ||
189 | return rOut; | ||
190 | } | ||
191 | |||
192 | Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long long i ) | ||
193 | { | ||
194 | rOut.ufmt<unsigned long long>( i ); | ||
195 | return rOut; | ||
196 | } | ||
197 | |||
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 | |||
6 | namespace 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 | ||
diff --git a/src/sio.cpp b/src/sio.cpp new file mode 100644 index 0000000..2e72fd9 --- /dev/null +++ b/src/sio.cpp | |||
@@ -0,0 +1,5 @@ | |||
1 | #include "bu/sio.h" | ||
2 | |||
3 | Bu::StdStream Bu::sioRaw; | ||
4 | Bu::Formatter Bu::sio( Bu::sioRaw ); | ||
5 | |||
diff --git a/src/sio.h b/src/sio.h new file mode 100644 index 0000000..298e786 --- /dev/null +++ b/src/sio.h | |||
@@ -0,0 +1,13 @@ | |||
1 | #ifndef BU_SIO_H | ||
2 | #define BU_SIO_H | ||
3 | |||
4 | #include "bu/stdstream.h" | ||
5 | #include "bu/formatter.h" | ||
6 | |||
7 | namespace Bu | ||
8 | { | ||
9 | extern Bu::StdStream sioRaw; | ||
10 | extern Bu::Formatter sio; | ||
11 | }; | ||
12 | |||
13 | #endif | ||
diff --git a/src/tests/stdstream.cpp b/src/tests/stdstream.cpp new file mode 100644 index 0000000..d67f9b8 --- /dev/null +++ b/src/tests/stdstream.cpp | |||
@@ -0,0 +1,21 @@ | |||
1 | #include "bu/sio.h" | ||
2 | |||
3 | int main() | ||
4 | { | ||
5 | Bu::sio << "Hello there" << Bu::sio.nl; | ||
6 | |||
7 | Bu::sio << "sizeof(Fmt) = " << sizeof(Bu::Fmt) << Bu::sio.nl; | ||
8 | |||
9 | Bu::sio << -123 << ", " << 0 << ", " << 123 << Bu::sio.nl; | ||
10 | |||
11 | Bu::sio << "+----------+" << Bu::sio.nl; | ||
12 | Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Center ) << "Test" << "|" << Bu::sio.nl; | ||
13 | Bu::sio << "+----------+" << Bu::sio.nl; | ||
14 | Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Left ) << 123 << "|" << Bu::sio.nl; | ||
15 | Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Center ) << 123 << "|" << Bu::sio.nl; | ||
16 | Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Right ) << 123 << "|" << Bu::sio.nl; | ||
17 | Bu::sio << "+----------+" << Bu::sio.nl; | ||
18 | |||
19 | return 0; | ||
20 | } | ||
21 | |||