From 292ae9453e7fdb2f1023ed9dfc99cbcd751f8b90 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 10 Feb 2009 22:26:46 +0000 Subject: Hey, got the formatter working, that's something. I really like it so far, lets see how nice we can really make it. --- src/formatter.cpp | 197 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 src/formatter.cpp (limited to 'src/formatter.cpp') 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 @@ +#include "formatter.h" + +Bu::Formatter::Formatter( Stream &rOut ) : + rOut( rOut ) +{ +} + +Bu::Formatter::~Formatter() +{ +} + +void Bu::Formatter::write( const Bu::FString &sStr ) +{ + rOut.write( sStr ); +} + +void Bu::Formatter::write( const char *sStr, int iLen ) +{ + rOut.write( sStr, iLen ); +} + +void Bu::Formatter::writeAligned( const Bu::FString &sStr ) +{ + int iLen = sStr.getSize(); + if( iLen > fLast.uMinWidth ) + { + write( sStr ); + } + else + { + int iRem = fLast.uMinWidth - iLen; + switch( fLast.uAlign ) + { + case Fmt::Right: + for( int k = 0; k < iRem; k++ ) + write(" ", 1 ); + write( sStr ); + break; + + case Fmt::Center: + { + int iHlf = iRem/2; + for( int k = 0; k < iHlf; k++ ) + write(" ", 1 ); + write( sStr ); + iHlf = iRem-iHlf;; + for( int k = 0; k < iHlf; k++ ) + write(" ", 1 ); + } + break; + + case Fmt::Left: + write( sStr ); + for( int k = 0; k < iRem; k++ ) + write(" ", 1 ); + break; + } + } + + usedFormat(); +} + +void Bu::Formatter::writeAligned( const char *sStr, int iLen ) +{ + if( iLen > fLast.uMinWidth ) + { + write( sStr, iLen ); + } + else + { + int iRem = fLast.uMinWidth - iLen; + switch( fLast.uAlign ) + { + case Fmt::Right: + for( int k = 0; k < iRem; k++ ) + write(" ", 1 ); + write( sStr, iLen ); + break; + + case Fmt::Center: + { + int iHlf = iRem/2; + for( int k = 0; k < iHlf; k++ ) + write(" ", 1 ); + write( sStr, iLen ); + iHlf = iRem-iHlf;; + for( int k = 0; k < iHlf; k++ ) + write(" ", 1 ); + } + break; + + case Fmt::Left: + write( sStr, iLen ); + for( int k = 0; k < iRem; k++ ) + write(" ", 1 ); + break; + } + } + + usedFormat(); +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Formatter::Fmt &f ) +{ + rOut.setTempFormat( f ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, Bu::Formatter::Special s ) +{ + switch( s ) + { + case Formatter::nl: + rOut.write("\n", 1 ); + break; + } + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const char *sStr ) +{ + rOut.writeAligned( sStr, strlen( sStr ) ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::FString &sStr ) +{ + rOut.writeAligned( sStr ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed char c ) +{ + rOut.write( (char *)&c, 1 ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, char c ) +{ + rOut.write( (char *)&c, 1 ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned char c ) +{ + rOut.write( (char *)&c, 1 ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed short i ) +{ + rOut.ifmt( i ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned short i ) +{ + rOut.ufmt( i ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed int i ) +{ + rOut.ifmt( i ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned int i ) +{ + rOut.ufmt( i ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long i ) +{ + rOut.ifmt( i ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long i ) +{ + rOut.ufmt( i ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, signed long long i ) +{ + rOut.ifmt( i ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long long i ) +{ + rOut.ufmt( i ); + return rOut; +} + -- cgit v1.2.3