diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-06-29 02:50:56 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-06-29 02:50:56 +0000 |
commit | 5e386890b41fe043e2639b25b613831ef8362e7b (patch) | |
tree | 60da93a6ade1db5ed2620fbe47b472291459a1a1 | |
parent | ac98da8eacbf1147d69e378db62432db05e8c73e (diff) | |
download | libbu++-5e386890b41fe043e2639b25b613831ef8362e7b.tar.gz libbu++-5e386890b41fe043e2639b25b613831ef8362e7b.tar.bz2 libbu++-5e386890b41fe043e2639b25b613831ef8362e7b.tar.xz libbu++-5e386890b41fe043e2639b25b613831ef8362e7b.zip |
Completely removed the old, crappy pproc and replaced it with the new, shiny
ParamProc class...it's soooo much better it makes me wanna' throw things...
-rw-r--r-- | src/paramproc.cpp | 398 | ||||
-rw-r--r-- | src/paramproc.h | 139 | ||||
-rw-r--r-- | src/pproc.cpp | 169 | ||||
-rw-r--r-- | src/pproc.h | 65 | ||||
-rw-r--r-- | src/test/param.cpp | 40 | ||||
-rw-r--r-- | src/test/param.h | 21 | ||||
-rw-r--r-- | src/test/params.cpp | 33 |
7 files changed, 598 insertions, 267 deletions
diff --git a/src/paramproc.cpp b/src/paramproc.cpp new file mode 100644 index 0000000..10284c4 --- /dev/null +++ b/src/paramproc.cpp | |||
@@ -0,0 +1,398 @@ | |||
1 | #include "paramproc.h" | ||
2 | #include <stdio.h> | ||
3 | |||
4 | #define ptrtype( iitype, iiname ) \ | ||
5 | ParamProc::ParamPtr::ParamPtr( iitype *iiname ) : \ | ||
6 | type( vt ##iiname ) { val.iiname = iiname; } | ||
7 | |||
8 | ParamProc::ParamPtr::ParamPtr() | ||
9 | { | ||
10 | val.str = NULL; | ||
11 | type = vtunset; | ||
12 | } | ||
13 | |||
14 | ptrtype( std::string, str ); | ||
15 | ptrtype( uint64_t, uint64 ); | ||
16 | ptrtype( uint32_t, uint32 ); | ||
17 | ptrtype( uint16_t, uint16 ); | ||
18 | ptrtype( uint8_t, uint8 ); | ||
19 | ptrtype( int64_t, int64 ); | ||
20 | ptrtype( int32_t, int32 ); | ||
21 | ptrtype( int16_t, int16 ); | ||
22 | ptrtype( int8_t, int8 ); | ||
23 | ptrtype( float, float32 ); | ||
24 | ptrtype( double, float64 ); | ||
25 | ptrtype( long double, float96 ); | ||
26 | ptrtype( bool, bln ); | ||
27 | |||
28 | ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( ParamProc::ParamPtr &ptr ) | ||
29 | { | ||
30 | val = ptr.val; | ||
31 | type = ptr.type; | ||
32 | |||
33 | return *this; | ||
34 | } | ||
35 | |||
36 | bool ParamProc::ParamPtr::isSet() | ||
37 | { | ||
38 | return type != vtunset; | ||
39 | } | ||
40 | |||
41 | ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( const char *str ) | ||
42 | { | ||
43 | if( !isSet() ) return *this; | ||
44 | switch( type ) | ||
45 | { | ||
46 | case vtstr: | ||
47 | (*val.str) = str; | ||
48 | break; | ||
49 | |||
50 | case vtuint64: | ||
51 | (*val.uint64) = strtoull( str, NULL, 10 ); | ||
52 | break; | ||
53 | |||
54 | case vtuint32: | ||
55 | (*val.uint32) = strtoul( str, NULL, 10 ); | ||
56 | break; | ||
57 | |||
58 | case vtuint16: | ||
59 | (*val.uint16) = (uint16_t)strtoul( str, NULL, 10 ); | ||
60 | break; | ||
61 | |||
62 | case vtuint8: | ||
63 | (*val.uint8) = (uint8_t)strtoul( str, NULL, 10 ); | ||
64 | break; | ||
65 | |||
66 | case vtint64: | ||
67 | (*val.int64) = strtoll( str, NULL, 10 ); | ||
68 | break; | ||
69 | |||
70 | case vtint32: | ||
71 | (*val.int32) = strtol( str, NULL, 10 ); | ||
72 | break; | ||
73 | |||
74 | case vtint16: | ||
75 | (*val.int16) = (int16_t)strtol( str, NULL, 10 ); | ||
76 | break; | ||
77 | |||
78 | case vtint8: | ||
79 | (*val.int8) = (int8_t)strtol( str, NULL, 10 ); | ||
80 | break; | ||
81 | |||
82 | case vtfloat32: | ||
83 | (*val.float32) = strtof( str, NULL ); | ||
84 | break; | ||
85 | |||
86 | case vtfloat64: | ||
87 | (*val.float64) = strtod( str, NULL ); | ||
88 | break; | ||
89 | |||
90 | case vtfloat96: | ||
91 | (*val.float96) = strtold( str, NULL ); | ||
92 | break; | ||
93 | |||
94 | case vtbln: | ||
95 | if( strcasecmp("yes", str ) == 0 || | ||
96 | strcasecmp("true", str ) == 0 ) | ||
97 | { | ||
98 | (*val.bln) = true; | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | (*val.bln) = false; | ||
103 | } | ||
104 | break; | ||
105 | } | ||
106 | |||
107 | return *this; | ||
108 | } | ||
109 | |||
110 | ParamProc::ParamProc() | ||
111 | { | ||
112 | } | ||
113 | |||
114 | ParamProc::~ParamProc() | ||
115 | { | ||
116 | } | ||
117 | /* | ||
118 | void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val ) | ||
119 | { | ||
120 | printf("Calling callback...\n"); | ||
121 | val = "Hello there, this is set in the ParamProc"; | ||
122 | (this->*proc)(); | ||
123 | }*/ | ||
124 | |||
125 | void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, | ||
126 | ParamPtr val, const char *lpDesc, const char *lpExtra, | ||
127 | const char *lpValue ) | ||
128 | { | ||
129 | ArgSpec *as = new ArgSpec; | ||
130 | if( lpWord ) | ||
131 | as->sWord = lpWord; | ||
132 | |||
133 | as->cChar = cChar; | ||
134 | as->proc = proc; | ||
135 | as->val = val; | ||
136 | if( lpDesc ) | ||
137 | as->sDesc = lpDesc; | ||
138 | if( lpExtra ) | ||
139 | as->sExtra = lpExtra; | ||
140 | if( lpValue ) | ||
141 | as->sValue = lpValue; | ||
142 | |||
143 | lArg.push_back( as ); | ||
144 | } | ||
145 | |||
146 | void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, | ||
147 | const char *lpDesc, const char *lpExtra, | ||
148 | const char *lpValue ) | ||
149 | { | ||
150 | addParam( lpWord, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
151 | } | ||
152 | |||
153 | void ParamProc::addParam( const char *lpWord, char cChar, ParamPtr val, | ||
154 | const char *lpDesc, const char *lpExtra, | ||
155 | const char *lpValue ) | ||
156 | { | ||
157 | addParam( lpWord, cChar, NULL, val, lpDesc, lpExtra, lpValue ); | ||
158 | } | ||
159 | |||
160 | void ParamProc::addParam( const char *lpWord, Proc proc, ParamPtr val, | ||
161 | const char *lpDesc, const char *lpExtra, | ||
162 | const char *lpValue ) | ||
163 | { | ||
164 | addParam( lpWord, '\0', proc, val, lpDesc, lpExtra, lpValue ); | ||
165 | } | ||
166 | |||
167 | void ParamProc::addParam( const char *lpWord, Proc proc, | ||
168 | const char *lpDesc, const char *lpExtra, | ||
169 | const char *lpValue ) | ||
170 | { | ||
171 | addParam( lpWord, '\0', proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
172 | } | ||
173 | |||
174 | void ParamProc::addParam( const char *lpWord, ParamPtr val, | ||
175 | const char *lpDesc, const char *lpExtra, | ||
176 | const char *lpValue ) | ||
177 | { | ||
178 | addParam( lpWord, '\0', NULL, val, lpDesc, lpExtra, lpValue ); | ||
179 | } | ||
180 | |||
181 | void ParamProc::addParam( char cChar, Proc proc, ParamPtr val, | ||
182 | const char *lpDesc, const char *lpExtra, | ||
183 | const char *lpValue ) | ||
184 | { | ||
185 | addParam( NULL, cChar, proc, val, lpDesc, lpExtra, lpValue ); | ||
186 | } | ||
187 | |||
188 | void ParamProc::addParam( char cChar, Proc proc, | ||
189 | const char *lpDesc, const char *lpExtra, | ||
190 | const char *lpValue ) | ||
191 | { | ||
192 | addParam( NULL, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
193 | } | ||
194 | |||
195 | void ParamProc::addParam( char cChar, ParamPtr val, | ||
196 | const char *lpDesc, const char *lpExtra, | ||
197 | const char *lpValue ) | ||
198 | { | ||
199 | addParam( NULL, cChar, NULL, val, lpDesc, lpExtra, lpValue ); | ||
200 | } | ||
201 | |||
202 | void ParamProc::process( int argc, char *argv[] ) | ||
203 | { | ||
204 | for( int arg = 1; arg < argc; arg++ ) | ||
205 | { | ||
206 | printf(":::%d:::%s\n", arg, argv[arg] ); | ||
207 | if( argv[arg][0] == '-' ) | ||
208 | { | ||
209 | if( argv[arg][1] == '-' ) | ||
210 | { | ||
211 | ArgSpec *s = checkWord( argv[arg]+2 ); | ||
212 | if( s ) | ||
213 | { | ||
214 | if( argv[arg][s->sWord.getLength()+2] == '=' ) | ||
215 | { | ||
216 | if( s->val.isSet() ) | ||
217 | { | ||
218 | if( s->sValue.getString() == NULL ) | ||
219 | { | ||
220 | s->val = argv[arg]+s->sWord.getLength()+3; | ||
221 | } | ||
222 | else | ||
223 | { | ||
224 | s->val = s->sValue.getString(); | ||
225 | } | ||
226 | } | ||
227 | if( s->proc ) | ||
228 | { | ||
229 | char **tmp = new char*[argc-arg]; | ||
230 | tmp[0] = argv[arg]+s->sWord.getLength()+3; | ||
231 | for( int k = 1; k < argc-arg; k++ ) | ||
232 | tmp[k] = argv[arg+k]; | ||
233 | int ret = (this->*s->proc)( argc-arg, tmp ); | ||
234 | if( ret > 0 ) | ||
235 | { | ||
236 | arg += ret-1; | ||
237 | } | ||
238 | delete tmp; | ||
239 | } | ||
240 | } | ||
241 | else | ||
242 | { | ||
243 | int add = 0; | ||
244 | if( s->val.isSet() ) | ||
245 | { | ||
246 | if( s->sValue.getString() == NULL ) | ||
247 | { | ||
248 | if( arg+1 >= argc ) | ||
249 | { | ||
250 | return; | ||
251 | } | ||
252 | s->val = argv[arg+1]; | ||
253 | add++; | ||
254 | } | ||
255 | else | ||
256 | { | ||
257 | s->val = s->sValue.getString(); | ||
258 | } | ||
259 | } | ||
260 | if( s->proc ) | ||
261 | { | ||
262 | int ret = (this->*s->proc)( | ||
263 | argc-arg-1, argv+arg+1 ); | ||
264 | |||
265 | if( ret > add ) | ||
266 | arg += ret; | ||
267 | else | ||
268 | arg += add; | ||
269 | } | ||
270 | } | ||
271 | continue; | ||
272 | } | ||
273 | } | ||
274 | else | ||
275 | { | ||
276 | for( int chr = 1; argv[arg][chr]; chr++ ) | ||
277 | { | ||
278 | ArgSpec *s = checkLetr( argv[arg][chr] ); | ||
279 | if( s ) | ||
280 | { | ||
281 | if( argv[arg][chr+1] != '\0' ) | ||
282 | { | ||
283 | bool bUsed = false; | ||
284 | if( s->val.isSet() ) | ||
285 | { | ||
286 | if( s->sValue.getString() == NULL ) | ||
287 | { | ||
288 | s->val = argv[arg]+chr+1; | ||
289 | bUsed = true; | ||
290 | } | ||
291 | else | ||
292 | { | ||
293 | s->val = s->sValue.getString(); | ||
294 | } | ||
295 | } | ||
296 | if( s->proc ) | ||
297 | { | ||
298 | char **tmp = new char*[argc-arg]; | ||
299 | tmp[0] = argv[arg]+chr+1; | ||
300 | for( int k = 1; k < argc-arg; k++ ) | ||
301 | tmp[k] = argv[arg+k]; | ||
302 | int ret = (this->*s->proc)( argc-arg, tmp ); | ||
303 | if( ret > 0 ) | ||
304 | { | ||
305 | arg += ret - 1; | ||
306 | delete tmp; | ||
307 | break; | ||
308 | } | ||
309 | if( bUsed ) | ||
310 | { | ||
311 | delete tmp; | ||
312 | break; | ||
313 | } | ||
314 | delete tmp; | ||
315 | } | ||
316 | } | ||
317 | else | ||
318 | { | ||
319 | bool bUsed = false; | ||
320 | if( s->val.isSet() ) | ||
321 | { | ||
322 | if( s->sValue.getString() == NULL ) | ||
323 | { | ||
324 | s->val = argv[arg+1]; | ||
325 | bUsed = true; | ||
326 | } | ||
327 | else | ||
328 | { | ||
329 | s->val = s->sValue.getString(); | ||
330 | } | ||
331 | } | ||
332 | if( s->proc ) | ||
333 | { | ||
334 | int ret = (this->*s->proc)( | ||
335 | argc-arg-1, argv+arg+1 | ||
336 | ); | ||
337 | if( ret > 0 ) | ||
338 | { | ||
339 | arg += ret; | ||
340 | break; | ||
341 | } | ||
342 | if( bUsed ) | ||
343 | { | ||
344 | arg++; | ||
345 | break; | ||
346 | } | ||
347 | } | ||
348 | } | ||
349 | } | ||
350 | } | ||
351 | } | ||
352 | } | ||
353 | } | ||
354 | } | ||
355 | |||
356 | ParamProc::ArgSpec *ParamProc::checkWord( const char *arg ) | ||
357 | { | ||
358 | printf("Checking \"%s\"...\n", arg ); | ||
359 | std::list<ArgSpec *>::const_iterator i; | ||
360 | for( i = lArg.begin(); i != lArg.end(); i++ ) | ||
361 | { | ||
362 | if( (*i)->sWord.getString() == NULL ) | ||
363 | continue; | ||
364 | |||
365 | if( !strcmp( (*i)->sWord, arg ) ) | ||
366 | return *i; | ||
367 | |||
368 | if( (*i)->val.isSet() ) | ||
369 | { | ||
370 | if( !strncmp( (*i)->sWord, arg, (*i)->sWord.getLength() ) && | ||
371 | arg[(*i)->sWord.getLength()] == '=' ) | ||
372 | { | ||
373 | return *i; | ||
374 | } | ||
375 | } | ||
376 | } | ||
377 | |||
378 | return NULL; | ||
379 | } | ||
380 | |||
381 | ParamProc::ArgSpec *ParamProc::checkLetr( const char arg ) | ||
382 | { | ||
383 | printf("Checking \'%c\'...\n", arg ); | ||
384 | std::list<ArgSpec *>::const_iterator i; | ||
385 | for( i = lArg.begin(); i != lArg.end(); i++ ) | ||
386 | { | ||
387 | if( (*i)->cChar == '\0' ) | ||
388 | continue; | ||
389 | |||
390 | if( (*i)->cChar == arg ) | ||
391 | { | ||
392 | return *i; | ||
393 | } | ||
394 | } | ||
395 | |||
396 | return NULL; | ||
397 | } | ||
398 | |||
diff --git a/src/paramproc.h b/src/paramproc.h new file mode 100644 index 0000000..b7450b7 --- /dev/null +++ b/src/paramproc.h | |||
@@ -0,0 +1,139 @@ | |||
1 | #ifndef PARAM_PROC_H | ||
2 | #define PARAM_PROC_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | #include <string> | ||
6 | #include <list> | ||
7 | #include "staticstring.h" | ||
8 | |||
9 | class ParamProc | ||
10 | { | ||
11 | public: | ||
12 | class ParamPtr | ||
13 | { | ||
14 | public: | ||
15 | ParamPtr(); | ||
16 | ParamPtr( std::string *str ); | ||
17 | ParamPtr( uint64_t *uint64 ); | ||
18 | ParamPtr( uint32_t *uint32 ); | ||
19 | ParamPtr( uint16_t *uint16 ); | ||
20 | ParamPtr( uint8_t *uint8 ); | ||
21 | ParamPtr( int64_t *int64 ); | ||
22 | ParamPtr( int32_t *int32 ); | ||
23 | ParamPtr( int16_t *int16 ); | ||
24 | ParamPtr( int8_t *int8 ); | ||
25 | ParamPtr( float *float32 ); | ||
26 | ParamPtr( double *float64 ); | ||
27 | ParamPtr( long double *float96 ); | ||
28 | ParamPtr( bool *bln ); | ||
29 | |||
30 | enum | ||
31 | { | ||
32 | vtunset, | ||
33 | vtstr, | ||
34 | vtuint64, | ||
35 | vtuint32, | ||
36 | vtuint16, | ||
37 | vtuint8, | ||
38 | vtint64, | ||
39 | vtint32, | ||
40 | vtint16, | ||
41 | vtint8, | ||
42 | vtfloat32, | ||
43 | vtfloat64, | ||
44 | vtfloat96, | ||
45 | vtbln, | ||
46 | }; | ||
47 | ParamPtr &operator=( ParamPtr &ptr ); | ||
48 | ParamPtr &operator=( const char *str ); | ||
49 | |||
50 | bool isSet(); | ||
51 | |||
52 | private: | ||
53 | int type; | ||
54 | union | ||
55 | { | ||
56 | std::string *str; | ||
57 | uint64_t *uint64; | ||
58 | uint32_t *uint32; | ||
59 | uint16_t *uint16; | ||
60 | uint8_t *uint8; | ||
61 | int64_t *int64; | ||
62 | int32_t *int32; | ||
63 | int16_t *int16; | ||
64 | int8_t *int8; | ||
65 | float *float32; | ||
66 | double *float64; | ||
67 | long double *float96; | ||
68 | bool *bln; | ||
69 | } val; | ||
70 | }; | ||
71 | |||
72 | typedef int (ParamProc::*Proc)( int, char *[] ); | ||
73 | |||
74 | typedef struct ArgSpec | ||
75 | { | ||
76 | uint8_t nFlags; | ||
77 | StaticString sWord; | ||
78 | char cChar; | ||
79 | Proc proc; | ||
80 | ParamProc::ParamPtr val; | ||
81 | StaticString sExtra; | ||
82 | StaticString sDesc; | ||
83 | StaticString sValue; | ||
84 | } ArgSpec; | ||
85 | |||
86 | public: | ||
87 | ParamProc(); | ||
88 | virtual ~ParamProc(); | ||
89 | |||
90 | void addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val, | ||
91 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
92 | const char *lpValue=NULL | ||
93 | ); | ||
94 | void addParam( const char *lpWord, char cChar, Proc proc, | ||
95 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
96 | const char *lpValue=NULL | ||
97 | ); | ||
98 | void addParam( const char *lpWord, char cChar, ParamPtr val, | ||
99 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
100 | const char *lpValue=NULL | ||
101 | ); | ||
102 | |||
103 | void addParam( const char *lpWord, Proc proc, ParamPtr val, | ||
104 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
105 | const char *lpValue=NULL | ||
106 | ); | ||
107 | void addParam( const char *lpWord, Proc proc, | ||
108 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
109 | const char *lpValue=NULL | ||
110 | ); | ||
111 | void addParam( const char *lpWord, ParamPtr val, | ||
112 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
113 | const char *lpValue=NULL | ||
114 | ); | ||
115 | |||
116 | void addParam( char cChar, Proc proc, ParamPtr val, | ||
117 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
118 | const char *lpValue=NULL | ||
119 | ); | ||
120 | void addParam( char cChar, Proc proc, | ||
121 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
122 | const char *lpValue=NULL | ||
123 | ); | ||
124 | void addParam( char cChar, ParamPtr val, | ||
125 | const char *lpDesc=NULL, const char *lpExtra=NULL, | ||
126 | const char *lpValue=NULL | ||
127 | ); | ||
128 | |||
129 | void process( int argc, char *argv[] ); | ||
130 | ArgSpec *checkWord( const char *arg ); | ||
131 | ArgSpec *checkLetr( const char arg ); | ||
132 | |||
133 | private: | ||
134 | std::list<ArgSpec *> lArg; | ||
135 | }; | ||
136 | |||
137 | #define mkproc( cls ) static_cast<int (ParamProc::*)( int, char *[])>(&cls) | ||
138 | |||
139 | #endif | ||
diff --git a/src/pproc.cpp b/src/pproc.cpp deleted file mode 100644 index eb52913..0000000 --- a/src/pproc.cpp +++ /dev/null | |||
@@ -1,169 +0,0 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <string.h> | ||
3 | #include "pproc.h" | ||
4 | |||
5 | void pprocHelp( PPROC *pproc ) | ||
6 | { | ||
7 | int maxlen = 0; | ||
8 | for( int j = 0; pproc[j].proc || pproc[j].stateVar; j++ ) | ||
9 | { | ||
10 | int len = strlen( pproc[j].lpWord ); | ||
11 | if( len > maxlen ) maxlen = len; | ||
12 | } | ||
13 | |||
14 | char fmt[100]; | ||
15 | sprintf( fmt, " -%%c, --%%-%ds %%s\n", maxlen ); | ||
16 | |||
17 | for( int j = 0; pproc[j].proc || pproc[j].stateVar; j++ ) | ||
18 | { | ||
19 | printf( fmt, | ||
20 | pproc[j].cChar, | ||
21 | pproc[j].lpWord, | ||
22 | pproc[j].shortHelp ); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | void grabParamAsData( PPROC *pproc, char *str, int *aind, int *cind ) | ||
27 | { | ||
28 | switch( pproc->cMode & PPROC_TYPE ) | ||
29 | { | ||
30 | case PPROC_BOOL_TRUE: | ||
31 | *((bool *)pproc->stateVar) = true; | ||
32 | break; | ||
33 | |||
34 | case PPROC_BOOL_FALSE: | ||
35 | *((bool *)pproc->stateVar) = false; | ||
36 | break; | ||
37 | |||
38 | case PPROC_BOOL_TOGGLE: | ||
39 | *((bool *)pproc->stateVar) = !(*((bool *)pproc->stateVar)); | ||
40 | break; | ||
41 | |||
42 | case PPROC_CHAR: | ||
43 | (*cind)++; | ||
44 | *((char *)pproc->stateVar) = str[(*cind)]; | ||
45 | break; | ||
46 | |||
47 | case PPROC_SHORT: | ||
48 | break; | ||
49 | |||
50 | case PPROC_LONG: | ||
51 | break; | ||
52 | |||
53 | case PPREC_LONG_LONG: | ||
54 | break; | ||
55 | |||
56 | case PPROC_UCHAR: | ||
57 | break; | ||
58 | |||
59 | case PPROC_USHORT: | ||
60 | break; | ||
61 | |||
62 | case PPROC_ULONG: | ||
63 | break; | ||
64 | |||
65 | case PPREC_ULONG_LONG: | ||
66 | break; | ||
67 | |||
68 | case PPROC_FLOAT: | ||
69 | break; | ||
70 | |||
71 | case PPROC_DOUBLE: | ||
72 | break; | ||
73 | |||
74 | case PPROC_LONG_DOUBLE: | ||
75 | break; | ||
76 | |||
77 | case PPROC_STRING: | ||
78 | strcpy( (char *)(pproc->stateVar), str ); | ||
79 | (*aind)++; | ||
80 | break; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | void processParams( int argc, char *argv[], PPROC *pproc ) | ||
85 | { | ||
86 | bool bUsed; | ||
87 | // Loop over all the params except the first, no params, no looping! | ||
88 | for( int j = 1; j < argc; j++ ) | ||
89 | { | ||
90 | //printf("Param[%d]: \"%s\"\n", j, argv[j] ); | ||
91 | if( argv[j][0] == '-' ) | ||
92 | { | ||
93 | if( argv[j][1] == '-' ) | ||
94 | { | ||
95 | bUsed = false; | ||
96 | // Proccess a long-word param string | ||
97 | for( int k = 0; | ||
98 | pproc[k].proc != NULL || pproc[k].stateVar != NULL; | ||
99 | k++ ) | ||
100 | { | ||
101 | if( !strcmp( pproc[k].lpWord, &argv[j][2] ) ) | ||
102 | { | ||
103 | bUsed = true; | ||
104 | if( pproc[k].proc != NULL ) | ||
105 | { | ||
106 | j += pproc[k].proc( argc-j, &argv[j] ); | ||
107 | } | ||
108 | if( pproc[k].stateVar != NULL ) | ||
109 | { | ||
110 | grabParamAsData( &pproc[k], argv[j+1], &j, &k ); | ||
111 | } | ||
112 | break; | ||
113 | } | ||
114 | } | ||
115 | if( !bUsed ) | ||
116 | { | ||
117 | if( !strcmp( "help", &argv[j][2] ) ) | ||
118 | { | ||
119 | pprocHelp( pproc ); | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | else | ||
124 | { | ||
125 | bUsed = false; | ||
126 | // Process a one char param string | ||
127 | for( int k = 0; | ||
128 | pproc[k].proc != NULL || pproc[k].stateVar != NULL; | ||
129 | k++ ) | ||
130 | { | ||
131 | if( pproc[k].cChar == argv[j][1] ) | ||
132 | { | ||
133 | bUsed = true; | ||
134 | if( pproc[k].proc != NULL ) | ||
135 | { | ||
136 | j += pproc[k].proc( argc-j, &argv[j] ); | ||
137 | } | ||
138 | if( pproc[k].stateVar != NULL ) | ||
139 | { | ||
140 | int tmp = 1; | ||
141 | if( argv[j][2] == '\0' ) | ||
142 | { | ||
143 | grabParamAsData( &pproc[k], argv[j+1], &j, &tmp ); | ||
144 | } | ||
145 | else | ||
146 | { | ||
147 | j--; | ||
148 | grabParamAsData( &pproc[k], (&argv[j+1][2]), &j, &tmp ); | ||
149 | } | ||
150 | } | ||
151 | break; | ||
152 | } | ||
153 | } | ||
154 | if( !bUsed ) | ||
155 | { | ||
156 | if( argv[j][1] == 'h' ) | ||
157 | { | ||
158 | pprocHelp( pproc ); | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | else | ||
164 | { | ||
165 | // Handle generic params here. | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | |||
diff --git a/src/pproc.h b/src/pproc.h deleted file mode 100644 index 31d7c02..0000000 --- a/src/pproc.h +++ /dev/null | |||
@@ -1,65 +0,0 @@ | |||
1 | #ifndef PPROC_H_ | ||
2 | #define PPROC_H_ | ||
3 | |||
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 | /** | ||
32 | * Contains all required info to handle a single program parameter. | ||
33 | *@author Mike Buland | ||
34 | */ | ||
35 | typedef struct PPROC | ||
36 | { | ||
37 | const char *lpWord; /**< The full text-word to use as a 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 | |||
42 | /** | ||
43 | * Pointer to the function to call when this param is triggered. | ||
44 | *@param argc The number of params after and including the one that | ||
45 | * triggered this call. | ||
46 | *@param argv The array of commandline tokens to use as parameters. | ||
47 | *@returns 0 for everything is ok. A number greater than zero signals that | ||
48 | * this parameter function used n parameters and they should be skipped by | ||
49 | * the processParams function. | ||
50 | */ | ||
51 | int (*proc)( int argc, char *argv[] ); | ||
52 | void *stateVar; /**< A pointer to a variable to set */ | ||
53 | const char *shortHelp; | ||
54 | } PPROC; | ||
55 | |||
56 | /** | ||
57 | * Process command line parameters based on a null-terminated array of PPROC | ||
58 | * structures. | ||
59 | *@param argc Should come straight from your main function's argc. | ||
60 | *@param argv Should come straight from your main function's argv. | ||
61 | *@param pproc The array of params that this function can respond to. | ||
62 | */ | ||
63 | void processParams( int argc, char *argv[], PPROC *pproc ); | ||
64 | |||
65 | #endif /*PPROC_H_*/ | ||
diff --git a/src/test/param.cpp b/src/test/param.cpp new file mode 100644 index 0000000..0641f90 --- /dev/null +++ b/src/test/param.cpp | |||
@@ -0,0 +1,40 @@ | |||
1 | #include "param.h" | ||
2 | #include <stdio.h> | ||
3 | |||
4 | Param::Param() | ||
5 | { | ||
6 | addParam( "name", 's', mkproc( Param::printStuff ), &str ); | ||
7 | //addParam( "name", &str ); | ||
8 | addParam( "job", 'U', mkproc( Param::printStuff ) ); | ||
9 | |||
10 | // --name=Bobo | ||
11 | // --job hello | ||
12 | } | ||
13 | |||
14 | Param::~Param() | ||
15 | { | ||
16 | } | ||
17 | |||
18 | int Param::printStuff( int argc, char *argv[] ) | ||
19 | { | ||
20 | printf("------------%02d-------------\n", argc ); | ||
21 | for( int j = 0; j < argc; j++ ) | ||
22 | { | ||
23 | printf("%d: %s\n", j, argv[j] ); | ||
24 | } | ||
25 | printf("---------------------------\n" ); | ||
26 | printf("SETVAR===\"%s\"\n", str.c_str() ); | ||
27 | |||
28 | return 1; | ||
29 | } | ||
30 | |||
31 | int main( int argc, char *argv[] ) | ||
32 | { | ||
33 | printf("Starting...\n"); | ||
34 | Param p; | ||
35 | p.process( argc, argv ); | ||
36 | |||
37 | //printf("Calling by hand...\n"); | ||
38 | //p.printStuff(); | ||
39 | } | ||
40 | |||
diff --git a/src/test/param.h b/src/test/param.h new file mode 100644 index 0000000..2756b69 --- /dev/null +++ b/src/test/param.h | |||
@@ -0,0 +1,21 @@ | |||
1 | #ifndef PARAM_H | ||
2 | #define PARAM_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | |||
6 | #include "paramproc.h" | ||
7 | |||
8 | class Param : public ParamProc | ||
9 | { | ||
10 | public: | ||
11 | Param(); | ||
12 | virtual ~Param(); | ||
13 | |||
14 | private: | ||
15 | int printStuff( int argc, char *argv[] ); | ||
16 | |||
17 | std::string str; | ||
18 | uint32_t uint32; | ||
19 | }; | ||
20 | |||
21 | #endif | ||
diff --git a/src/test/params.cpp b/src/test/params.cpp deleted file mode 100644 index bb62047..0000000 --- a/src/test/params.cpp +++ /dev/null | |||
@@ -1,33 +0,0 @@ | |||
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, | ||
20 | NULL } | ||
21 | }; | ||
22 | |||
23 | processParams( argc, argv, table ); | ||
24 | |||
25 | printf("Final results:\n"); | ||
26 | printf("\tbOn = %s\n", (bOn ? "true" : "false") ); | ||
27 | printf("\tbOff = %s\n", (bOff ? "true" : "false") ); | ||
28 | printf("\tbTog = %s\n", (bTog ? "true" : "false") ); | ||
29 | printf("\tcChar = '%c'\n", cChar ); | ||
30 | |||
31 | return 0; | ||
32 | } | ||
33 | |||