aboutsummaryrefslogtreecommitdiff
path: root/src/formatter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/formatter.cpp')
-rw-r--r--src/formatter.cpp172
1 files changed, 104 insertions, 68 deletions
diff --git a/src/formatter.cpp b/src/formatter.cpp
index 830e527..5ab1b3f 100644
--- a/src/formatter.cpp
+++ b/src/formatter.cpp
@@ -9,8 +9,8 @@
9 9
10#include <string.h> 10#include <string.h>
11 11
12Bu::Formatter::Formatter( Stream &rOut ) : 12Bu::Formatter::Formatter( Stream &rStream ) :
13 rOut( rOut ), 13 rStream( rStream ),
14 uIndent( 0 ), 14 uIndent( 0 ),
15 cIndent( '\t' ) 15 cIndent( '\t' )
16{ 16{
@@ -22,12 +22,12 @@ Bu::Formatter::~Formatter()
22 22
23void Bu::Formatter::write( const Bu::FString &sStr ) 23void Bu::Formatter::write( const Bu::FString &sStr )
24{ 24{
25 rOut.write( sStr ); 25 rStream.write( sStr );
26} 26}
27 27
28void Bu::Formatter::write( const void *sStr, int iLen ) 28void Bu::Formatter::write( const void *sStr, int iLen )
29{ 29{
30 rOut.write( sStr, iLen ); 30 rStream.write( sStr, iLen );
31} 31}
32 32
33void Bu::Formatter::writeAligned( const Bu::FString &sStr ) 33void Bu::Formatter::writeAligned( const Bu::FString &sStr )
@@ -111,6 +111,36 @@ void Bu::Formatter::writeAligned( const char *sStr, int iLen )
111 usedFormat(); 111 usedFormat();
112} 112}
113 113
114Bu::FString Bu::Formatter::readToken()
115{
116 Bu::FString sRet;
117 for(;;)
118 {
119 char buf;
120 int iRead = rStream.read( &buf, 1 );
121 if( iRead == 0 )
122 return sRet;
123 if( buf == ' ' || buf == '\t' || buf == '\n' || buf == '\r' )
124 continue;
125 else
126 {
127 sRet += buf;
128 break;
129 }
130 }
131 for(;;)
132 {
133 char buf;
134 int iRead = rStream.read( &buf, 1 );
135 if( iRead == 0 )
136 return sRet;
137 if( buf == ' ' || buf == '\t' || buf == '\n' || buf == '\r' )
138 return sRet;
139 else
140 sRet += buf;
141 }
142}
143
114void Bu::Formatter::incIndent() 144void Bu::Formatter::incIndent()
115{ 145{
116 if( uIndent < 0xFFU ) 146 if( uIndent < 0xFFU )
@@ -192,137 +222,143 @@ Bu::Formatter::Fmt &Bu::Formatter::Fmt::caps( bool bCaps )
192 return *this; 222 return *this;
193} 223}
194 224
195Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Formatter::Fmt &f ) 225Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Formatter::Fmt &fmt )
196{ 226{
197 rOut.setTempFormat( f ); 227 f.setTempFormat( fmt );
198 return rOut; 228 return f;
199} 229}
200 230
201Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, Bu::Formatter::Special s ) 231Bu::Formatter &Bu::operator<<( Bu::Formatter &f, Bu::Formatter::Special s )
202{ 232{
203 switch( s ) 233 switch( s )
204 { 234 {
205 case Formatter::nl: 235 case Formatter::nl:
206 { 236 {
207 rOut.write("\n", 1 ); 237 f.write("\n", 1 );
208 char ci = rOut.getIndentChar(); 238 char ci = f.getIndentChar();
209 for( int j = 0; j < rOut.getIndent(); j++ ) 239 for( int j = 0; j < f.getIndent(); j++ )
210 rOut.write( &ci, 1 ); 240 f.write( &ci, 1 );
211 } 241 }
212 break; 242 break;
213 243
214 case Formatter::flush: 244 case Formatter::flush:
215 rOut.doFlush(); 245 f.doFlush();
216 break; 246 break;
217 } 247 }
218 return rOut; 248 return f;
249}
250
251Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const char *sStr )
252{
253 f.writeAligned( sStr, strlen( sStr ) );
254 return f;
219} 255}
220 256
221Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const char *sStr ) 257Bu::Formatter &Bu::operator<<( Bu::Formatter &f, char *sStr )
222{ 258{
223 rOut.writeAligned( sStr, strlen( sStr ) ); 259 f.writeAligned( sStr, strlen( sStr ) );
224 return rOut; 260 return f;
225} 261}
226 262
227Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, char *sStr ) 263Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::FString &sStr )
228{ 264{
229 rOut.writeAligned( sStr, strlen( sStr ) ); 265 f.writeAligned( sStr );
230 return rOut; 266 return f;
231} 267}
232 268
233Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::FString &sStr ) 269Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed char c )
234{ 270{
235 rOut.writeAligned( sStr ); 271 f.write( (char *)&c, 1 );
236 return rOut; 272 return f;
237} 273}
238 274
239Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed char c ) 275Bu::Formatter &Bu::operator<<( Bu::Formatter &f, char c )
240{ 276{
241 rOut.write( (char *)&c, 1 ); 277 f.write( (char *)&c, 1 );
242 return rOut; 278 return f;
243} 279}
244 280
245Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, char c ) 281Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned char c )
246{ 282{
247 rOut.write( (char *)&c, 1 ); 283 f.write( (char *)&c, 1 );
248 return rOut; 284 return f;
249} 285}
250 286
251Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned char c ) 287Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed short i )
252{ 288{
253 rOut.write( (char *)&c, 1 ); 289 f.ifmt<signed short>( i );
254 return rOut; 290 return f;
255} 291}
256 292
257Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed short i ) 293Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned short i )
258{ 294{
259 rOut.ifmt<signed short>( i ); 295 f.ufmt<unsigned short>( i );
260 return rOut; 296 return f;
261} 297}
262 298
263Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned short i ) 299Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed int i )
264{ 300{
265 rOut.ufmt<unsigned short>( i ); 301 f.ifmt<signed int>( i );
266 return rOut; 302 return f;
267} 303}
268 304
269Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed int i ) 305Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned int i )
270{ 306{
271 rOut.ifmt<signed int>( i ); 307 f.ufmt<unsigned int>( i );
272 return rOut; 308 return f;
273} 309}
274 310
275Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned int i ) 311Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed long i )
276{ 312{
277 rOut.ufmt<unsigned int>( i ); 313 f.ifmt<signed long>( i );
278 return rOut; 314 return f;
279} 315}
280 316
281Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long i ) 317Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned long i )
282{ 318{
283 rOut.ifmt<signed long>( i ); 319 f.ufmt<unsigned long>( i );
284 return rOut; 320 return f;
285} 321}
286 322
287Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long i ) 323Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed long long i )
288{ 324{
289 rOut.ufmt<unsigned long>( i ); 325 f.ifmt<signed long long>( i );
290 return rOut; 326 return f;
291} 327}
292 328
293Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long long i ) 329Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned long long i )
294{ 330{
295 rOut.ifmt<signed long long>( i ); 331 f.ufmt<unsigned long long>( i );
296 return rOut; 332 return f;
297} 333}
298 334
299Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long long i ) 335Bu::Formatter &Bu::operator<<( Bu::Formatter &f, float flt )
300{ 336{
301 rOut.ufmt<unsigned long long>( i ); 337 f.ffmt<float>( flt );
302 return rOut; 338 return f;
303} 339}
304 340
305Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, float f ) 341Bu::Formatter &Bu::operator<<( Bu::Formatter &f, double flt )
306{ 342{
307 rOut.ffmt<float>( f ); 343 f.ffmt<double>( flt );
308 return rOut; 344 return f;
309} 345}
310 346
311Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, double f ) 347Bu::Formatter &Bu::operator<<( Bu::Formatter &f, long double flt )
312{ 348{
313 rOut.ffmt<double>( f ); 349 f.ffmt<long double>( flt );
314 return rOut; 350 return f;
315} 351}
316 352
317Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, long double f ) 353Bu::Formatter &Bu::operator<<( Bu::Formatter &f, bool b )
318{ 354{
319 rOut.ffmt<long double>( f ); 355 f.writeAligned( b?("true"):("false") );
320 return rOut; 356 return f;
321} 357}
322 358
323Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, bool b ) 359Bu::Formatter &Bu::operator>>( Bu::Formatter &f, Bu::FString &sStr )
324{ 360{
325 rOut.writeAligned( b?("true"):("false") ); 361 sStr = f.readToken();
326 return rOut; 362 return f;
327} 363}
328 364