-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptions.h
249 lines (201 loc) · 5.53 KB
/
options.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**********************************
* Samsonov Dima
* Command line options parser.
* See bottom of file for usage
************************************/
#ifndef __OPTIONS_H
#define __OPTIONS_H
#include <algorithm>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <stdio.h>
using namespace std;
#define MAX_STRING_SIZE 0x100
#define TAB_SIZE 20
static const char *typeNames[]={"int","float","double","string","bool"};
const char* getTypename(int& val) {return typeNames[0];}
const char* getTypename(float& val) {return typeNames[1];}
const char* getTypename(double& val) {return typeNames[2];}
const char* getTypename(string& val) {return typeNames[3];}
const char* getTypename(bool& val) {return typeNames[4];}
class COptions
{
public:
COptions(int pn,char*ps[]) : m_exename(ps[0])
{
for(int i=1;i<pn;i++)
{
m_strings.push_back(string(ps[i]));
}
}
template<class T> void Parse(const char* optString,T & optVar,const char*comment,T defaultval)
{
//~ char HelpString[MAX_STRING_SIZE];
//~ for(int i=0;i<MAX_STRING_SIZE;i++)
//~ {
//~ HelpString[i] = 0;
//~ }
//~ memset(HelpString,0,MAX_STRING_SIZE);
ostringstream ss;
ss.setf(ios::left);
ss <<setw(TAB_SIZE)<<optString
<<setw(TAB_SIZE)<<getTypename(optVar)
<<setw(TAB_SIZE)<<comment
<<setw(TAB_SIZE)<<defaultval;
m_help.push_back(ss.str());
vector<string>::iterator current= find(m_strings.begin(),m_strings.end(),string(optString));
optVar = defaultval;
if(current!=m_strings.end())
{
if(current+1==m_strings.end())
{
m_error.push_back(string("\nUncomplete parm error:"));
m_error.push_back(*current);
m_strings.erase(current,current+1);
m_error.push_back(string("\n"));
return ;
}
if(!Read(*(current+1),optVar))
{
m_error.push_back(string("\nRead argument error:"));
m_error.push_back(*current);
m_error.push_back(*(current+1));
m_error.push_back(string("\n"));
}
m_strings.erase(current,current+2);
}
}
template<class T> void Parse(const char* optString,T & optVar,const char*comment)
{
//~ char HelpString[MAX_STRING_SIZE];
//~ for(int i=0;i<MAX_STRING_SIZE;i++)
//~ {
//~ HelpString[i] = 0;
//~ }
//~ memset(HelpString,0,MAX_STRING_SIZE);
ostringstream ss;
ss.setf(ios::left);
ss <<setw(TAB_SIZE)<<optString
<<setw(TAB_SIZE)<<getTypename(optVar)
<<setw(TAB_SIZE)<<comment
<<setw(TAB_SIZE)<<"No default";
m_help.push_back(ss.str());
vector<string>::iterator current= find(m_strings.begin(),m_strings.end(),string(optString));
//optVar = defaultval;
if(current!=m_strings.end())
{
if(current+1==m_strings.end())
{
m_error.push_back(string("\nUncomplete parm error:"));
m_error.push_back(*current);
m_strings.erase(current,current+1);
m_error.push_back(string("\n"));
return ;
}
if(!Read(*(current+1),optVar))
{
m_error.push_back(string("\nRead argument error:"));
m_error.push_back(*current);
m_error.push_back(*(current+1));
m_error.push_back(string("\n"));
}
m_strings.erase(current,current+2);
}
else
{
m_error.push_back(string("\nParameter must be defined:"));
m_error.push_back(string(optString));
m_error.push_back(string("\n"));
}
}
bool IsOk()
{
return !(m_strings.size()||m_error.size());
}
void Usage(const char* Mess)
{
cerr<<"\n"<<Mess<<"\n";
if(m_strings.size())
{
cerr<<"Unknow Parametr(s) : ";
copy(m_strings.begin(),m_strings.end(),ostream_iterator<string>(cerr," "));
cerr<<"\n";
}
if(m_error.size())
{
copy(m_error.begin(),m_error.end(),ostream_iterator<string>(cerr," "));
cerr<<"\n";
}
cerr<<"\nUsage : ";
cerr<<m_exename<<"\n";
cerr.setf(ios::left);
cerr <<setw(TAB_SIZE)<<"Option String"
<<setw(TAB_SIZE)<<"Type Of Argument"
<<setw(TAB_SIZE)<<"Comment"
<<setw(TAB_SIZE)<<"Default Value"
<<"\n\n";
copy(m_help.begin(),m_help.end(),ostream_iterator<string>(cerr," \n"));
};
private:
vector<string> m_strings;
string m_exename;
vector<string> m_help;
vector<string> m_error;
private:
bool Read(string& str,float&optVar)
{
sscanf(str.c_str(),"%f",&optVar);
return true;
}
bool Read(string& str,double&optVar)
{
sscanf(str.c_str(),"%lf",&optVar);
return true;
}
bool Read(string& str,int&optVar)
{
sscanf(str.c_str(),"%d",&optVar);
return true;
}
bool Read(string& str,bool&optVar)
{
if(str=="on") {optVar = true; return true;}
if(str=="off") {optVar = false;return true;}
if(str=="1") {optVar = true; return true;}
if(str=="0") {optVar = false;return true;}
return false;
}
bool Read(string& str,string&optVar)
{
optVar = str;
return true;
}
};
#ifdef __C_OPTIONS_TEST
void main(int pn,char*ps[])
{
COptions Options(pn,ps);
float ftest;
int itest;
string inputFile;
string outputFile;
bool btest;
try {
Options.Parse("-fov" ,ftest ,"Fild Of View" ,3.1f);
Options.Parse("-in" ,inputFile ,"Input File Name");
Options.Parse("-out" ,outputFile ,"Output File Name" ,string("out.file"));
Options.Parse("-numViews" ,itest ,"Number Of Views" ,984);
Options.Parse("-rf" ,btest ,"RingFix (on,off)" ,true);
}
catch(...)
{
Options.Usage("Catch error");exit(0);
}
if(!Options.IsOk()){Options.Usage("Parse Error");exit(0);}
}
#endif //C_OPTIONS_TEST
#endif //__OPTIONS_H