From bd5bb1ca60a6a97b110cbf221b3625e6e6200141 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 26 May 2006 04:03:24 +0000 Subject: Loads of updates to several systems, see each for what really changed, the biggest are the updates to the exception framework, and to the pproc system, which is almost a competitor to popt already... --- src/exception.cpp | 25 +++++++++----- src/exception.h | 50 ++++++++++++++++++++++++++-- src/pproc.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/pproc.h | 34 ++++++++++++++++++-- src/serializable.h | 22 ++++++++++++- src/test/params.cpp | 32 ++++++++++++++++++ src/tokenstring.h | 20 ++++-------- 7 files changed, 248 insertions(+), 28 deletions(-) create mode 100644 src/test/params.cpp (limited to 'src') diff --git a/src/exception.cpp b/src/exception.cpp index 11f04a9..291a198 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -5,12 +5,9 @@ Exception::Exception( const char *lpFormat, ... ) throw() : nErrorCode( 0 ) { va_list ap; - int nSize; va_start(ap, lpFormat); - nSize = vsnprintf( NULL, 0, lpFormat, ap ); - sWhat = new char[nSize+1]; - vsnprintf( sWhat, nSize+1, lpFormat, ap ); + setWhat( lpFormat, ap ); va_end(ap); } @@ -18,20 +15,32 @@ Exception::Exception( int nCode, const char *lpFormat, ... ) throw() : nErrorCode( nCode ) { va_list ap; - int nSize; va_start(ap, lpFormat); - nSize = vsnprintf( NULL, 0, lpFormat, ap ); - sWhat = new char[nSize+1]; - vsnprintf( sWhat, nSize+1, lpFormat, ap ); + setWhat( lpFormat, ap ); va_end(ap); } +Exception::Exception( int nCode ) throw() : + nErrorCode( nCode ), + sWhat( NULL ) +{ +} + Exception::~Exception() throw() { delete[] sWhat; } +void Exception::setWhat( const char *lpFormat, va_list &vargs ) +{ + int nSize; + + nSize = vsnprintf( NULL, 0, lpFormat, vargs ); + sWhat = new char[nSize+1]; + vsnprintf( sWhat, nSize+1, lpFormat, vargs ); +} + const char *Exception::what() const throw() { return sWhat; diff --git a/src/exception.h b/src/exception.h index be876d7..2233736 100644 --- a/src/exception.h +++ b/src/exception.h @@ -3,20 +3,66 @@ #include #include +#include +/** + * A generalized Exception base class. This is nice for making general and + * flexible child classes that can create new error code classes. + */ class Exception : public std::exception { public: + /** + * Construct an exception with an error code of zero, but with a + * description. The use of this is not reccomended most of the time, it's + * generally best to include an error code with the exception so your + * program can handle the exception in a better way. + * @param sFormat The format of the text. See printf for more info. + */ Exception( const char *sFormat, ... ) throw(); + + /** + * + * @param nCode + * @param sFormat + */ Exception( int nCode, const char *sFormat, ... ) throw(); + + /** + * + * @param nCode + * @return + */ + Exception( int nCode=0 ) throw(); + + /** + * + * @return + */ virtual ~Exception() throw(); + /** + * + * @return + */ virtual const char *what() const throw(); + + /** + * + * @return + */ int getErrorCode(); + + /** + * + * @param lpFormat + * @param vargs + */ + void setWhat( const char *lpFormat, va_list &vargs ); private: - char *sWhat; - int nErrorCode; + char *sWhat; /**< The text string telling people what went wrong. */ + int nErrorCode; /**< The code for the error that occured. */ }; #endif diff --git a/src/pproc.cpp b/src/pproc.cpp index f5cb869..ea836eb 100644 --- a/src/pproc.cpp +++ b/src/pproc.cpp @@ -2,8 +2,76 @@ #include #include "pproc.h" +void pprocHelp( PPROC *pproc ) +{ + for( int j = 0; pproc[j].proc || pproc[j].stateVar; j++ ) + { + printf("%c, %s - %s\n", + pproc[j].cChar, + pproc[j].lpWord, + pproc[j].shortHelp ); + } +} + +void grabParamAsData( PPROC *pproc, char *str, int *aind, int *cind ) +{ + switch( pproc->cMode & PPROC_TYPE ) + { + case PPROC_BOOL_TRUE: + *((bool *)pproc->stateVar) = true; + break; + + case PPROC_BOOL_FALSE: + *((bool *)pproc->stateVar) = false; + break; + + case PPROC_BOOL_TOGGLE: + *((bool *)pproc->stateVar) = !(*((bool *)pproc->stateVar)); + break; + + case PPROC_CHAR: + (*cind)++; + *((char *)pproc->stateVar) = str[(*cind)]; + break; + + case PPROC_SHORT: + break; + + case PPROC_LONG: + break; + + case PPREC_LONG_LONG: + break; + + case PPROC_UCHAR: + break; + + case PPROC_USHORT: + break; + + case PPROC_ULONG: + break; + + case PPREC_ULONG_LONG: + break; + + case PPROC_FLOAT: + break; + + case PPROC_DOUBLE: + break; + + case PPROC_LONG_DOUBLE: + break; + + case PPROC_STRING: + break; + } +} + void processParams( int argc, char *argv[], PPROC *pproc ) { + bool bUsed; // Loop over all the params except the first, no params, no looping! for( int j = 1; j < argc; j++ ) { @@ -12,6 +80,7 @@ void processParams( int argc, char *argv[], PPROC *pproc ) { if( argv[j][1] == '-' ) { + bUsed = false; // Proccess a long-word param string for( int k = 0; pproc[k].proc != NULL || pproc[k].stateVar != NULL; @@ -19,19 +88,29 @@ void processParams( int argc, char *argv[], PPROC *pproc ) { if( !strcmp( pproc[k].lpWord, &argv[j][2] ) ) { + bUsed = true; if( pproc[k].proc != NULL ) { j += pproc[k].proc( argc-j, &argv[j] ); } if( pproc[k].stateVar != NULL ) { - (*(pproc[k].stateVar)) = pproc[k].bSetState; + grabParamAsData( &pproc[k], argv[j+1], &j, &k ); } + break; + } + } + if( !bUsed ) + { + if( !strcmp( "help", &argv[j][2] ) ) + { + pprocHelp( pproc ); } } } else { + bUsed = false; // Process a one char param string for( int k = 0; pproc[k].proc != NULL || pproc[k].stateVar != NULL; @@ -39,14 +118,24 @@ void processParams( int argc, char *argv[], PPROC *pproc ) { if( pproc[k].cChar == argv[j][1] ) { + bUsed = true; if( pproc[k].proc != NULL ) { j += pproc[k].proc( argc-j, &argv[j] ); } if( pproc[k].stateVar != NULL ) { - (*(pproc[k].stateVar)) = pproc[k].bSetState; + int tmp = 1; + grabParamAsData( &pproc[k], argv[j], &j, &tmp ); } + break; + } + } + if( !bUsed ) + { + if( argv[j][1] == 'h' ) + { + pprocHelp( pproc ); } } } diff --git a/src/pproc.h b/src/pproc.h index bf5063c..31d7c02 100644 --- a/src/pproc.h +++ b/src/pproc.h @@ -1,6 +1,33 @@ #ifndef PPROC_H_ #define PPROC_H_ +/** + * A mask to discover what the type is even if flags are set. + */ +#define PPROC_TYPE 0x0F + +#define PPROC_BOOL_TRUE 0x01 +#define PPROC_BOOL_FALSE 0x02 +#define PPROC_BOOL_TOGGLE 0x03 +#define PPROC_CHAR 0x04 +#define PPROC_SHORT 0x05 +#define PPROC_LONG 0x06 +#define PPREC_LONG_LONG 0x07 +#define PPROC_UCHAR 0x08 +#define PPROC_USHORT 0x09 +#define PPROC_ULONG 0x0A +#define PPREC_ULONG_LONG 0x0B +#define PPROC_FLOAT 0x0C +#define PPROC_DOUBLE 0x0D +#define PPROC_LONG_DOUBLE 0x0E +#define PPROC_STRING 0x0F + +#define PPROCF_CALLBACK 0x10 +#define PPROCF_ALLOW_EQUALS 0x20 +#define PPROCF_SHORT_TERMINAL 0x40 +#define PPROCF_TERMINATE 0x80 + + /** * Contains all required info to handle a single program parameter. *@author Mike Buland @@ -9,6 +36,9 @@ typedef struct PPROC { const char *lpWord; /**< The full text-word to use as a param. */ const char cChar; /**< The short char version of the param. */ + + const char cMode; /**< One of the PPROC_* macros, these are not flags. */ + /** * Pointer to the function to call when this param is triggered. *@param argc The number of params after and including the one that @@ -19,8 +49,8 @@ typedef struct PPROC * the processParams function. */ int (*proc)( int argc, char *argv[] ); - bool *stateVar; /**< A pointer to a bool to be setwhen this is triggered */ - bool bSetState; /**< The state to set the above bool to. */ + void *stateVar; /**< A pointer to a variable to set */ + const char *shortHelp; } PPROC; /** diff --git a/src/serializable.h b/src/serializable.h index 36c94df..06def29 100644 --- a/src/serializable.h +++ b/src/serializable.h @@ -3,12 +3,32 @@ //#include "serializer.h" +/** + * The base class for any class you want to serialize. Simply include this as + * a base class, implement the purely virtual serialize function and you've got + * an easily serializable class. + */ class Serializable { public: + /** + * Does nothing, here for completeness. + */ Serializable(); + + /** + * Here to ensure the deconstructor is virtual. + */ virtual ~Serializable(); - virtual void serialize(class Serializer &)=0; + + /** + * This is the main workhorse of the serialization system, just override and + * you've got a serializable class. A reference to the Serializer archive + * used is passed in as your only parameter, query it to discover if you are + * loading or saving. + * @param ar A reference to the Serializer object to use. + */ + virtual void serialize( class Serializer &ar )=0; }; #endif diff --git a/src/test/params.cpp b/src/test/params.cpp new file mode 100644 index 0000000..7bd2c0c --- /dev/null +++ b/src/test/params.cpp @@ -0,0 +1,32 @@ +#include +#include "pproc.h" + +int main( int argc, char *argv[] ) +{ + bool bOn = false; + bool bOff = true; + bool bTog = false; + char cChar = '?'; + PPROC table[] = { + { "boolon", 'n', PPROC_BOOL_TRUE, NULL, &bOn, + "Set the bool on." }, + { "booloff", 'f', PPROC_BOOL_FALSE, NULL, &bOff, + "Set the bool off." }, + { "booltog", 't', PPROC_BOOL_TOGGLE, NULL, &bTog, + "Set the bool off." }, + { "char", 'c', PPROC_CHAR, NULL, &cChar, + "Set the char." }, + { NULL, '\0', 0, NULL, NULL, NULL } + }; + + processParams( argc, argv, table ); + + printf("Final results:\n"); + printf("\tbOn = %s\n", (bOn ? "true" : "false") ); + printf("\tbOff = %s\n", (bOff ? "true" : "false") ); + printf("\tbTog = %s\n", (bTog ? "true" : "false") ); + printf("\tcChar = '%c'\n", cChar ); + + return 0; +} + diff --git a/src/tokenstring.h b/src/tokenstring.h index 25f710b..37ce6f5 100644 --- a/src/tokenstring.h +++ b/src/tokenstring.h @@ -1,12 +1,3 @@ -/*************************************************************************** - * Copyright (C) 2003 by Mike Buland * - * eichlan@Xagafinelle * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - ***************************************************************************/ #ifndef TOKENSTRING_H #define TOKENSTRING_H @@ -107,13 +98,16 @@ public: void insertToken( int nStart, int nEnd, char *lpOldOrig, const char *lpNewToken, int nIndex ); private: - char *lpTokenString; - LinkedList lToken; + char *lpTokenString; /**< The original text that this string came from */ + LinkedList lToken; /**< The list of tokens. */ + /** + * A single token within the token string. + */ typedef struct Token { - char *lpOrig; // This is just a pointer back to lpTokenString - char *lpToken; // This is really a whole token + char *lpOrig; /**< This is just a pointer back to lpTokenString */ + char *lpToken; /**< This is really a whole token */ } Token; }; -- cgit v1.2.3