-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseargs.c++
More file actions
57 lines (50 loc) · 1.46 KB
/
parseargs.c++
File metadata and controls
57 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "shared.h++"
#include <cstdio>
#include <cstdlib>
#include <cstddef>
#include <stack>
#include "parseargs.h++"
struct optdata{
optdata(const char *option,int args):option(option),neededargs(args),argc(0),args(NULL){}
const char *option;
int neededargs;
int argc;
const char **args;
};
void parseargs(int argc, const char** argv,bool (&isopt)(const char*,void*),int (&optargs)(const char*,void*),void (&addopt)(const char*,int,const char**,void*),void (&addarg)(const char*,void*),void *data){
int i;
// initialize the option stack
std::stack<optdata> options;
for(i=1;i<argc;i++){
const char* arg=argv[i];
if(isopt(arg,data)){ // the argument is an option
int args=optargs(arg,data);
DEBUGP(OPT_DEBUG,"%i\n",args);
if(args==0){
addopt(arg,0,NULL,data);
}else{
options.push(optdata(arg,args));
}
}else{
if(options.empty()){
DEBUGP(OPT_DEBUG,"arg");
addarg(arg,data);
}else{
// add the arg to the top option on the stack
optdata d=options.top();
d.argc++;
d.args=(const char**)realloc(d.args,d.argc*sizeof(const char*));
d.args[d.argc-1]=arg;
if(d.argc==d.neededargs){
// add the option
addopt(d.option,d.neededargs,d.args,data);
// remove the option on top
options.pop();
}
}
}
}
if(!options.empty()){
// some of the options didn't get enough args
}
}