MiniballSort
Loading...
Searching...
No Matches
CommandLineInterface.cc
Go to the documentation of this file.
2
3using namespace std;
4
6
8 fFlags.clear();
9 fValues.clear();
11 fTypes.clear();
13 fComments.clear();
14
15}
16
17bool CommandLineInterface::CheckFlags( unsigned int argc, char* argv[], const bool& Debug ) {
18
19 unsigned int i,j;
20
21 if( argc == 1 ) {
22
23 for( i = 0; i < fFlags.size(); i++ ) {
24
25 if( fTypes[i].empty() )
26 cout << fComments[i] << endl << endl;
27 }
28
29 cout << "use " << argv[0] << " with following flags:" << endl;
30
31 for( i = 0; i < fFlags.size(); i++ ) {
32
33 if( fTypes[i] == "bool" ) {
34
35 cout << " [" << setw(fMaximumFlagLength+fMaximumTypeLength);
36 cout << left << fFlags[i] << " : " << fComments[i] << "]" << endl;
37
38 }
39
40 else if( !fTypes[i].empty() ) {
41
42 cout << " [" << setw(fMaximumFlagLength) << left << fFlags[i] << " <" ;
43 cout << setw(fMaximumTypeLength) << left << fTypes[i] << ">: " << fComments[i] << "]" << endl;
44
45 }
46
47 }
48
49 return true;
50
51 }
52
53 for(i = 1; i < argc; i++) {
54
55 for(j = 0; j < fFlags.size(); j++) {
56
57 if(argv[i] == fFlags[j]) {
58
59 //bool doesn't need any value to be read
60 if(fTypes[j] == "bool") {
61
62 *((bool*) fValues[j]) = true;
63 break;//found the right flag for this argument so the flag loop can be stopped
64
65 }
66
67 //if not bool check whether there are more arguments (with values) coming
68 else if(i+1 >= argc) {
69
70 cerr<<"Error in CheckFlags, flag "<<fFlags[j]<<" needs additional arguments"<<endl;
71 return false;
72
73 }
74
75 else if(fTypes[j] == "char*") {
76
77 *((char**) fValues[j]) = argv[i+1];
78 i++;
79 break;//found the right flag for this argument so the flag loop can be stopped
80
81 }
82
83 else if(fTypes[j] == "string") {
84
85 *((string*) fValues[j]) = argv[i+1];
86 i++;
87 break;//found the right flag for this argument so the flag loop can be stopped
88
89 }
90
91 else if(fTypes[j] == "int") {
92
93 *((int*) fValues[j]) = atoi(argv[i+1]);
94 i++;
95 break;//found the right flag for this argument so the flag loop can be stopped
96
97 }
98
99 else if(fTypes[j] == "float") {
100
101 *((float*) fValues[j]) = atof(argv[i+1]);
102 i++;
103 break;//found the right flag for this argument so the flag loop can be stopped
104
105 }
106
107 else if(fTypes[j] == "size_t") {
108
109 *((size_t*) fValues[j]) = atoi(argv[i+1]);
110 i++;
111 break;//found the right flag for this argument so the flag loop can be stopped
112
113 }
114
115 else if(fTypes[j] == "long long") {
116
117 *((long long*) fValues[j]) = atoi(argv[i+1]);
118 i++;
119 break;//found the right flag for this argument so the flag loop can be stopped
120
121 }
122
123 else if(fTypes[j] == "double") {
124
125 *((double*) fValues[j]) = atof(argv[i+1])*fFactors[j];
126 i++;
127 break;//found the right flag for this argument so the flag loop can be stopped
128
129 }
130
131 else if(fTypes[j] == "vector<char*>") {
132
133 i++;
134 //as long as there are arguments left and no new flag is found (flags start with -) => read another value
135 while(i < argc) {
136
137 if(argv[i][0] != '-') {
138
139 (*((vector<char*>*)fValues[j])).push_back(argv[i]);
140 i++;
141
142 }
143
144 else break;
145
146 }
147
148 i--;
149 break;//found the right flag for this argument so the flag loop can be stopped
150
151 }
152
153 else if(fTypes[j] == "vector<string>") {
154
155 i++;
156 //as long as there are arguments left and no new flag is found (flags start with -) => read another value
157 while(i < argc) {
158
159 if(argv[i][0] != '-') {
160
161 (*((vector<string>*)fValues[j])).push_back(argv[i]);
162 i++;
163
164 }
165
166 else break;
167
168 }
169
170 i--;
171 break;//found the right flag for this argument so the flag loop can be stopped
172
173 }
174
175 else if(fTypes[j] == "vector<int>") {
176
177 i++;
178 //as long as there are arguments left and no new flag is found (flags start with -) => read another value
179 while(i < argc) {
180
181 if(argv[i][0] != '-') {
182
183 (*((vector<int>*)fValues[j])).push_back(atoi(argv[i]));
184 i++;
185
186 }
187
188 else break;
189
190 }
191
192 i--;
193 break;//found the right flag for this argument so the flag loop can be stopped
194 }
195
196 else if(fTypes[j] == "vector<long long>") {
197
198 i++;
199 //as long as there are arguments left and no new flag is found (flags start with -) => read another value
200 while( i < argc ) {
201
202 if(argv[i][0] != '-') {
203 (*((vector<long long>*)fValues[j])).push_back(atoi(argv[i]));
204 i++;
205 }
206
207 else break;
208
209 }
210
211 i--;
212 break;//found the right flag for this argument so the flag loop can be stopped
213
214 }
215
216 else if( fTypes[j] == "vector<double>" ) {
217 i++;
218 //as long as there are arguments left and no new flag is found (flags start with -) => read another value
219 while( i < argc ) {
220
221 if(argv[i][0] != '-') {
222
223 (*((vector<double>*)fValues[j])).push_back(atof(argv[i])*fFactors[j]);
224 i++;
225
226 }
227
228 else break;
229
230 }
231
232 i--;
233 break;//found the right flag for this argument so the flag loop can be stopped
234
235 }
236
237 }//if(argv[i] == flags[j])
238
239
240 }//for(j = 0; j < flags.size(); j++)
241
242 if( j == fFlags.size() ) { //this means no matching flag was found
243
244 cerr<<"flag "<<argv[i]<<" unknown"<<endl;
245 }
246
247 else if(Debug) cout<<"found flag "<<i<<" = "<<argv[i]<<endl;
248
249 }//for(i = 1; i < argc; i++)
250
251 if(Debug) cout<<*this<<endl;
252
253 return true;
254
255}
256
257ostream& operator <<(ostream &os,const CommandLineInterface &obj) {
258
259 os<<"command line flags are:"<<endl;
260 for( unsigned int i = 0; i < obj.fValues.size(); i++) {
261
262 if(obj.fTypes[i] == "bool")
263 cout<<obj.fFlags[i]<<": "<<*((bool*) obj.fValues[i])<<endl;
264
265
266 else if(obj.fTypes[i] == "char*")
267 cout<<obj.fFlags[i]<<": "<<*((char**) obj.fValues[i])<<endl;
268
269 else if(obj.fTypes[i] == "string")
270 cout<<obj.fFlags[i]<<": "<<*((string*) obj.fValues[i])<<endl;
271
272 else if(obj.fTypes[i] == "int")
273 cout<<obj.fFlags[i]<<": "<<*((int*) obj.fValues[i])<<endl;
274
275 else if(obj.fTypes[i] == "float")
276 cout<<obj.fFlags[i]<<": "<<*((float*) obj.fValues[i])<<endl;
277
278 else if(obj.fTypes[i] == "long long")
279 cout<<obj.fFlags[i]<<": "<<*((long*) obj.fValues[i])<<endl;
280
281 else if(obj.fTypes[i] == "double")
282 cout<<obj.fFlags[i]<<": "<<*((double*) obj.fValues[i])<<endl;
283
284 else if(obj.fTypes[i] == "vector<char*>") {
285
286 cout<<obj.fFlags[i]<<": ";
287 for( unsigned int j = 0; j < ((vector<char*>*) obj.fValues[i])->size(); j++)
288 cout<<(*((vector<char*>*) obj.fValues[i]))[j]<<" ";
289 cout<<endl;
290
291 }
292
293 else if(obj.fTypes[i] == "vector<string>") {
294
295 cout<<obj.fFlags[i]<<": ";
296 for( unsigned int j = 0; j < ((vector<string>*) obj.fValues[i])->size(); j++)
297 cout<<(*((vector<string>*) obj.fValues[i]))[j]<<" ";
298 cout<<endl;
299
300 }
301
302 else if(obj.fTypes[i] == "vector<int>") {
303
304 cout<<obj.fFlags[i]<<": ";
305 for( unsigned int j = 0; j < ((vector<int>*) obj.fValues[i])->size(); j++)
306 cout<<(*((vector<int>*) obj.fValues[i]))[j]<<" ";
307 cout<<endl;
308
309 }
310
311 else if(obj.fTypes[i] == "vector<long long>") {
312
313 cout<<obj.fFlags[i]<<": ";
314 for( unsigned int j = 0; j < ((vector<long long>*) obj.fValues[i])->size(); j++)
315 cout<<(*((vector<long long>*) obj.fValues[i]))[j]<<" ";
316 cout<<endl;
317
318 }
319
320 else if(obj.fTypes[i] == "vector<double>") {
321
322 cout<<obj.fFlags[i]<<": ";
323 for( unsigned int j = 0; j < ((vector<double>*) obj.fValues[i])->size(); j++)
324 cout<<(*((vector<double>*) obj.fValues[i]))[j]<<" ";
325 cout<<endl;
326
327 }
328
329 }
330
331 return os;
332
333}
334
335void CommandLineInterface::Add(const char* comment)
336{
337 fFlags.push_back(string());
338 fValues.push_back((void*) NULL);
339 fTypes.push_back(string());
340 if(strlen(comment) > fMaximumCommentLength)
341 fMaximumCommentLength = strlen(comment);
342 fComments.push_back(string(comment));
343 fFactors.push_back(1.);
344}
345
346void CommandLineInterface::Add(const char* flag, const char* comment, bool* value)
347{
348 if(strlen(flag) > fMaximumFlagLength)
349 fMaximumFlagLength = strlen(flag);
350 fFlags.push_back(string(flag));
351 fValues.push_back((void*) value);
352 if(strlen("bool") > fMaximumTypeLength)
353 fMaximumTypeLength = strlen("bool");
354 fTypes.push_back(string("bool"));
355 if(strlen(comment) > fMaximumCommentLength)
356 fMaximumCommentLength = strlen(comment);
357 fComments.push_back(string(comment));
358 fFactors.push_back(1.);
359}
360
361void CommandLineInterface::Add(const char* flag, const char* comment, char** value)
362{
363 if(strlen(flag) > fMaximumFlagLength)
364 fMaximumFlagLength = strlen(flag);
365 fFlags.push_back(string(flag));
366 fValues.push_back((void*) value);
367 if(strlen("char*") > fMaximumTypeLength)
368 fMaximumTypeLength = strlen("char*");
369 fTypes.push_back(string("char*"));
370 if(strlen(comment) > fMaximumCommentLength)
371 fMaximumCommentLength = strlen(comment);
372 fComments.push_back(string(comment));
373 fFactors.push_back(1.);
374}
375
376void CommandLineInterface::Add(const char* flag, const char* comment, string* value)
377{
378 if(strlen(flag) > fMaximumFlagLength)
379 fMaximumFlagLength = strlen(flag);
380 fFlags.push_back(string(flag));
381 fValues.push_back((void*) value);
382 if(strlen("string") > fMaximumTypeLength)
383 fMaximumTypeLength = strlen("string");
384 fTypes.push_back(string("string"));
385 if(strlen(comment) > fMaximumCommentLength)
386 fMaximumCommentLength = strlen(comment);
387 fComments.push_back(string(comment));
388 fFactors.push_back(1.);
389}
390
391void CommandLineInterface::Add(const char* flag, const char* comment, int* value)
392{
393 if(strlen(flag) > fMaximumFlagLength)
394 fMaximumFlagLength = strlen(flag);
395 fFlags.push_back(string(flag));
396 fValues.push_back((void*) value);
397 if(strlen("int") > fMaximumTypeLength)
398 fMaximumTypeLength = strlen("int");
399 fTypes.push_back(string("int"));
400 if(strlen(comment) > fMaximumCommentLength)
401 fMaximumCommentLength = strlen(comment);
402 fComments.push_back(string(comment));
403 fFactors.push_back(1.);
404}
405
406void CommandLineInterface::Add(const char* flag, const char* comment, float* value)
407{
408 if(strlen(flag) > fMaximumFlagLength)
409 fMaximumFlagLength = strlen(flag);
410 fFlags.push_back(string(flag));
411 fValues.push_back((void*) value);
412 if(strlen("float") > fMaximumTypeLength)
413 fMaximumTypeLength = strlen("float");
414 fTypes.push_back(string("float"));
415 if(strlen(comment) > fMaximumCommentLength)
416 fMaximumCommentLength = strlen(comment);
417 fComments.push_back(string(comment));
418 fFactors.push_back(1.);
419}
420
421void CommandLineInterface::Add(const char* flag, const char* comment, size_t* value)
422{
423 if(strlen(flag) > fMaximumFlagLength)
424 fMaximumFlagLength = strlen(flag);
425 fFlags.push_back(string(flag));
426 fValues.push_back((void*) value);
427 if(strlen("int") > fMaximumTypeLength)
428 fMaximumTypeLength = strlen("size_t");
429 fTypes.push_back(string("size_t"));
430 if(strlen(comment) > fMaximumCommentLength)
431 fMaximumCommentLength = strlen(comment);
432 fComments.push_back(string(comment));
433 fFactors.push_back(1.);
434}
435
436void CommandLineInterface::Add(const char* flag, const char* comment, long long* value)
437{
438 if(strlen(flag) > fMaximumFlagLength)
439 fMaximumFlagLength = strlen(flag);
440 fFlags.push_back(string(flag));
441 fValues.push_back((void*) value);
442 if(strlen("long long") > fMaximumTypeLength)
443 fMaximumTypeLength = strlen("long long");
444 fTypes.push_back(string("long long"));
445 if(strlen(comment) > fMaximumCommentLength)
446 fMaximumCommentLength = strlen(comment);
447 fComments.push_back(string(comment));
448 fFactors.push_back(1.);
449}
450
451void CommandLineInterface::Add(const char* flag, const char* comment, double* value, double factor)
452{
453 if(strlen(flag) > fMaximumFlagLength)
454 fMaximumFlagLength = strlen(flag);
455 fFlags.push_back(string(flag));
456 fValues.push_back((void*) value);
457 if(strlen("double") > fMaximumTypeLength)
458 fMaximumTypeLength = strlen("double");
459 fTypes.push_back(string("double"));
460 if(strlen(comment) > fMaximumCommentLength)
461 fMaximumCommentLength = strlen(comment);
462 fComments.push_back(string(comment));
463 fFactors.push_back(factor);
464}
465
466void CommandLineInterface::Add(const char* flag, const char* comment, vector<char*>* value)
467{
468 if(strlen(flag) > fMaximumFlagLength)
469 fMaximumFlagLength = strlen(flag);
470 fFlags.push_back(string(flag));
471 fValues.push_back((void*) value);
472 if(strlen("vector<char*>") > fMaximumTypeLength)
473 fMaximumTypeLength = strlen("vector<char*>");
474 fTypes.push_back(string("vector<char*>"));
475 if(strlen(comment) > fMaximumCommentLength)
476 fMaximumCommentLength = strlen(comment);
477 fComments.push_back(string(comment));
478 fFactors.push_back(1.);
479}
480
481void CommandLineInterface::Add(const char* flag, const char* comment, vector<string>* value)
482{
483 if(strlen(flag) > fMaximumFlagLength)
484 fMaximumFlagLength = strlen(flag);
485 fFlags.push_back(string(flag));
486 fValues.push_back((void*) value);
487 if(strlen("vector<string>") > fMaximumTypeLength)
488 fMaximumTypeLength = strlen("vector<string>");
489 fTypes.push_back(string("vector<string>"));
490 if(strlen(comment) > fMaximumCommentLength)
491 fMaximumCommentLength = strlen(comment);
492 fComments.push_back(string(comment));
493 fFactors.push_back(1.);
494}
495
496void CommandLineInterface::Add(const char* flag, const char* comment, vector<int>* value)
497{
498 if(strlen(flag) > fMaximumFlagLength)
499 fMaximumFlagLength = strlen(flag);
500 fFlags.push_back(string(flag));
501 fValues.push_back((void*) value);
502 if(strlen("vector<int>") > fMaximumTypeLength)
503 fMaximumTypeLength = strlen("vector<int>");
504 fTypes.push_back(string("vector<int>"));
505 if(strlen(comment) > fMaximumCommentLength)
506 fMaximumCommentLength = strlen(comment);
507 fComments.push_back(string(comment));
508 fFactors.push_back(1.);
509}
510
511void CommandLineInterface::Add(const char* flag, const char* comment, vector<long long>* value)
512{
513 if(strlen(flag) > fMaximumFlagLength)
514 fMaximumFlagLength = strlen(flag);
515 fFlags.push_back(string(flag));
516 fValues.push_back((void*) value);
517 if(strlen("vector<long long>") > fMaximumTypeLength)
518 fMaximumTypeLength = strlen("vector<long long>");
519 fTypes.push_back(string("vector<long long>"));
520 if(strlen(comment) > fMaximumCommentLength)
521 fMaximumCommentLength = strlen(comment);
522 fComments.push_back(string(comment));
523 fFactors.push_back(1.);
524}
525
526void CommandLineInterface::Add(const char* flag, const char* comment, vector<double>* value, double factor)
527{
528 if(strlen(flag) > fMaximumFlagLength)
529 fMaximumFlagLength = strlen(flag);
530 fFlags.push_back(string(flag));
531 fValues.push_back((void*) value);
532 if(strlen("vector<double>") > fMaximumTypeLength)
533 fMaximumTypeLength = strlen("vector<double>");
534 fTypes.push_back(string("vector<double>"));
535 if(strlen(comment) > fMaximumCommentLength)
536 fMaximumCommentLength = strlen(comment);
537 fComments.push_back(string(comment));
538 fFactors.push_back(factor);
539}
ostream & operator<<(ostream &os, const CommandLineInterface &obj)
bool CheckFlags(unsigned int, char *[], const bool &Debug=false)