From 038815ae3a019ac56fa1c62e18c5861166d3a975 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 18 Dec 2009 15:32:37 +0000 Subject: Wow, cool, Bu::Formatter can read all the basic types now, (int, float, bool, char, etc.) and OptParser totally works. I have one last change to make to it, which is using the return value of signal type options to determine weather or not the option took a parameter at all, especially in the case of short options. --- src/formatter.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ src/formatter.h | 73 ++++++++++++++++++++++++++++++++++ src/optparser.cpp | 57 ++++++++++++++++++++++++++- src/optparser.h | 74 +++++++++++++++++++++++++++++----- src/stdstream.cpp | 1 + src/stdstream.h | 1 - src/tests/optparser.cpp | 63 +++++++++++++++++++---------- 7 files changed, 340 insertions(+), 32 deletions(-) diff --git a/src/formatter.cpp b/src/formatter.cpp index 5ab1b3f..14f70ed 100644 --- a/src/formatter.cpp +++ b/src/formatter.cpp @@ -111,6 +111,11 @@ void Bu::Formatter::writeAligned( const char *sStr, int iLen ) usedFormat(); } +void Bu::Formatter::read( void *sStr, int iLen ) +{ + rStream.read( sStr, iLen ); +} + Bu::FString Bu::Formatter::readToken() { Bu::FString sRet; @@ -362,3 +367,101 @@ Bu::Formatter &Bu::operator>>( Bu::Formatter &f, Bu::FString &sStr ) return f; } +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed char &c ) +{ + f.read( &c, 1 ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, char &c ) +{ + f.read( &c, 1 ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned char &c ) +{ + f.read( &c, 1 ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed short &i ) +{ + f.iparse( i, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned short &i ) +{ + f.uparse( i, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed int &i ) +{ + f.iparse( i, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned int &i ) +{ + f.uparse( i, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed long &i ) +{ + f.iparse( i, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned long &i ) +{ + f.uparse( i, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed long long &i ) +{ + f.iparse( i, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned long long &i ) +{ + f.uparse( i, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, float &flt ) +{ + f.fparse( flt, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, double &flt ) +{ + f.fparse( flt, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, long double &flt ) +{ + f.fparse( flt, f.readToken() ); + return f; +} + +Bu::Formatter &Bu::operator>>( Bu::Formatter &f, bool &b ) +{ + Bu::FString sStr = f.readToken(); + if( !sStr ) + return f; + char c = *sStr.begin(); + if( c == 'y' || c == 'Y' || c == 't' || c == 'T' ) + b = true; + else if( c == 'n' || c == 'N' || c == 'f' || c == 'F' ) + b = false; + + return f; +} + diff --git a/src/formatter.h b/src/formatter.h index aec5c5d..3f51e8e 100644 --- a/src/formatter.h +++ b/src/formatter.h @@ -103,6 +103,7 @@ namespace Bu void writeAligned( const Bu::FString &sStr ); void writeAligned( const char *sStr, int iLen ); + void read( void *sStr, int iLen ); Bu::FString readToken(); void incIndent(); @@ -199,6 +200,63 @@ namespace Bu writeAligned( fTmp ); usedFormat(); } + + template + void iparse( type &i, const Bu::FString &sBuf ) + { + if( !sBuf ) + return; + if( sBuf[0] != '+' && sBuf[0] != '-' && + (sBuf[0] < '0' && sBuf[0] > '9') ) + return; + int j = 1; + int iMax = sBuf.getSize(); + for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { } + i = 0; + type iPos = 1; + for(j--; j >= 0; j-- ) + { + if( sBuf[j] == '+' || sBuf[j] == '-' ) + continue; + i += (sBuf[j]-'0')*iPos; + iPos *= fLast.uRadix; + } + if( sBuf[0] == '-' ) + i = -i; + + usedFormat(); + } + + template + void uparse( type &i, const Bu::FString &sBuf ) + { + if( !sBuf ) + return; + if( sBuf[0] != '+' && + (sBuf[0] < '0' && sBuf[0] > '9') ) + return; + int j = 1; + int iMax = sBuf.getSize(); + for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { } + i = 0; + type iPos = 1; + for(j--; j >= 0; j-- ) + { + if( sBuf[j] == '+' ) + continue; + i += (sBuf[j]-'0')*iPos; + iPos *= fLast.uRadix; + } + + usedFormat(); + } + + template + void fparse( type &f, const Bu::FString &sBuf ) + { + sscanf( sBuf.getStr(), "%f", &f ); + usedFormat(); + } enum Special { @@ -243,6 +301,21 @@ namespace Bu Formatter &operator<<( Formatter &f, bool b ); Formatter &operator>>( Formatter &f, Bu::FString &sStr ); + Formatter &operator>>( Formatter &f, signed char &c ); + Formatter &operator>>( Formatter &f, char &c ); + Formatter &operator>>( Formatter &f, unsigned char &c ); + Formatter &operator>>( Formatter &f, signed short &i ); + Formatter &operator>>( Formatter &f, unsigned short &i ); + Formatter &operator>>( Formatter &f, signed int &i ); + Formatter &operator>>( Formatter &f, unsigned int &i ); + Formatter &operator>>( Formatter &f, signed long &i ); + Formatter &operator>>( Formatter &f, unsigned long &i ); + Formatter &operator>>( Formatter &f, signed long long &i ); + Formatter &operator>>( Formatter &f, unsigned long long &i ); + Formatter &operator>>( Formatter &f, float &flt ); + Formatter &operator>>( Formatter &f, double &flt ); + Formatter &operator>>( Formatter &f, long double &flt ); + Formatter &operator>>( Formatter &f, bool &b ); template Formatter &operator<<( Formatter &f, const type *p ) diff --git a/src/optparser.cpp b/src/optparser.cpp index 2a8e64b..d656e12 100644 --- a/src/optparser.cpp +++ b/src/optparser.cpp @@ -106,6 +106,14 @@ void Bu::OptParser::parse( int argc, char **argv ) ); break; } + else if( argv[j+1] ) + { + pOpt->pProxy->setValue( + argv[j+1] + ); + j++; + break; + } } } else @@ -135,6 +143,16 @@ void Bu::OptParser::addOption( const Option &opt ) hlOption.insert( opt.sOpt, &lOption.last() ); } +void Bu::OptParser::setOverride( char cOpt, const Bu::FString &sOverride ) +{ + hsOption.get( cOpt )->sOverride = sOverride; +} + +void Bu::OptParser::setOverride( const Bu::FString &sOpt, const Bu::FString &sOverride ) +{ + hlOption.get( sOpt )->sOverride = sOverride; +} + void Bu::OptParser::addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp ) { Option o; @@ -146,7 +164,19 @@ void Bu::OptParser::addHelpOption( char c, const Bu::FString &s, const Bu::FStri addOption( o ); } -int Bu::OptParser::optHelp( StrArray aParams ) +void Bu::OptParser::addHelpBanner( const Bu::FString &sText, bool bFormatted ) +{ + Banner b; + b.sText = sText; + b.bFormatted = bFormatted; + if( lOption.getSize() > 0 ) + { + for( b.iAfter = lOption.begin(); b.iAfter+1; b.iAfter++ ) { } + } + lBanner.append( b ); +} + +int Bu::OptParser::optHelp( StrArray /*aParams*/ ) { bool bHasShort = false; int iMaxWidth = 0; @@ -166,6 +196,19 @@ int Bu::OptParser::optHelp( StrArray aParams ) iIndent += 4; if( iMaxWidth > 0 ) iIndent += 4 + iMaxWidth; + + BannerList::iterator iBanner; + for( iBanner = lBanner.begin(); iBanner; iBanner++ ) + { + if( (*iBanner).iAfter ) + break; + + if( (*iBanner).bFormatted ) + sio << format( (*iBanner).sText, iScrWidth-1, 0 ); + else + sio << (*iBanner).sText; + sio << sio.nl; + } for( OptionList::iterator i = lOption.begin(); i; i++ ) { sio << " "; @@ -191,6 +234,18 @@ int Bu::OptParser::optHelp( StrArray aParams ) } sio << format( (*i).sHelp, iScrWidth-iIndent-1, iIndent ); sio << sio.nl; + + for( ; iBanner; iBanner++ ) + { + if( (*iBanner).iAfter != i ) + break; + + if( (*iBanner).bFormatted ) + sio << format( (*iBanner).sText, iScrWidth-1, 0 ); + else + sio << (*iBanner).sText; + sio << sio.nl; + } } exit( 0 ); return 0; diff --git a/src/optparser.h b/src/optparser.h index acfb35d..425bc90 100644 --- a/src/optparser.h +++ b/src/optparser.h @@ -14,7 +14,7 @@ namespace Bu typedef Bu::Array StrArray; class OptParser { - public: + private: class _ValueProxy { public: @@ -54,6 +54,7 @@ namespace Bu ptype &v; }; + public: typedef Signal1 OptionSignal; class Option { @@ -70,6 +71,21 @@ namespace Bu _ValueProxy *pProxy; Bu::FString sOverride; }; + + private: + typedef Bu::List