diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-05-26 04:03:24 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-05-26 04:03:24 +0000 |
commit | bd5bb1ca60a6a97b110cbf221b3625e6e6200141 (patch) | |
tree | 063b1576b57aba698159f50a04461ddfb7c4dfb6 /src | |
parent | bc6f456ef27bdf25bf7a7f677217b9b7204b4241 (diff) | |
download | libbu++-bd5bb1ca60a6a97b110cbf221b3625e6e6200141.tar.gz libbu++-bd5bb1ca60a6a97b110cbf221b3625e6e6200141.tar.bz2 libbu++-bd5bb1ca60a6a97b110cbf221b3625e6e6200141.tar.xz libbu++-bd5bb1ca60a6a97b110cbf221b3625e6e6200141.zip |
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...
Diffstat (limited to '')
-rw-r--r-- | src/exception.cpp | 25 | ||||
-rw-r--r-- | src/exception.h | 50 | ||||
-rw-r--r-- | src/pproc.cpp | 93 | ||||
-rw-r--r-- | src/pproc.h | 34 | ||||
-rw-r--r-- | src/serializable.h | 22 | ||||
-rw-r--r-- | src/test/params.cpp | 32 | ||||
-rw-r--r-- | src/tokenstring.h | 20 |
7 files changed, 248 insertions, 28 deletions
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() : | |||
5 | nErrorCode( 0 ) | 5 | nErrorCode( 0 ) |
6 | { | 6 | { |
7 | va_list ap; | 7 | va_list ap; |
8 | int nSize; | ||
9 | 8 | ||
10 | va_start(ap, lpFormat); | 9 | va_start(ap, lpFormat); |
11 | nSize = vsnprintf( NULL, 0, lpFormat, ap ); | 10 | setWhat( lpFormat, ap ); |
12 | sWhat = new char[nSize+1]; | ||
13 | vsnprintf( sWhat, nSize+1, lpFormat, ap ); | ||
14 | va_end(ap); | 11 | va_end(ap); |
15 | } | 12 | } |
16 | 13 | ||
@@ -18,20 +15,32 @@ Exception::Exception( int nCode, const char *lpFormat, ... ) throw() : | |||
18 | nErrorCode( nCode ) | 15 | nErrorCode( nCode ) |
19 | { | 16 | { |
20 | va_list ap; | 17 | va_list ap; |
21 | int nSize; | ||
22 | 18 | ||
23 | va_start(ap, lpFormat); | 19 | va_start(ap, lpFormat); |
24 | nSize = vsnprintf( NULL, 0, lpFormat, ap ); | 20 | setWhat( lpFormat, ap ); |
25 | sWhat = new char[nSize+1]; | ||
26 | vsnprintf( sWhat, nSize+1, lpFormat, ap ); | ||
27 | va_end(ap); | 21 | va_end(ap); |
28 | } | 22 | } |
29 | 23 | ||
24 | Exception::Exception( int nCode ) throw() : | ||
25 | nErrorCode( nCode ), | ||
26 | sWhat( NULL ) | ||
27 | { | ||
28 | } | ||
29 | |||
30 | Exception::~Exception() throw() | 30 | Exception::~Exception() throw() |
31 | { | 31 | { |
32 | delete[] sWhat; | 32 | delete[] sWhat; |
33 | } | 33 | } |
34 | 34 | ||
35 | void Exception::setWhat( const char *lpFormat, va_list &vargs ) | ||
36 | { | ||
37 | int nSize; | ||
38 | |||
39 | nSize = vsnprintf( NULL, 0, lpFormat, vargs ); | ||
40 | sWhat = new char[nSize+1]; | ||
41 | vsnprintf( sWhat, nSize+1, lpFormat, vargs ); | ||
42 | } | ||
43 | |||
35 | const char *Exception::what() const throw() | 44 | const char *Exception::what() const throw() |
36 | { | 45 | { |
37 | return sWhat; | 46 | 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 @@ | |||
3 | 3 | ||
4 | #include <string> | 4 | #include <string> |
5 | #include <exception> | 5 | #include <exception> |
6 | #include <stdarg.h> | ||
6 | 7 | ||
8 | /** | ||
9 | * A generalized Exception base class. This is nice for making general and | ||
10 | * flexible child classes that can create new error code classes. | ||
11 | */ | ||
7 | class Exception : public std::exception | 12 | class Exception : public std::exception |
8 | { | 13 | { |
9 | public: | 14 | public: |
15 | /** | ||
16 | * Construct an exception with an error code of zero, but with a | ||
17 | * description. The use of this is not reccomended most of the time, it's | ||
18 | * generally best to include an error code with the exception so your | ||
19 | * program can handle the exception in a better way. | ||
20 | * @param sFormat The format of the text. See printf for more info. | ||
21 | */ | ||
10 | Exception( const char *sFormat, ... ) throw(); | 22 | Exception( const char *sFormat, ... ) throw(); |
23 | |||
24 | /** | ||
25 | * | ||
26 | * @param nCode | ||
27 | * @param sFormat | ||
28 | */ | ||
11 | Exception( int nCode, const char *sFormat, ... ) throw(); | 29 | Exception( int nCode, const char *sFormat, ... ) throw(); |
30 | |||
31 | /** | ||
32 | * | ||
33 | * @param nCode | ||
34 | * @return | ||
35 | */ | ||
36 | Exception( int nCode=0 ) throw(); | ||
37 | |||
38 | /** | ||
39 | * | ||
40 | * @return | ||
41 | */ | ||
12 | virtual ~Exception() throw(); | 42 | virtual ~Exception() throw(); |
13 | 43 | ||
44 | /** | ||
45 | * | ||
46 | * @return | ||
47 | */ | ||
14 | virtual const char *what() const throw(); | 48 | virtual const char *what() const throw(); |
49 | |||
50 | /** | ||
51 | * | ||
52 | * @return | ||
53 | */ | ||
15 | int getErrorCode(); | 54 | int getErrorCode(); |
55 | |||
56 | /** | ||
57 | * | ||
58 | * @param lpFormat | ||
59 | * @param vargs | ||
60 | */ | ||
61 | void setWhat( const char *lpFormat, va_list &vargs ); | ||
16 | 62 | ||
17 | private: | 63 | private: |
18 | char *sWhat; | 64 | char *sWhat; /**< The text string telling people what went wrong. */ |
19 | int nErrorCode; | 65 | int nErrorCode; /**< The code for the error that occured. */ |
20 | }; | 66 | }; |
21 | 67 | ||
22 | #endif | 68 | #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 @@ | |||
2 | #include <string.h> | 2 | #include <string.h> |
3 | #include "pproc.h" | 3 | #include "pproc.h" |
4 | 4 | ||
5 | void pprocHelp( PPROC *pproc ) | ||
6 | { | ||
7 | for( int j = 0; pproc[j].proc || pproc[j].stateVar; j++ ) | ||
8 | { | ||
9 | printf("%c, %s - %s\n", | ||
10 | pproc[j].cChar, | ||
11 | pproc[j].lpWord, | ||
12 | pproc[j].shortHelp ); | ||
13 | } | ||
14 | } | ||
15 | |||
16 | void grabParamAsData( PPROC *pproc, char *str, int *aind, int *cind ) | ||
17 | { | ||
18 | switch( pproc->cMode & PPROC_TYPE ) | ||
19 | { | ||
20 | case PPROC_BOOL_TRUE: | ||
21 | *((bool *)pproc->stateVar) = true; | ||
22 | break; | ||
23 | |||
24 | case PPROC_BOOL_FALSE: | ||
25 | *((bool *)pproc->stateVar) = false; | ||
26 | break; | ||
27 | |||
28 | case PPROC_BOOL_TOGGLE: | ||
29 | *((bool *)pproc->stateVar) = !(*((bool *)pproc->stateVar)); | ||
30 | break; | ||
31 | |||
32 | case PPROC_CHAR: | ||
33 | (*cind)++; | ||
34 | *((char *)pproc->stateVar) = str[(*cind)]; | ||
35 | break; | ||
36 | |||
37 | case PPROC_SHORT: | ||
38 | break; | ||
39 | |||
40 | case PPROC_LONG: | ||
41 | break; | ||
42 | |||
43 | case PPREC_LONG_LONG: | ||
44 | break; | ||
45 | |||
46 | case PPROC_UCHAR: | ||
47 | break; | ||
48 | |||
49 | case PPROC_USHORT: | ||
50 | break; | ||
51 | |||
52 | case PPROC_ULONG: | ||
53 | break; | ||
54 | |||
55 | case PPREC_ULONG_LONG: | ||
56 | break; | ||
57 | |||
58 | case PPROC_FLOAT: | ||
59 | break; | ||
60 | |||
61 | case PPROC_DOUBLE: | ||
62 | break; | ||
63 | |||
64 | case PPROC_LONG_DOUBLE: | ||
65 | break; | ||
66 | |||
67 | case PPROC_STRING: | ||
68 | break; | ||
69 | } | ||
70 | } | ||
71 | |||
5 | void processParams( int argc, char *argv[], PPROC *pproc ) | 72 | void processParams( int argc, char *argv[], PPROC *pproc ) |
6 | { | 73 | { |
74 | bool bUsed; | ||
7 | // Loop over all the params except the first, no params, no looping! | 75 | // Loop over all the params except the first, no params, no looping! |
8 | for( int j = 1; j < argc; j++ ) | 76 | for( int j = 1; j < argc; j++ ) |
9 | { | 77 | { |
@@ -12,6 +80,7 @@ void processParams( int argc, char *argv[], PPROC *pproc ) | |||
12 | { | 80 | { |
13 | if( argv[j][1] == '-' ) | 81 | if( argv[j][1] == '-' ) |
14 | { | 82 | { |
83 | bUsed = false; | ||
15 | // Proccess a long-word param string | 84 | // Proccess a long-word param string |
16 | for( int k = 0; | 85 | for( int k = 0; |
17 | pproc[k].proc != NULL || pproc[k].stateVar != NULL; | 86 | pproc[k].proc != NULL || pproc[k].stateVar != NULL; |
@@ -19,19 +88,29 @@ void processParams( int argc, char *argv[], PPROC *pproc ) | |||
19 | { | 88 | { |
20 | if( !strcmp( pproc[k].lpWord, &argv[j][2] ) ) | 89 | if( !strcmp( pproc[k].lpWord, &argv[j][2] ) ) |
21 | { | 90 | { |
91 | bUsed = true; | ||
22 | if( pproc[k].proc != NULL ) | 92 | if( pproc[k].proc != NULL ) |
23 | { | 93 | { |
24 | j += pproc[k].proc( argc-j, &argv[j] ); | 94 | j += pproc[k].proc( argc-j, &argv[j] ); |
25 | } | 95 | } |
26 | if( pproc[k].stateVar != NULL ) | 96 | if( pproc[k].stateVar != NULL ) |
27 | { | 97 | { |
28 | (*(pproc[k].stateVar)) = pproc[k].bSetState; | 98 | grabParamAsData( &pproc[k], argv[j+1], &j, &k ); |
29 | } | 99 | } |
100 | break; | ||
101 | } | ||
102 | } | ||
103 | if( !bUsed ) | ||
104 | { | ||
105 | if( !strcmp( "help", &argv[j][2] ) ) | ||
106 | { | ||
107 | pprocHelp( pproc ); | ||
30 | } | 108 | } |
31 | } | 109 | } |
32 | } | 110 | } |
33 | else | 111 | else |
34 | { | 112 | { |
113 | bUsed = false; | ||
35 | // Process a one char param string | 114 | // Process a one char param string |
36 | for( int k = 0; | 115 | for( int k = 0; |
37 | pproc[k].proc != NULL || pproc[k].stateVar != NULL; | 116 | pproc[k].proc != NULL || pproc[k].stateVar != NULL; |
@@ -39,14 +118,24 @@ void processParams( int argc, char *argv[], PPROC *pproc ) | |||
39 | { | 118 | { |
40 | if( pproc[k].cChar == argv[j][1] ) | 119 | if( pproc[k].cChar == argv[j][1] ) |
41 | { | 120 | { |
121 | bUsed = true; | ||
42 | if( pproc[k].proc != NULL ) | 122 | if( pproc[k].proc != NULL ) |
43 | { | 123 | { |
44 | j += pproc[k].proc( argc-j, &argv[j] ); | 124 | j += pproc[k].proc( argc-j, &argv[j] ); |
45 | } | 125 | } |
46 | if( pproc[k].stateVar != NULL ) | 126 | if( pproc[k].stateVar != NULL ) |
47 | { | 127 | { |
48 | (*(pproc[k].stateVar)) = pproc[k].bSetState; | 128 | int tmp = 1; |
129 | grabParamAsData( &pproc[k], argv[j], &j, &tmp ); | ||
49 | } | 130 | } |
131 | break; | ||
132 | } | ||
133 | } | ||
134 | if( !bUsed ) | ||
135 | { | ||
136 | if( argv[j][1] == 'h' ) | ||
137 | { | ||
138 | pprocHelp( pproc ); | ||
50 | } | 139 | } |
51 | } | 140 | } |
52 | } | 141 | } |
diff --git a/src/pproc.h b/src/pproc.h index bf5063c..31d7c02 100644 --- a/src/pproc.h +++ b/src/pproc.h | |||
@@ -2,6 +2,33 @@ | |||
2 | #define PPROC_H_ | 2 | #define PPROC_H_ |
3 | 3 | ||
4 | /** | 4 | /** |
5 | * A mask to discover what the type is even if flags are set. | ||
6 | */ | ||
7 | #define PPROC_TYPE 0x0F | ||
8 | |||
9 | #define PPROC_BOOL_TRUE 0x01 | ||
10 | #define PPROC_BOOL_FALSE 0x02 | ||
11 | #define PPROC_BOOL_TOGGLE 0x03 | ||
12 | #define PPROC_CHAR 0x04 | ||
13 | #define PPROC_SHORT 0x05 | ||
14 | #define PPROC_LONG 0x06 | ||
15 | #define PPREC_LONG_LONG 0x07 | ||
16 | #define PPROC_UCHAR 0x08 | ||
17 | #define PPROC_USHORT 0x09 | ||
18 | #define PPROC_ULONG 0x0A | ||
19 | #define PPREC_ULONG_LONG 0x0B | ||
20 | #define PPROC_FLOAT 0x0C | ||
21 | #define PPROC_DOUBLE 0x0D | ||
22 | #define PPROC_LONG_DOUBLE 0x0E | ||
23 | #define PPROC_STRING 0x0F | ||
24 | |||
25 | #define PPROCF_CALLBACK 0x10 | ||
26 | #define PPROCF_ALLOW_EQUALS 0x20 | ||
27 | #define PPROCF_SHORT_TERMINAL 0x40 | ||
28 | #define PPROCF_TERMINATE 0x80 | ||
29 | |||
30 | |||
31 | /** | ||
5 | * Contains all required info to handle a single program parameter. | 32 | * Contains all required info to handle a single program parameter. |
6 | *@author Mike Buland | 33 | *@author Mike Buland |
7 | */ | 34 | */ |
@@ -9,6 +36,9 @@ typedef struct PPROC | |||
9 | { | 36 | { |
10 | const char *lpWord; /**< The full text-word to use as a param. */ | 37 | const char *lpWord; /**< The full text-word to use as a param. */ |
11 | const char cChar; /**< The short char version of the param. */ | 38 | const char cChar; /**< The short char version of the param. */ |
39 | |||
40 | const char cMode; /**< One of the PPROC_* macros, these are not flags. */ | ||
41 | |||
12 | /** | 42 | /** |
13 | * Pointer to the function to call when this param is triggered. | 43 | * Pointer to the function to call when this param is triggered. |
14 | *@param argc The number of params after and including the one that | 44 | *@param argc The number of params after and including the one that |
@@ -19,8 +49,8 @@ typedef struct PPROC | |||
19 | * the processParams function. | 49 | * the processParams function. |
20 | */ | 50 | */ |
21 | int (*proc)( int argc, char *argv[] ); | 51 | int (*proc)( int argc, char *argv[] ); |
22 | bool *stateVar; /**< A pointer to a bool to be setwhen this is triggered */ | 52 | void *stateVar; /**< A pointer to a variable to set */ |
23 | bool bSetState; /**< The state to set the above bool to. */ | 53 | const char *shortHelp; |
24 | } PPROC; | 54 | } PPROC; |
25 | 55 | ||
26 | /** | 56 | /** |
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 @@ | |||
3 | 3 | ||
4 | //#include "serializer.h" | 4 | //#include "serializer.h" |
5 | 5 | ||
6 | /** | ||
7 | * The base class for any class you want to serialize. Simply include this as | ||
8 | * a base class, implement the purely virtual serialize function and you've got | ||
9 | * an easily serializable class. | ||
10 | */ | ||
6 | class Serializable | 11 | class Serializable |
7 | { | 12 | { |
8 | public: | 13 | public: |
14 | /** | ||
15 | * Does nothing, here for completeness. | ||
16 | */ | ||
9 | Serializable(); | 17 | Serializable(); |
18 | |||
19 | /** | ||
20 | * Here to ensure the deconstructor is virtual. | ||
21 | */ | ||
10 | virtual ~Serializable(); | 22 | virtual ~Serializable(); |
11 | virtual void serialize(class Serializer &)=0; | 23 | |
24 | /** | ||
25 | * This is the main workhorse of the serialization system, just override and | ||
26 | * you've got a serializable class. A reference to the Serializer archive | ||
27 | * used is passed in as your only parameter, query it to discover if you are | ||
28 | * loading or saving. | ||
29 | * @param ar A reference to the Serializer object to use. | ||
30 | */ | ||
31 | virtual void serialize( class Serializer &ar )=0; | ||
12 | }; | 32 | }; |
13 | 33 | ||
14 | #endif | 34 | #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 @@ | |||
1 | #include <stdio.h> | ||
2 | #include "pproc.h" | ||
3 | |||
4 | int main( int argc, char *argv[] ) | ||
5 | { | ||
6 | bool bOn = false; | ||
7 | bool bOff = true; | ||
8 | bool bTog = false; | ||
9 | char cChar = '?'; | ||
10 | PPROC table[] = { | ||
11 | { "boolon", 'n', PPROC_BOOL_TRUE, NULL, &bOn, | ||
12 | "Set the bool on." }, | ||
13 | { "booloff", 'f', PPROC_BOOL_FALSE, NULL, &bOff, | ||
14 | "Set the bool off." }, | ||
15 | { "booltog", 't', PPROC_BOOL_TOGGLE, NULL, &bTog, | ||
16 | "Set the bool off." }, | ||
17 | { "char", 'c', PPROC_CHAR, NULL, &cChar, | ||
18 | "Set the char." }, | ||
19 | { NULL, '\0', 0, NULL, NULL, NULL } | ||
20 | }; | ||
21 | |||
22 | processParams( argc, argv, table ); | ||
23 | |||
24 | printf("Final results:\n"); | ||
25 | printf("\tbOn = %s\n", (bOn ? "true" : "false") ); | ||
26 | printf("\tbOff = %s\n", (bOff ? "true" : "false") ); | ||
27 | printf("\tbTog = %s\n", (bTog ? "true" : "false") ); | ||
28 | printf("\tcChar = '%c'\n", cChar ); | ||
29 | |||
30 | return 0; | ||
31 | } | ||
32 | |||
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 @@ | |||
1 | /*************************************************************************** | ||
2 | * Copyright (C) 2003 by Mike Buland * | ||
3 | * eichlan@Xagafinelle * | ||
4 | * * | ||
5 | * This program is free software; you can redistribute it and/or modify * | ||
6 | * it under the terms of the GNU General Public License as published by * | ||
7 | * the Free Software Foundation; either version 2 of the License, or * | ||
8 | * (at your option) any later version. * | ||
9 | ***************************************************************************/ | ||
10 | #ifndef TOKENSTRING_H | 1 | #ifndef TOKENSTRING_H |
11 | #define TOKENSTRING_H | 2 | #define TOKENSTRING_H |
12 | 3 | ||
@@ -107,13 +98,16 @@ public: | |||
107 | void insertToken( int nStart, int nEnd, char *lpOldOrig, const char *lpNewToken, int nIndex ); | 98 | void insertToken( int nStart, int nEnd, char *lpOldOrig, const char *lpNewToken, int nIndex ); |
108 | 99 | ||
109 | private: | 100 | private: |
110 | char *lpTokenString; | 101 | char *lpTokenString; /**< The original text that this string came from */ |
111 | LinkedList lToken; | 102 | LinkedList lToken; /**< The list of tokens. */ |
112 | 103 | ||
104 | /** | ||
105 | * A single token within the token string. | ||
106 | */ | ||
113 | typedef struct Token | 107 | typedef struct Token |
114 | { | 108 | { |
115 | char *lpOrig; // This is just a pointer back to lpTokenString | 109 | char *lpOrig; /**< This is just a pointer back to lpTokenString */ |
116 | char *lpToken; // This is really a whole token | 110 | char *lpToken; /**< This is really a whole token */ |
117 | } Token; | 111 | } Token; |
118 | }; | 112 | }; |
119 | 113 | ||