From 0d3d73fb0cacd3d1cf7eb8b83ba87f8b740b871a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 16 Dec 2009 23:54:13 +0000 Subject: Signals is even safer and works even better. Also, OptParser is nearly done. Now I just have to come up with a way to modify data that you already have, that sure was a nice feature of the old one, even if it was implemented in a silly way. --- src/optparser.cpp | 225 +++++++++++++++++++++++++++++++++++++++++++++++- src/optparser.h | 17 ++-- src/signals.h | 12 +-- src/tests/optparser.cpp | 25 +++++- src/tests/signals.cpp | 39 ++++++++- 5 files changed, 300 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/optparser.cpp b/src/optparser.cpp index 2046792..f99dd85 100644 --- a/src/optparser.cpp +++ b/src/optparser.cpp @@ -1,4 +1,8 @@ #include "bu/optparser.h" +#include "bu/sio.h" +using namespace Bu; + +#include Bu::OptParser::OptParser() { @@ -8,6 +12,81 @@ Bu::OptParser::~OptParser() { } +void Bu::OptParser::parse( int argc, char **argv ) +{ + for( int j = 1; j < argc; j++ ) + { + if( argv[j][0] == '-' ) + { + // Now we're on to something, which kind is it? + if( argv[j][1] == '-' ) + { + // Long param, cool, that's easy, first search for = + int iEPos; + for( iEPos = 2; argv[j][iEPos] != '\0' && + argv[j][iEPos] != '='; iEPos++ ) { } + + Bu::FString sOpt; + int iCount = argc-j; + if( argv[j][iEPos] == '=' ) + { + sOpt.set( argv[j]+2, iEPos-2 ); + iCount++; + } + else + { + sOpt.set( argv[j]+2 ); + } + Option *pOpt = hlOption.get( sOpt ); + Bu::StrArray aParams( iCount ); + aParams.append( sOpt ); + if( argv[j][iEPos] == '=' ) + { + aParams.append( argv[j]+iEPos+1 ); + } + for( int k = j+1; k < argc; k++ ) + { + aParams.append( argv[k] ); + } + if( pOpt->sUsed ) + j += pOpt->sUsed( aParams ); + } + else + { + int iCPos; + for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) + { + Option *pOpt = hsOption.get( argv[j][iCPos] ); + Bu::StrArray aParams( argc-j+1 ); + char buf[2] = {argv[j][iCPos], '\0'}; + aParams.append( buf ); + if( pOpt->bShortHasParams ) + { + if( argv[j][iCPos+1] != '\0' ) + aParams.append( argv[j]+iCPos+1 ); + for( int k = j+1; k < argc; k++ ) + { + aParams.append( argv[k] ); + } + if( pOpt->sUsed ) + { + j += pOpt->sUsed( aParams ); + } + break; + } + else + { + pOpt->sUsed( aParams ); + } + } + } + } + else + { + } + } +} + void Bu::OptParser::addOption( const Option &opt ) { lOption.append( opt ); @@ -15,13 +94,155 @@ void Bu::OptParser::addOption( const Option &opt ) hsOption.insert( opt.cOpt, &lOption.last() ); if( opt.sOpt ) hlOption.insert( opt.sOpt, &lOption.last() ); +} +void Bu::OptParser::addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp ) +{ + Option o; + o.sUsed = slot( this, &OptParser::optHelp ); + o.cOpt = c; + o.sOpt = s; + o.sHelp = sHelp; + o.bShortHasParams = false; + addOption( o ); +} + +int Bu::OptParser::optHelp( StrArray aParams ) +{ + bool bHasShort = false; + int iMaxWidth = 0; + int iScrWidth = 80; + char *env = getenv("COLUMNS"); + if( env ) + iScrWidth = strtol( env, NULL, 10 ); + for( OptionList::iterator i = lOption.begin(); i; i++ ) + { + if( (*i).cOpt != '\0' ) + bHasShort = true; + if( (*i).sOpt && iMaxWidth < (*i).sOpt.getSize() ) + iMaxWidth = (*i).sOpt.getSize(); + } + int iIndent = 4; + if( bHasShort ) + iIndent += 4; + if( iMaxWidth > 0 ) + iIndent += 4 + iMaxWidth; + for( OptionList::iterator i = lOption.begin(); i; i++ ) + { + sio << " "; + if( bHasShort ) + { + if( (*i).cOpt == '\0' ) + sio << " "; + else + sio << "-" << (*i).cOpt; + sio << " "; + } + if( iMaxWidth > 0 ) + { + if( (*i).sOpt ) + { + sio << "--" << Fmt(iMaxWidth, Fmt::Left) << (*i).sOpt; + } + else + { + sio << " " << Fmt(iMaxWidth) << ""; + } + sio << " "; + } + sio << format( (*i).sHelp, iScrWidth-iIndent-1, iIndent ); + sio << sio.nl; + } + exit( 0 ); + return 0; } Bu::FString Bu::OptParser::format( const Bu::FString &sIn, int iWidth, int iIndent ) { + Bu::FString sOut; + Bu::FString sIndent; + for( int j = 0; j < iIndent; j++ ) + sIndent.append(" ", 1); + bool bFirst = true; + int iSpaceCount = 0; + bool bSpace = false; + int iPrevLineLen; + int iLineLen = 0; + char c; + Bu::FString::const_iterator iLastSpace, iStart; + for( Bu::FString::const_iterator i = iLastSpace = iStart = sIn.begin(); i; i++ ) + { + c = *i; + if( *i == ' ' ) + { + if( bSpace == false ) + { + iLastSpace = i; + iSpaceCount++; + bSpace = true; + iPrevLineLen = iLineLen; + } + } + else + { + bSpace = false; + } + iLineLen++; + if( iLineLen >= iWidth ) + { + iSpaceCount--; + if( bFirst == true ) + bFirst = false; + else + sOut += sIndent; + int iExtraSpaces = iWidth-iPrevLineLen; + bSpace = false; + float fFill = 0.0; + int iSubSpaceCount = 0; + float fAdd = ((float)iExtraSpaces/(float)iSpaceCount); + for( Bu::FString::const_iterator k = iStart; k != iLastSpace; k++ ) + { + sOut += *k; + if( *k == ' ' ) + { + if( bSpace == false && iExtraSpaces > 0 ) + { + bSpace = true; + fFill += fAdd; + iSubSpaceCount++; + for( int sp = 0; sp < (int)(fFill); sp++ ) + { + sOut += ' '; + iExtraSpaces--; + } + fFill -= (int)fFill; + if( iSubSpaceCount == iSpaceCount && iExtraSpaces > 0 ) + { + for(; iExtraSpaces > 0; iExtraSpaces-- ) + { + sOut += ' '; + } + } + } + } + else + bSpace = false; + } + //sOut.append( iStart, iLastSpace ); + sOut.append("\n"); + for(; iLastSpace && *iLastSpace == ' '; iLastSpace++ ) { } + iStart = i = iLastSpace; + bSpace = false; + iLineLen = 1; + iSpaceCount = 0; + } + } + if( !bFirst ) + sOut += sIndent; + sOut.append( iStart ); + return sOut; } @@ -29,7 +250,9 @@ Bu::FString Bu::OptParser::format( const Bu::FString &sIn, int iWidth, // Code for Bu::OptParser::Option // -Bu::OptParser::Option::Option() +Bu::OptParser::Option::Option() : + cOpt( '\0' ), + bShortHasParams( false ) { } diff --git a/src/optparser.h b/src/optparser.h index 39f048d..ed32e45 100644 --- a/src/optparser.h +++ b/src/optparser.h @@ -4,12 +4,16 @@ #include "bu/fstring.h" #include "bu/list.h" #include "bu/hash.h" +#include "bu/signals.h" +#include "bu/array.h" namespace Bu { + typedef Bu::Array StrArray; class OptParser { public: + typedef Signal1 OptionSignal; class Option { public: @@ -18,22 +22,21 @@ namespace Bu char cOpt; Bu::FString sOpt; - Bu::FString sDesc; + Bu::FString sHelp; + OptionSignal sUsed; + bool bShortHasParams; }; public: OptParser(); virtual ~OptParser(); - void parse( int argc, char *argv[] ); + void parse( int argc, char **argv ); void addOption( const Option &opt ); + void addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp ); - template - void callback( c *pObj, int (c::*fnc)( int, char *[] ) ) - { - (pObj->*fnc)( 0, NULL ); - } + int optHelp( StrArray aParams ); private: Bu::FString format( const Bu::FString &sIn, int iWidth, int iIndent ); diff --git a/src/signals.h b/src/signals.h index 3f892b8..075e5bf 100644 --- a/src/signals.h +++ b/src/signals.h @@ -89,7 +89,7 @@ namespace Bu bool isSet() const { return pCb != NULL; } operator bool() const { return isSet(); } - Signal0 &operator=( const Signal0 &rhs ) + Signal0 &operator=( const Signal0 &rhs ) { pCb = rhs.pCb->clone(); return *this; @@ -198,7 +198,7 @@ namespace Bu bool isSet() const { return pCb != NULL; } operator bool() const { return isSet(); } - Signal1 &operator=( const Signal1 &rhs ) + Signal1 &operator=( const Signal1 &rhs ) { pCb = rhs.pCb->clone(); return *this; @@ -307,7 +307,7 @@ namespace Bu bool isSet() const { return pCb != NULL; } operator bool() const { return isSet(); } - Signal2 &operator=( const Signal2 &rhs ) + Signal2 &operator=( const Signal2 &rhs ) { pCb = rhs.pCb->clone(); return *this; @@ -416,7 +416,7 @@ namespace Bu bool isSet() const { return pCb != NULL; } operator bool() const { return isSet(); } - Signal3 &operator=( const Signal3 &rhs ) + Signal3 &operator=( const Signal3 &rhs ) { pCb = rhs.pCb->clone(); return *this; @@ -525,7 +525,7 @@ namespace Bu bool isSet() const { return pCb != NULL; } operator bool() const { return isSet(); } - Signal4 &operator=( const Signal4 &rhs ) + Signal4 &operator=( const Signal4 &rhs ) { pCb = rhs.pCb->clone(); return *this; @@ -634,7 +634,7 @@ namespace Bu bool isSet() const { return pCb != NULL; } operator bool() const { return isSet(); } - Signal5 &operator=( const Signal5 &rhs ) + Signal5 &operator=( const Signal5 &rhs ) { pCb = rhs.pCb->clone(); return *this; diff --git a/src/tests/optparser.cpp b/src/tests/optparser.cpp index 9168af8..ee2ea58 100644 --- a/src/tests/optparser.cpp +++ b/src/tests/optparser.cpp @@ -8,13 +8,30 @@ public: Opts() : iBob( 542 ) { - callback( this, &Opts::cb ); + Option o; + o.sUsed = slot( this, &Opts::cb ); + o.cOpt = 'x'; + o.sOpt = "things"; + o.bShortHasParams = true; + o.sHelp = "This is the first test parameter. It calls a function, and takes parameters."; + addOption( o ); + + Option o2; + o2.sUsed = slot( this, &Opts::cb ); + o2.cOpt = 'y'; + o2.sOpt = "stuff"; + o2.bShortHasParams = false; + o2.sHelp = "This is the second test parameter. It does not take parameters. However, I do want to make this part much longer to see how it looks when you add way too much text to one of these things. It can't really be that bad, right?"; + addOption( o2 ); + + addHelpOption('h', "help", "This help."); } - int cb( int argc, char *argv[] ) + int cb( StrArray aParams ) { sio << "Hey, cb was called, here's a class var: " << iBob << sio.nl; - return 5; + sio << "argv[] = " << aParams << sio.nl; + return 1; } int iBob; @@ -23,5 +40,7 @@ public: int main( int argc, char *argv[] ) { Opts o; + + o.parse( argc, argv ); } diff --git a/src/tests/signals.cpp b/src/tests/signals.cpp index b4b1363..4af2b8e 100644 --- a/src/tests/signals.cpp +++ b/src/tests/signals.cpp @@ -51,7 +51,32 @@ private: void pfnc0() { - sio << "This doesn't have state, it's pfnc0()!" << sio.nl; + sio << ": void pfnc0()" << sio.nl; +} + +void pfnc1( int a ) +{ + sio << ": void pfnc1( " << a << " )" << sio.nl; +} + +void pfnc2( int a, Bu::FString b ) +{ + sio << ": void pfnc2( " << a << ", \"" << b << "\" )" << sio.nl; +} + +void pfnc3( int a, Bu::FString b, double c ) +{ + sio << ": void pfnc3( " << a << ", \"" << b << "\", " << c << " )" << sio.nl; +} + +void pfnc4( int a, Bu::FString b, double c, char d ) +{ + sio << ": void pfnc4( " << a << ", \"" << b << "\", " << c << ", '" << d << "' )" << sio.nl; +} + +void pfnc5( int a, Bu::FString b, double c, char d, long e ) +{ + sio << ": void pfnc5( " << a << ", \"" << b << "\", " << c << ", '" << d << "', " << e << " )" << sio.nl; } void callit( Signal0 sig ) @@ -65,21 +90,33 @@ int main() Signal0 cb0( slot( &t, &Thing::fnc0 ) ); cb0(); + cb0 = slot( &pfnc0 ); + cb0(); Signal1 cb1( slot( &t, &Thing::fnc1 ) ); cb1( 5 ); + cb1 = slot( &pfnc1 ); + cb1( 5 ); Signal2 cb2( slot( &t, &Thing::fnc2 ) ); cb2( 5, "Hi there" ); + cb2 = slot( &pfnc2 ); + cb2( 5, "Hi there" ); Signal3 cb3( slot( &t, &Thing::fnc3 ) ); cb3( 5, "Hi there", 12.85 ); + cb3 = slot( &pfnc3 ); + cb3( 5, "Hi there", 12.85 ); Signal4 cb4( slot( &t, &Thing::fnc4 ) ); cb4( 5, "Hi there", 12.85, 'z' ); + cb4 = slot( &pfnc4 ); + cb4( 5, "Hi there", 12.85, 'z' ); Signal5 cb5( slot( &t, &Thing::fnc5 ) ); cb5( 5, "Hi there", 12.85, 'z', 849 ); + cb5 = slot( &pfnc5 ); + cb5( 5, "Hi there", 12.85, 'z', 849 ); // Signal1 cb1( slot( &t, &Thing::fnc1 ) ); // sio << "Result: " << cb1( 5 ) << sio.nl; -- cgit v1.2.3