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 ++++++++++++++++++++++++++++++++++++++++++++++++ src/formatter.h | 158 ++++++++++++++++++++++++++++++++++++++ src/sio.cpp | 5 ++ src/sio.h | 13 ++++ src/tests/stdstream.cpp | 21 ++++++ 5 files changed, 394 insertions(+) create mode 100644 src/formatter.cpp create mode 100644 src/formatter.h create mode 100644 src/sio.cpp create mode 100644 src/sio.h create mode 100644 src/tests/stdstream.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; +} + 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 @@ +#ifndef BU_FORMATTER_H +#define BU_FORMATTER_H + +#include "stream.h" + +namespace Bu +{ + class Formatter + { + public: + Formatter( Stream &rOut ); + virtual ~Formatter(); + + typedef struct Fmt + { + enum Alignment + { + Left = 0, + Center = 1, + Right = 2 + }; + Fmt() : + uMinWidth( 0 ), + uRadix( 10 ), + uAlign( Right ), + bPlus( false ), + bCaps( false ) + { + } + + Fmt( unsigned int uMinWidth, unsigned int uRadix=10, + Alignment a=Right, bool bPlus=false ) : + uMinWidth( uMinWidth ), + uRadix( uRadix ), + uAlign( a ), + bPlus( bPlus ), + bCaps( false ) + { + } + + unsigned int uMinWidth : 8; + unsigned int uRadix : 6; + unsigned int uAlign : 2; + unsigned int bPlus : 1; + unsigned int bCaps : 1; + } Fmt; + + void write( const Bu::FString &sStr ); + void write( const char *sStr, int iLen ); + void writeAligned( const Bu::FString &sStr ); + void writeAligned( const char *sStr, int iLen ); + + void setFormat( const Fmt &f ) + { + fLast = f; + bTempFmt = false; + } + + void setTempFormat( const Fmt &f ) + { + fLast = f; + bTempFmt = true; + } + + void usedFormat() + { + if( bTempFmt ) + fLast = Fmt(); + } + + template + void ifmt( type i ) + { + // This code is taken from Nango, hopefully we can make it better. + bool bNeg = i<0; + char buf[sizeof(type)*8+1]; + if( bNeg ) i = -i; + if( fLast.uRadix < 2 || fLast.uRadix > 36 ) + { + usedFormat(); + return; + } + + for( int j = sizeof(type)*8; j >= 0; j-- ) + { + int c = i%fLast.uRadix; + i /= fLast.uRadix; + buf[j] = (char)((c<10)?('0'+c):('A'+c-10)); + if( i == 0 ) + { + if( bNeg ) buf[--j] = '-'; + else if( fLast.bPlus ) buf[--j] = '+'; + writeAligned( buf+j, sizeof(type)*8-j+1 ); + + return; + } + } + usedFormat(); + } + + template + void ufmt( type i ) + { + // This code is taken from Nango, hopefully we can make it better. + char buf[sizeof(type)*8+1]; + if( fLast.uRadix < 2 || fLast.uRadix > 36 ) + { + usedFormat(); + return; + } + + for( int j = sizeof(type)*8; j >= 0; j-- ) + { + int c = i%fLast.uRadix; + i /= fLast.uRadix; + buf[j] = (char)((c<10)?('0'+c):('A'+c-10)); + if( i == 0 ) + { + if( fLast.bPlus ) buf[--j] = '+'; + writeAligned( buf+j, sizeof(type)*8-j+1 ); + + return; + } + } + usedFormat(); + } + + enum Special + { + nl + }; + + private: + Stream &rOut; + Fmt fLast; + bool bTempFmt; + }; + + typedef Formatter::Fmt Fmt; + + Formatter &operator<<( Formatter &rOut, const Formatter::Fmt &f ); + Formatter &operator<<( Formatter &rOut, Formatter::Special s ); + Formatter &operator<<( Formatter &rOut, const char *sStr ); + Formatter &operator<<( Formatter &rOut, const Bu::FString &sStr ); + Formatter &operator<<( Formatter &rOut, signed char c ); + Formatter &operator<<( Formatter &rOut, char c ); + Formatter &operator<<( Formatter &rOut, unsigned char c ); + Formatter &operator<<( Formatter &rOut, signed short i ); + Formatter &operator<<( Formatter &rOut, unsigned short i ); + Formatter &operator<<( Formatter &rOut, signed int i ); + Formatter &operator<<( Formatter &rOut, unsigned int i ); + Formatter &operator<<( Formatter &rOut, signed long i ); + Formatter &operator<<( Formatter &rOut, unsigned long i ); + Formatter &operator<<( Formatter &rOut, signed long long i ); + Formatter &operator<<( Formatter &rOut, unsigned long long i ); +}; + +#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 @@ +#include "bu/sio.h" + +Bu::StdStream Bu::sioRaw; +Bu::Formatter Bu::sio( Bu::sioRaw ); + 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 @@ +#ifndef BU_SIO_H +#define BU_SIO_H + +#include "bu/stdstream.h" +#include "bu/formatter.h" + +namespace Bu +{ + extern Bu::StdStream sioRaw; + extern Bu::Formatter sio; +}; + +#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 @@ +#include "bu/sio.h" + +int main() +{ + Bu::sio << "Hello there" << Bu::sio.nl; + + Bu::sio << "sizeof(Fmt) = " << sizeof(Bu::Fmt) << Bu::sio.nl; + + Bu::sio << -123 << ", " << 0 << ", " << 123 << Bu::sio.nl; + + Bu::sio << "+----------+" << Bu::sio.nl; + Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Center ) << "Test" << "|" << Bu::sio.nl; + Bu::sio << "+----------+" << Bu::sio.nl; + Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Left ) << 123 << "|" << Bu::sio.nl; + Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Center ) << 123 << "|" << Bu::sio.nl; + Bu::sio << "|" << Bu::Fmt( 10, 10, Bu::Fmt::Right ) << 123 << "|" << Bu::sio.nl; + Bu::sio << "+----------+" << Bu::sio.nl; + + return 0; +} + -- cgit v1.2.3