summaryrefslogtreecommitdiff
path: root/src/formatter.cpp
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.cpp
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.cpp')
-rw-r--r--src/formatter.cpp197
1 files changed, 197 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
3Bu::Formatter::Formatter( Stream &rOut ) :
4 rOut( rOut )
5{
6}
7
8Bu::Formatter::~Formatter()
9{
10}
11
12void Bu::Formatter::write( const Bu::FString &sStr )
13{
14 rOut.write( sStr );
15}
16
17void Bu::Formatter::write( const char *sStr, int iLen )
18{
19 rOut.write( sStr, iLen );
20}
21
22void 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
63void 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
103Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Formatter::Fmt &f )
104{
105 rOut.setTempFormat( f );
106 return rOut;
107}
108
109Bu::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
120Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const char *sStr )
121{
122 rOut.writeAligned( sStr, strlen( sStr ) );
123 return rOut;
124}
125
126Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::FString &sStr )
127{
128 rOut.writeAligned( sStr );
129 return rOut;
130}
131
132Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed char c )
133{
134 rOut.write( (char *)&c, 1 );
135 return rOut;
136}
137
138Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, char c )
139{
140 rOut.write( (char *)&c, 1 );
141 return rOut;
142}
143
144Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned char c )
145{
146 rOut.write( (char *)&c, 1 );
147 return rOut;
148}
149
150Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed short i )
151{
152 rOut.ifmt<signed short>( i );
153 return rOut;
154}
155
156Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned short i )
157{
158 rOut.ufmt<unsigned short>( i );
159 return rOut;
160}
161
162Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed int i )
163{
164 rOut.ifmt<signed int>( i );
165 return rOut;
166}
167
168Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned int i )
169{
170 rOut.ufmt<unsigned int>( i );
171 return rOut;
172}
173
174Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long i )
175{
176 rOut.ifmt<signed long>( i );
177 return rOut;
178}
179
180Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long i )
181{
182 rOut.ufmt<unsigned long>( i );
183 return rOut;
184}
185
186Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long long i )
187{
188 rOut.ifmt<signed long long>( i );
189 return rOut;
190}
191
192Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long long i )
193{
194 rOut.ufmt<unsigned long long>( i );
195 return rOut;
196}
197