aboutsummaryrefslogtreecommitdiff
path: root/src/formatter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/formatter.cpp')
-rw-r--r--src/formatter.cpp547
1 files changed, 0 insertions, 547 deletions
diff --git a/src/formatter.cpp b/src/formatter.cpp
deleted file mode 100644
index f275d71..0000000
--- a/src/formatter.cpp
+++ /dev/null
@@ -1,547 +0,0 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/formatter.h"
9
10#include "bu/stream.h"
11#include <string.h>
12
13template<> float Bu::tlog( float x )
14{
15 return logf( x );
16}
17
18template<> double Bu::tlog( double x )
19{
20 return log( x );
21}
22
23template<> long double Bu::tlog( long double x )
24{
25 return logl( x );
26}
27
28template<> float Bu::tfloor( float x )
29{
30 return floorf( x );
31}
32
33template<> double Bu::tfloor( double x )
34{
35 return floor( x );
36}
37
38template<> long double Bu::tfloor( long double x )
39{
40 return floorl( x );
41}
42
43template<> float Bu::tpow( float x, float y )
44{
45 return powf( x, y );
46}
47
48template<> double Bu::tpow( double x, double y )
49{
50 return pow( x, y );
51}
52
53template<> long double Bu::tpow( long double x, long double y )
54{
55 return powl( x, y );
56}
57
58Bu::Formatter::Formatter( Stream &rStream ) :
59 rStream( rStream ),
60 bTempFmt( false ),
61 uIndent( 0 ),
62 cIndent( '\t' )
63{
64}
65
66Bu::Formatter::~Formatter()
67{
68}
69
70void Bu::Formatter::write( const Bu::String &sStr )
71{
72 rStream.write( sStr );
73}
74
75void Bu::Formatter::write( const void *sStr, int iLen )
76{
77 rStream.write( sStr, iLen );
78}
79
80void Bu::Formatter::writeAligned( const Bu::String &sStr )
81{
82 int iLen = sStr.getSize();
83 if( iLen > fLast.uMinWidth )
84 {
85 write( sStr );
86 }
87 else
88 {
89 int iRem = fLast.uMinWidth - iLen;
90 switch( fLast.uAlign )
91 {
92 case Fmt::Right:
93 for( int k = 0; k < iRem; k++ )
94 write( &fLast.cFillChar, 1 );
95 write( sStr );
96 break;
97
98 case Fmt::Center:
99 {
100 int iHlf = iRem/2;
101 for( int k = 0; k < iHlf; k++ )
102 write( &fLast.cFillChar, 1 );
103 write( sStr );
104 iHlf = iRem-iHlf;;
105 for( int k = 0; k < iHlf; k++ )
106 write( &fLast.cFillChar, 1 );
107 }
108 break;
109
110 case Fmt::Left:
111 write( sStr );
112 for( int k = 0; k < iRem; k++ )
113 write( &fLast.cFillChar, 1 );
114 break;
115 }
116 }
117
118 usedFormat();
119}
120
121void Bu::Formatter::writeAligned( const char *sStr, int iLen )
122{
123 if( iLen > fLast.uMinWidth )
124 {
125 write( sStr, iLen );
126 }
127 else
128 {
129 int iRem = fLast.uMinWidth - iLen;
130 switch( fLast.uAlign )
131 {
132 case Fmt::Right:
133 for( int k = 0; k < iRem; k++ )
134 write( &fLast.cFillChar, 1 );
135 write( sStr, iLen );
136 break;
137
138 case Fmt::Center:
139 {
140 int iHlf = iRem/2;
141 for( int k = 0; k < iHlf; k++ )
142 write( &fLast.cFillChar, 1 );
143 write( sStr, iLen );
144 iHlf = iRem-iHlf;;
145 for( int k = 0; k < iHlf; k++ )
146 write( &fLast.cFillChar, 1 );
147 }
148 break;
149
150 case Fmt::Left:
151 write( sStr, iLen );
152 for( int k = 0; k < iRem; k++ )
153 write( &fLast.cFillChar, 1 );
154 break;
155 }
156 }
157
158 usedFormat();
159}
160
161void Bu::Formatter::read( void *sStr, int iLen )
162{
163 rStream.read( sStr, iLen );
164}
165
166Bu::String Bu::Formatter::readToken()
167{
168 Bu::String sRet;
169 if( fLast.bTokenize )
170 {
171 for(;;)
172 {
173 char buf;
174 int iRead = rStream.read( &buf, 1 );
175 if( iRead == 0 )
176 return sRet;
177 if( buf == ' ' || buf == '\t' || buf == '\n' || buf == '\r' )
178 continue;
179 else
180 {
181 sRet += buf;
182 break;
183 }
184 }
185 for(;;)
186 {
187 char buf;
188 int iRead = rStream.read( &buf, 1 );
189 if( iRead == 0 )
190 return sRet;
191 if( buf == ' ' || buf == '\t' || buf == '\n' || buf == '\r' )
192 return sRet;
193 else
194 sRet += buf;
195 }
196 }
197 else
198 {
199 for(;;)
200 {
201 char buf;
202 int iRead = rStream.read( &buf, 1 );
203 if( iRead == 0 )
204 return sRet;
205 else
206 sRet += buf;
207 }
208 }
209}
210
211void Bu::Formatter::incIndent()
212{
213 if( uIndent < 0xFFU )
214 uIndent++;
215}
216
217void Bu::Formatter::decIndent()
218{
219 if( uIndent > 0 )
220 uIndent--;
221}
222
223void Bu::Formatter::setIndent( uint8_t uLevel )
224{
225 uIndent = uLevel;
226}
227
228void Bu::Formatter::clearIndent()
229{
230 uIndent = 0;
231}
232
233void Bu::Formatter::setIndentChar( char cIndent )
234{
235 this->cIndent = cIndent;
236}
237
238void Bu::Formatter::doFlush()
239{
240 rStream.flush();
241}
242
243Bu::Fmt &Bu::Fmt::width( unsigned int uWidth )
244{
245 this->uMinWidth = uWidth;
246 return *this;
247}
248
249Bu::Fmt &Bu::Fmt::fill( char cFill )
250{
251 this->cFillChar = (unsigned char)cFill;
252 return *this;
253}
254
255Bu::Fmt &Bu::Fmt::radix( unsigned int uRadix )
256{
257 this->uRadix = uRadix;
258 return *this;
259}
260
261Bu::Fmt &Bu::Fmt::align( Alignment eAlign )
262{
263 this->uAlign = eAlign;
264 return *this;
265}
266
267Bu::Fmt &Bu::Fmt::left()
268{
269 this->uAlign = Fmt::Left;
270 return *this;
271}
272
273Bu::Fmt &Bu::Fmt::center()
274{
275 this->uAlign = Fmt::Center;
276 return *this;
277}
278
279Bu::Fmt &Bu::Fmt::right()
280{
281 this->uAlign = Fmt::Right;
282 return *this;
283}
284
285Bu::Fmt &Bu::Fmt::plus( bool bPlus )
286{
287 this->bPlus = bPlus;
288 return *this;
289}
290
291Bu::Fmt &Bu::Fmt::caps( bool bCaps )
292{
293 this->bCaps = bCaps;
294 return *this;
295}
296
297Bu::Fmt &Bu::Fmt::tokenize( bool bTokenize )
298{
299 this->bTokenize = bTokenize;
300 return *this;
301}
302
303Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Fmt &fmt )
304{
305 f.setTempFormat( fmt );
306 return f;
307}
308
309Bu::Formatter &Bu::operator<<( Bu::Formatter &f, Bu::Formatter::Special s )
310{
311 switch( s )
312 {
313 case Formatter::nl:
314 {
315#ifdef WIN32
316 f.write("\r\n", 2 );
317#else
318 f.write("\n", 1 );
319#endif
320 char ci = f.getIndentChar();
321 for( int j = 0; j < f.getIndent(); j++ )
322 f.write( &ci, 1 );
323 f.doFlush();
324 }
325 break;
326
327 case Formatter::flush:
328 f.doFlush();
329 break;
330 }
331 return f;
332}
333
334Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const char *sStr )
335{
336 f.writeAligned( sStr, strlen( sStr ) );
337 return f;
338}
339
340Bu::Formatter &Bu::operator<<( Bu::Formatter &f, char *sStr )
341{
342 f.writeAligned( sStr, strlen( sStr ) );
343 return f;
344}
345
346Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::String &sStr )
347{
348 f.writeAligned( sStr );
349 return f;
350}
351
352Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed char c )
353{
354 f.ifmt<signed char>( c );
355 //f.write( (char *)&c, 1 );
356 return f;
357}
358
359Bu::Formatter &Bu::operator<<( Bu::Formatter &f, char c )
360{
361 f.write( (char *)&c, 1 );
362 return f;
363}
364
365Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned char c )
366{
367 f.ufmt<unsigned char>( c );
368 //f.write( (char *)&c, 1 );
369 return f;
370}
371
372Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed short i )
373{
374 f.ifmt<signed short>( i );
375 return f;
376}
377
378Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned short i )
379{
380 f.ufmt<unsigned short>( i );
381 return f;
382}
383
384Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed int i )
385{
386 f.ifmt<signed int>( i );
387 return f;
388}
389
390Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned int i )
391{
392 f.ufmt<unsigned int>( i );
393 return f;
394}
395
396Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed long i )
397{
398 f.ifmt<signed long>( i );
399 return f;
400}
401
402Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned long i )
403{
404 f.ufmt<unsigned long>( i );
405 return f;
406}
407
408Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed long long i )
409{
410 f.ifmt<signed long long>( i );
411 return f;
412}
413
414Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned long long i )
415{
416 f.ufmt<unsigned long long>( i );
417 return f;
418}
419
420Bu::Formatter &Bu::operator<<( Bu::Formatter &f, float flt )
421{
422 f.ffmt<float>( flt );
423 return f;
424}
425
426Bu::Formatter &Bu::operator<<( Bu::Formatter &f, double flt )
427{
428 f.ffmt<double>( flt );
429 return f;
430}
431
432Bu::Formatter &Bu::operator<<( Bu::Formatter &f, long double flt )
433{
434 f.ffmt<long double>( flt );
435 return f;
436}
437
438Bu::Formatter &Bu::operator<<( Bu::Formatter &f, bool b )
439{
440 f.writeAligned( b?("true"):("false") );
441 return f;
442}
443
444Bu::Formatter &Bu::operator>>( Bu::Formatter &f, Bu::String &sStr )
445{
446 sStr = f.readToken();
447 return f;
448}
449
450Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed char &c )
451{
452 f.read( &c, 1 );
453 return f;
454}
455
456Bu::Formatter &Bu::operator>>( Bu::Formatter &f, char &c )
457{
458 f.read( &c, 1 );
459 return f;
460}
461
462Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned char &c )
463{
464 f.read( &c, 1 );
465 return f;
466}
467
468Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed short &i )
469{
470 f.iparse( i, f.readToken() );
471 return f;
472}
473
474Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned short &i )
475{
476 f.uparse( i, f.readToken() );
477 return f;
478}
479
480Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed int &i )
481{
482 f.iparse( i, f.readToken() );
483 return f;
484}
485
486Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned int &i )
487{
488 f.uparse( i, f.readToken() );
489 return f;
490}
491
492Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed long &i )
493{
494 f.iparse( i, f.readToken() );
495 return f;
496}
497
498Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned long &i )
499{
500 f.uparse( i, f.readToken() );
501 return f;
502}
503
504Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed long long &i )
505{
506 f.iparse( i, f.readToken() );
507 return f;
508}
509
510Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned long long &i )
511{
512 f.uparse( i, f.readToken() );
513 return f;
514}
515
516Bu::Formatter &Bu::operator>>( Bu::Formatter &f, float &flt )
517{
518 f.fparse( flt, f.readToken() );
519 return f;
520}
521
522Bu::Formatter &Bu::operator>>( Bu::Formatter &f, double &flt )
523{
524 f.fparse( flt, f.readToken() );
525 return f;
526}
527
528Bu::Formatter &Bu::operator>>( Bu::Formatter &f, long double &flt )
529{
530 f.fparse( flt, f.readToken() );
531 return f;
532}
533
534Bu::Formatter &Bu::operator>>( Bu::Formatter &f, bool &b )
535{
536 Bu::String sStr = f.readToken();
537 if( !sStr.isSet() )
538 return f;
539 char c = *sStr.begin();
540 if( c == 'y' || c == 'Y' || c == 't' || c == 'T' )
541 b = true;
542 else if( c == 'n' || c == 'N' || c == 'f' || c == 'F' )
543 b = false;
544
545 return f;
546}
547