-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpiOptions.cpp
544 lines (469 loc) · 16.4 KB
/
piOptions.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//
// myOptions.cpp
// ParticlesGUI
//
// Created by Joohwi Lee on 2/11/13.
//
//
#include "piOptions.h"
#include "sstream"
#include "iostream"
#include "fstream"
using namespace std;
namespace pi {
void Options::SetBool(std::string name, bool value) {
std::pair<BoolMap::iterator, bool> result = _boolMap.insert(BoolPair(name, value));
if (!result.second) {
result.first->second = value;
}
}
void Options::SetInt(std::string name, int value) {
std::pair<IntMap::iterator, bool> result = _intMap.insert(IntPair(name, value));
if (!result.second) {
result.first->second = value;
}
}
void Options::SetReal(std::string name, OptionReal value) {
std::pair<RealMap::iterator, bool> result = _realMap.insert(RealPair(name, value));
if (!result.second) {
result.first->second = value;
} }
void Options::SetString(std::string name, std::string value) {
std::pair<StringMap::iterator, bool> result = _stringMap.insert(StringPair(name, value));
if (!result.second) {
result.first->second = value;
}
}
void Options::AppendString(std::string name, std::string value) {
_stringVectorMap[name].push_back(value);
}
void Options::AppendReal(std::string name, OptionReal value) {
_realVectorMap[name].push_back(value);
}
void Options::AppendInt(std::string name, int value) {
_intVectorMap[name].push_back(value);
}
bool Options::GetBoolTo(std::string name, bool& var) {
if (_boolMap.find(name) == _boolMap.end()) {
return false;
}
var = _boolMap.at(name);
return true;
}
bool Options::GetIntTo(std::string name, int& var) {
if (_intMap.find(name) == _intMap.end()) {
return false;
}
var = _intMap.at(name);
return true;
}
bool Options::GetRealTo(std::string name, OptionReal& var) {
if (_realMap.find(name) == _realMap.end()) {
return false;
}
var = _realMap.at(name);
return true;
}
bool Options::GetStringTo(std::string name, std::string& var) {
if (_stringMap.find(name) == _stringMap.end()) {
return false;
}
var = _stringMap.at(name);
return true;
}
bool Options::GetStringVectorValueTo(std::string name, int n, std::string& out) {
StringVector& vector = _stringVectorMap[name];
if (vector.size() > n) {
out = vector[n];
return true;
}
return false;
}
bool Options::GetIntVectorValueTo(std::string name, int n, int& out) {
IntVector& vector = _intVectorMap[name];
if (vector.size() > n) {
out = vector[n];
return true;
}
return false;
}
bool Options::GetRealVectorValueTo(std::string name, int n, OptionReal& out) {
RealVector& vector = _realVectorMap[name];
if (vector.size() > n) {
out = vector[n];
return true;
}
return false;
}
bool Options::GetBool(std::string name, bool def) {
if (_boolMap.find(name) == _boolMap.end()) {
return def;
} else {
return _boolMap.at(name);
}
}
int Options::GetInt(std::string name, int def) {
if (_intMap.find(name) == _intMap.end()) {
return def;
} else {
return _intMap.at(name);
}
}
int Options::GetStringAsInt(std::string name, int def) {
string val = GetString(name, "");
if (val != "") {
return atoi(val.c_str());
}
return def;
}
OptionReal Options::GetReal(std::string name, OptionReal def) {
if (_realMap.find(name) == _realMap.end()) {
return def;
} else {
return _realMap.at(name);
}
}
OptionReal Options::GetStringAsReal(std::string name, OptionReal def) {
string val = GetString(name, "");
if (val != "") {
return atof(val.c_str());
}
return def;
}
bool Options::HasString(std::string name) {
return _stringMap.find(name) != _stringMap.end();
}
std::string Options::GetString(std::string name, std::string def) {
if (_stringMap.find(name) == _stringMap.end()) {
return def;
} else {
return _stringMap.at(name);
}
}
StringVector& Options::GetStringVector(std::string name) {
return _stringVectorMap[name];
}
std::string Options::GetStringVectorValue(std::string name, int n, std::string def) {
StringVector& vector = _stringVectorMap[name];
if (vector.size() > n) {
return vector[n];
}
return def;
}
RealVector& Options::GetRealVector(std::string name) {
return _realVectorMap[name];
}
OptionReal Options::GetRealVectorValue(std::string name, int nth, OptionReal def) {
RealVector& vector = _realVectorMap[name];
if (vector.size() > nth) {
return vector[nth];
}
return def;
}
IntVector& Options::GetIntVector(std::string name) {
return _intVectorMap[name];
}
int Options::GetIntVectorValue(std::string name, int nth, int def) {
IntVector& vector = _intVectorMap[name];
if (vector.size() > nth) {
return vector[nth];
}
return def;
}
std::string Options::GetConfigFile() {
if (GetString("--config") != "") {
return GetString("--config");
} else {
bool hasConfigFile = false;
ifstream in("config.txt");
hasConfigFile = in.is_open();
in.close();
if (hasConfigFile) {
return string("config.txt");
}
}
return "";
}
StringVector& Options::ParseOptions(int argc, char* argv[], CSimpleOpt::SOption* optionSpecs) {
const CSimpleOpt::SOption endOption= { -1, NULL, SO_NONE };
if (optionSpecs == NULL) {
if (_specs.size() == 0 || _specs.back().nId > -1) {
_specs.push_back(endOption);
}
optionSpecs = &_specs[0];
for (int i = 0; i < _specs.size(); i++) {
optionSpecs[i].pszArg = _specNames[i].c_str();
}
}
CSimpleOpt args(argc, argv, optionSpecs);
while (args.Next()) {
if (args.LastError() == SO_SUCCESS) {
if (args.OptionArg()) {
this->SetString(args.OptionText(), string(args.OptionArg()));
} else {
this->SetBool(args.OptionText(), true);
}
} else {
// handle error (see the error codes - enum ESOError)
std::cout << "ERROR:" << args.LastError() << std::endl;
}
}
for (int i = 0; i < args.FileCount(); i++) {
this->AppendString("args", args.File(i));
}
return this->GetStringVector("args");
}
std::ostream & operator<<(std::ostream &os, const Options& opt) {
bool hasValue = false;
for(Options::BoolMap::const_iterator iter = opt._boolMap.begin(); iter != opt._boolMap.end(); ++iter) {
if (!hasValue) {
os << "Options:";
hasValue = true;
}
if (iter->first[0] != '_') {
os << " " << (iter->second ? "+" : "-") << iter->first;
}
}
if (hasValue) {
os << endl;
hasValue = false;
}
for(Options::IntMap::const_iterator iter = opt._intMap.begin(); iter != opt._intMap.end(); ++iter) {
os << iter->first << " int " << iter->second << endl;
}
for(Options::RealMap::const_iterator iter = opt._realMap.begin(); iter != opt._realMap.end(); ++iter) {
os << iter->first << " real " << iter->second << endl;
}
for(Options::StringMap::const_iterator iter = opt._stringMap.begin(); iter != opt._stringMap.end(); ++iter) {
os << iter->first << " string " << iter->second << endl;
}
for(Options::IntVectorMap::const_iterator iter = opt._intVectorMap.begin(); iter != opt._intVectorMap.end(); ++iter) {
IntVector vector = iter->second;
os << iter->first << " ints";
for (int i = 0; i < vector.size(); i++) {
os << " " << vector[i];
}
os << endl;
}
for(Options::RealVectorMap::const_iterator iter = opt._realVectorMap.begin(); iter != opt._realVectorMap.end(); ++iter) {
RealVector vector = iter->second;
os << iter->first << " reals";
for (int i = 0; i < vector.size(); i++) {
os << " " << vector[i];
}
os << endl;
}
for(Options::StringVectorMap::const_iterator iter = opt._stringVectorMap.begin(); iter != opt._stringVectorMap.end(); ++iter) {
StringVector vector = iter->second;
os << iter->first << " strings " << vector.size() << endl;
for (int i = 0; i < vector.size(); i++) {
os << vector[i] << endl;
}
}
return os;
}
std::istream & operator>>(std::istream &is, Options& opt) {
char *cbuf = new char[10240];
while (is.good()) {
is.getline(cbuf, 10240);
if (is.good()) {
string line(cbuf);
stringstream ss(line);
string name;
string type;
ss >> name;
// skip whitespace
if (name[0] == '#' || name[0] == '%') {
continue;
}
if (name == "Options:") {
while (ss.good()) {
string option;
ss >> option;
if (option != "") {
bool value = (option[0] == '+');
string optionname = option.substr(1);
opt.SetBool(optionname, value);
}
}
}
ss >> type;
if (name == OPTION_END) {
return is;
}
if (type == "int" ) {
int value;
ss >> value;
opt.SetInt(name, value);
} else if (type == "real") {
OptionReal value;
ss >> value;
opt.SetReal(name, value);
} else if (type == "bool") {
string value;
ss >> value;
opt.SetBool(name, value == "true");
} else if (type == "string") {
string value;
ss >> value;
opt.SetString(name, value);
} else if (type == "ints") {
while (ss.good()) {
int val;
ss >> val;
opt.AppendInt(name, val);
}
} else if (type == "reals") {
while (ss.good()) {
OptionReal val;
ss >> val;
opt.AppendReal(name, val);
}
} else if (type == "strings") {
int len;
ss >> len;
for (int i = 0; i < len; i++) {
char sbuf[1024];
is.getline(sbuf, sizeof(sbuf));
if (is.good()) {
opt.AppendString(name, sbuf);
}
}
}
}
}
delete[] cbuf;
return is;
}
StringVector Options::GetSplitString(std::string name, std::string tok, std::string def) {
std::string val = GetString(name, def);
return Split(val, tok);
}
IntVector Options::GetSplitIntVector(std::string name, std::string tok) {
StringVector data = GetSplitString(name, tok);
IntVector intVector;
for (int i = 0; i < data.size(); i++) {
intVector.push_back(atoi(data[i].c_str()));
}
return intVector;
}
DoubleVector Options::GetSplitDoubleVector(std::string name, std::string tok) {
StringVector data = GetSplitString(name, tok);
DoubleVector doubleVector;
for (int i = 0; i < data.size(); i++) {
doubleVector.push_back(atof(data[i].c_str()));
}
return doubleVector;
}
IntVector Options::GetStringAsIntVector(std::string name) {
StringVector data = GetSplitString(name, ",");
IntVector intVector;
for (int i = 0; i < data.size(); i++) {
intVector.push_back(atoi(data[i].c_str()));
}
return intVector;
}
void Options::addOption(std::string name, int argType) {
_specNames.push_back(name);
CSimpleOpt::SOption opt;
opt.nId = _specs.size();
opt.pszArg = _specNames.back().c_str();
opt.nArgType = (_ESOArgType) argType;
_specs.push_back(opt);
}
void Options::addOption(std::string name, string help, int argType) {
_specNames.push_back(name);
CSimpleOpt::SOption opt;
opt.nId = _specs.size();
opt.pszArg = _specNames.back().c_str();
opt.nArgType = (_ESOArgType) argType;
_specs.push_back(opt);
std::pair<StringMap::iterator, bool> result = _specHelpMessages.insert(StringPair(name, help));
if (!result.second) {
result.first->second = help;
}
}
void Options::addOption(std::string name, string help, string usage, int argType) {
_specNames.push_back(name);
CSimpleOpt::SOption opt;
opt.nId = _specs.size();
opt.pszArg = _specNames.back().c_str();
opt.nArgType = (_ESOArgType) argType;
_specs.push_back(opt);
std::pair<StringMap::iterator, bool> result = _specHelpMessages.insert(StringPair(name, help));
if (!result.second) {
result.first->second = help;
}
std::pair<StringMap::iterator, bool> resultUsage = _specUsage.insert(StringPair(name, usage));
if (!resultUsage.second) {
resultUsage.first->second = usage;
}
}
std::string Options::GetOptionHelp(std::string name) {
if (_specHelpMessages.find(name) == _specHelpMessages.end()) {
return "";
} else {
return _specHelpMessages.at(name);
}
}
std::string Options::GetOptionUsage(std::string name) {
if (_specUsage.find(name) == _specUsage.end()) {
return "";
} else {
return _specUsage.at(name);
}
}
StringVector& Options::GetOptionNames() {
return _specNames;
}
// print usage
void Options::PrintUsage() {
StringVector& specs = GetOptionNames();
for (int i = 0; i < specs.size(); i++) {
string name = specs[i];
string help = GetOptionHelp(name);
string usage = GetOptionUsage(name);
cout << "* " << name << endl;
if (help != "") {
cout << "\t* " << help << endl;
}
if (usage != "") {
cout << "\t* *ex)* " << usage << endl;
}
}
}
void Options::main(Options& opts, StringVector& args) {
}
//////////////////////////////////////////////////////////////////////////////////////
//
// STATIC FUNCTIONS
//
//////////////////////////////////////////////////////////////////////////////////////
StringVector Options::Split(std::string str, std::string tok) {
StringVector data;
istringstream is(str);
string part;
while (getline(is, part, tok[0])) {
data.push_back(part);
}
return data;
}
IntVector Options::SplitAsInt(std::string str, char tok) {
IntVector data;
istringstream is(str);
string part;
while (getline(is, part, tok)) {
data.push_back(atoi(part.c_str()));
}
return data;
}
DoubleVector Options::SplitAsDouble(std::string str, char tok) {
DoubleVector data;
istringstream is(str);
string part;
while (getline(is, part, tok)) {
data.push_back(atof(part.c_str()));
}
return data;
}
}