From 3f958097632256329cdbaf2219e2ba15325e9c52 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 11 Feb 2009 22:51:25 +0000 Subject: Hey, formatter, awesome, and look at that...I'm adding uuid support. --- src/formatter.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++------ src/formatter.h | 71 +++++++++++++++++++++++++++++++++++++++------ src/tests/stdstream.cpp | 31 +++++++++++++------- src/uuid.cpp | 18 ++++++++++++ src/uuid.h | 25 ++++++++++++++++ 5 files changed, 195 insertions(+), 26 deletions(-) create mode 100644 src/uuid.cpp create mode 100644 src/uuid.h (limited to 'src') diff --git a/src/formatter.cpp b/src/formatter.cpp index c6ae3b7..1ec3c39 100644 --- a/src/formatter.cpp +++ b/src/formatter.cpp @@ -33,7 +33,7 @@ void Bu::Formatter::writeAligned( const Bu::FString &sStr ) { case Fmt::Right: for( int k = 0; k < iRem; k++ ) - write(" ", 1 ); + write( &fLast.cFillChar, 1 ); write( sStr ); break; @@ -41,18 +41,18 @@ void Bu::Formatter::writeAligned( const Bu::FString &sStr ) { int iHlf = iRem/2; for( int k = 0; k < iHlf; k++ ) - write(" ", 1 ); + write( &fLast.cFillChar, 1 ); write( sStr ); iHlf = iRem-iHlf;; for( int k = 0; k < iHlf; k++ ) - write(" ", 1 ); + write( &fLast.cFillChar, 1 ); } break; case Fmt::Left: write( sStr ); for( int k = 0; k < iRem; k++ ) - write(" ", 1 ); + write( &fLast.cFillChar, 1 ); break; } } @@ -73,7 +73,7 @@ void Bu::Formatter::writeAligned( const char *sStr, int iLen ) { case Fmt::Right: for( int k = 0; k < iRem; k++ ) - write(" ", 1 ); + write( &fLast.cFillChar, 1 ); write( sStr, iLen ); break; @@ -81,18 +81,18 @@ void Bu::Formatter::writeAligned( const char *sStr, int iLen ) { int iHlf = iRem/2; for( int k = 0; k < iHlf; k++ ) - write(" ", 1 ); + write( &fLast.cFillChar, 1 ); write( sStr, iLen ); iHlf = iRem-iHlf;; for( int k = 0; k < iHlf; k++ ) - write(" ", 1 ); + write( &fLast.cFillChar, 1 ); } break; case Fmt::Left: write( sStr, iLen ); for( int k = 0; k < iRem; k++ ) - write(" ", 1 ); + write( &fLast.cFillChar, 1 ); break; } } @@ -100,6 +100,42 @@ void Bu::Formatter::writeAligned( const char *sStr, int iLen ) usedFormat(); } +Bu::Formatter::Fmt &Bu::Formatter::Fmt::width( unsigned int uWidth ) +{ + this->uMinWidth = uWidth; + return *this; +} + +Bu::Formatter::Fmt &Bu::Formatter::Fmt::fill( char cFill ) +{ + this->cFillChar = (unsigned char)cFill; + return *this; +} + +Bu::Formatter::Fmt &Bu::Formatter::Fmt::radix( unsigned int uRadix ) +{ + this->uRadix = uRadix; + return *this; +} + +Bu::Formatter::Fmt &Bu::Formatter::Fmt::align( Alignment eAlign ) +{ + this->uAlign = eAlign; + return *this; +} + +Bu::Formatter::Fmt &Bu::Formatter::Fmt::plus( bool bPlus ) +{ + this->bPlus = bPlus; + return *this; +} + +Bu::Formatter::Fmt &Bu::Formatter::Fmt::caps( bool bCaps ) +{ + this->bCaps = bCaps; + return *this; +} + Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Formatter::Fmt &f ) { rOut.setTempFormat( f ); @@ -195,3 +231,27 @@ Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, unsigned long long i ) return rOut; } +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, float f ) +{ + rOut.ffmt( f ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, double f ) +{ + rOut.ffmt( f ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, long double f ) +{ + rOut.ffmt( f ); + return rOut; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, bool b ) +{ + rOut.writeAligned( b?("true"):("false") ); + return rOut; +} + diff --git a/src/formatter.h b/src/formatter.h index 168f48f..4bab505 100644 --- a/src/formatter.h +++ b/src/formatter.h @@ -21,28 +21,65 @@ namespace Bu }; Fmt() : uMinWidth( 0 ), + cFillChar(' '), uRadix( 10 ), uAlign( Right ), bPlus( false ), - bCaps( false ) + bCaps( true ) { } Fmt( unsigned int uMinWidth, unsigned int uRadix=10, - Alignment a=Right, bool bPlus=false ) : + Alignment a=Right, bool bPlus=false, bool bCaps=true, + char cFill=' ') : uMinWidth( uMinWidth ), + cFillChar(cFill), uRadix( uRadix ), uAlign( a ), bPlus( bPlus ), - bCaps( false ) + bCaps( bCaps ) { } + Fmt( unsigned int uMinWidth, Alignment a=Right, + unsigned int uRadix=10, bool bPlus=false, bool bCaps=true, + char cFill=' ') : + uMinWidth( uMinWidth ), + cFillChar(cFill), + uRadix( uRadix ), + uAlign( a ), + bPlus( bPlus ), + bCaps( bCaps ) + { + } + + static Fmt hex( unsigned int uWidth=0, bool bCaps=true ) + { + return Fmt( uWidth, 16, Right, false, bCaps, '0' ); + } + + static Fmt oct( unsigned int uWidth=0, bool bCaps=true ) + { + return Fmt( uWidth, 8, Right, false, bCaps, '0' ); + } - unsigned int uMinWidth : 8; - unsigned int uRadix : 6; - unsigned int uAlign : 2; - unsigned int bPlus : 1; - unsigned int bCaps : 1; + static Fmt ptr( bool bCaps=true ) + { + return Fmt( sizeof(ptrdiff_t)*2, 16, Right, false, bCaps, '0' ); + } + + Fmt &width( unsigned int uWidth ); + Fmt &fill( char cFill='0' ); + Fmt &radix( unsigned int uRadix ); + Fmt &align( Alignment eAlign ); + Fmt &plus( bool bPlus=true ); + Fmt &caps( bool bCaps=true ); + + unsigned char uMinWidth; + char cFillChar; + unsigned short uRadix : 6; + unsigned short uAlign : 2; + unsigned short bPlus : 1; + unsigned short bCaps : 1; } Fmt; void write( const Bu::FString &sStr ); @@ -125,6 +162,13 @@ namespace Bu usedFormat(); } + template + void ffmt( type /*f*/ ) + { + writeAligned("**make floats work**"); + usedFormat(); + } + enum Special { nl @@ -153,6 +197,17 @@ namespace Bu Formatter &operator<<( Formatter &rOut, unsigned long i ); Formatter &operator<<( Formatter &rOut, signed long long i ); Formatter &operator<<( Formatter &rOut, unsigned long long i ); + Formatter &operator<<( Formatter &rOut, float f ); + Formatter &operator<<( Formatter &rOut, double f ); + Formatter &operator<<( Formatter &rOut, long double f ); + Formatter &operator<<( Formatter &rOut, bool b ); + + template + Formatter &operator<<( Formatter &rOut, type *p ) + { + rOut << (ptrdiff_t)(p); + return rOut; + } }; #endif diff --git a/src/tests/stdstream.cpp b/src/tests/stdstream.cpp index d67f9b8..cb22e22 100644 --- a/src/tests/stdstream.cpp +++ b/src/tests/stdstream.cpp @@ -1,20 +1,31 @@ #include "bu/sio.h" +using Bu::sio; +using Bu::Fmt; + int main() { - Bu::sio << "Hello there" << Bu::sio.nl; + sio << "Hello there" << sio.nl; + + sio << "sizeof(Fmt) = " << sizeof(Fmt) << sio.nl; + + sio << -123 << ", " << 0 << ", " << 123 << sio.nl; + + sio << "+----------+" << sio.nl; + sio << "|" << Fmt( 10, 10, Fmt::Center ) << "Test" << "|" << sio.nl; + sio << "+----------+" << sio.nl; + sio << "|" << Fmt( 10, 10, Fmt::Left ) << 123 << "|" << sio.nl; + sio << "|" << Fmt( 10, 10, Fmt::Center ) << 123 << "|" << sio.nl; + sio << "|" << Fmt( 10, 10, Fmt::Right ) << 123 << "|" << sio.nl; + sio << "+----------+" << sio.nl; - Bu::sio << "sizeof(Fmt) = " << sizeof(Bu::Fmt) << Bu::sio.nl; + sio << Fmt(10,Fmt::Left) << "Hexcode:" << Fmt::ptr() << (&sio) << sio.nl; - Bu::sio << -123 << ", " << 0 << ", " << 123 << Bu::sio.nl; + sio << 0.123 << sio.nl; + sio << true << " and then " << false << 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; + //for( int j = 2; j <= 36; j++ ) + // sio << "radix(" << j << ") = " << Fmt().radix( j ) << 255 << sio.nl; return 0; } diff --git a/src/uuid.cpp b/src/uuid.cpp new file mode 100644 index 0000000..8a30e2a --- /dev/null +++ b/src/uuid.cpp @@ -0,0 +1,18 @@ +#include "bu/uuid.h" + +Bu::Uuid::Uuid() +{ + clear(); +} + +Bu::Uuid::~Uuid() +{ +} + +#define msb( i ) (1<<(7-i)) + +void Bu::Uuid::clear() +{ + data[7] = msb(0); +} + diff --git a/src/uuid.h b/src/uuid.h new file mode 100644 index 0000000..2bb1404 --- /dev/null +++ b/src/uuid.h @@ -0,0 +1,25 @@ +#ifndef BU_UUID_H +#define BU_UUID_H + +namespace Bu +{ + class Uuid + { + public: + Uuid(); + virtual ~Uuid(); + + static Uuid genV1(); + static Uuid genV2(); + static Uuid genV3(); + static Uuid genV4(); + static Uuid genV5(); + + void clear(); + + private: + unsigned char data[8]; + }; +}; + +#endif -- cgit v1.2.3