/
JKArgs.cpp
127 lines (118 loc) · 1.85 KB
/
JKArgs.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "JKArgs.h"
JKArgs::
JKArgs(int argc,char *argv[])
{
init(argc,argv);
}
JKArgs::
JKArgs(const char * filename)
{
init(filename);
}
void
JKArgs::
init(int argc,char *argv[])
{
for(int i=1;i<argc;i++)
{
process_one_arg(argv[i]);
}
}
void
JKArgs::
init(const char *filename)
{
ifstream file;
file.open(filename);
if(!file.good())
{
cerr<<"open "<<filename<<" failed"<<endl;
file.close();
return;
}
else
cerr<<"open "<<filename<<" succeed"<<endl;
string arg="";
for(;!file.eof();)
{
arg="";
getline(file,arg);
//cerr<<arg<<endl;
if(arg!="")
process_one_arg(arg);
/* if(file.badbit||file.failbit)
{
file.clear();
file.close();
file.open(filename);
}
*/
}
file.close();
}
void
JKArgs::
process_one_arg(string arg)
{
string value;
size_t pos;
//cerr<<"processing:"<<arg<<endl;
//cerr<<arg<<endl;
while(arg.size()>0&&arg[0]==' ')
arg.erase(0,1);
if(arg=="")return;
while(!isgraph(arg[arg.length()-1]))
{
//cerr<<"erase "<<arg[arg.length()-1]<<":"<<endl;
arg.erase(arg.length()-1,1);
if(arg.length()==0)return;
}
if(arg[0]=='-'||arg[0]=='/')
{
arg.erase(0,1);
}
else return;
pos=arg.find("=");
if(pos==string::npos)
{
value="";
}
else
{
value=arg.substr(pos+1,arg.size()-pos-1);
arg.erase(pos,arg.size()-pos);
}
//cerr<<arg<<" = "<<value<<endl;
argsPool[arg]=value;
}
JKArgs::
~JKArgs()
{
argsPool.clear();
}
bool
JKArgs::
is_set(string arg)
{
if(argsPool.find(arg)==argsPool.end())
return false;
else return true;
}
string
JKArgs::
value(string arg)
{
if(argsPool.find(arg)==argsPool.end())
return "";
else return argsPool[arg];
}
void
JKArgs::
print(ostream& os)const
{
map<string,string>::const_iterator citer=argsPool.begin();
for(;citer!=argsPool.end();citer++)
{
os<<"-"<<citer->first<<"="<<citer->second<<endl;
}
}