-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgument.h
527 lines (498 loc) · 19.1 KB
/
Argument.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
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
#ifndef _ARGUMENT_H_
#define _ARGUMENT_H_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // gethostname
#include <string.h> // strlen
#include <time.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include "OrderedMap.h"
#include "TypeConversion.h"
typedef enum PARAMETER_TYPE{
UNSUPPORTED_TYPE,
BOOL_TYPE,
INT_TYPE,
DOUBLE_TYPE,
STRING_TYPE
} ParameterType;
struct FlagInfo{
public:
FlagInfo():
pt(UNSUPPORTED_TYPE),
data(NULL),
isParsed(false),
isLongParam(false) {};
public:
ParameterType pt; // the parameter type
void* data; // where the parsed value stores
std::string doc; // documentation
bool isParsed; // whether we have parsed the flag and store the value in data.
bool isLongParam; // --flag i long parameter, -flag is not
};
class ParameterParser{
public:
ParameterParser(){
this->currentParameterGroupName = "Default Parameter Group";
}
void AddParameterGroup(const char* name) {
// change default group name
this->currentParameterGroupName = name;
};
void AddRemainingArg(void* data) {
this->ptrRemainingArg = (std::vector<std::string>*) data;
};
void AddParameter(ParameterType pt, void* data, const char* flag, const char* doc) {
std::string f = flag;
// check parameter validity
if (!data) {
fprintf(stderr, "ERROR: Invalid data pointer for flag \"%s\".\n", flag);
return;
}
if (f.size() == 0) {
fprintf(stderr, "ERROR: Invalid flag name \"%s\"\n", flag);
return;
}
FlagInfo fi;
// chop flag leading - or --
if (f.size() > 2 && f[0] == '-' && f[1] == '-') {
f = f.substr(2);
fi.isLongParam = true;
} else if (f.size() > 1 && f[0] == '-') {
f = f.substr(1);
fi.isLongParam = false;
} else {
fprintf(stderr, "WARNING: Flag \"%s\" is not a valid declaration, try \"-%s\" or \"--%s\".\n", flag, flag, flag);
return;
}
// prevent double existence
std::vector<std::string>::iterator it;
for (it = flagVec.begin(); it != flagVec.end(); it++) {
if (*it == flag){
fprintf(stderr, "WARNING: Duplicate flag \"%s\" and \"%s\"\n", flag, (*it).c_str());
return;
}
}
// update internal data
unsigned int idx = this->flagVec.size();
this->flagVec.push_back(f);
groupNameFlagIndexMap[this->currentParameterGroupName].push_back(idx);
fi.pt = pt;
fi.data = data;
fi.doc = doc;
flagInfoMap[idx] = fi;
// initialize data
switch(pt) {
case BOOL_TYPE:
*(bool*)data = false;
break;
case INT_TYPE:
*(int*)data = 0;
break;
case DOUBLE_TYPE:
*(double*)data = 0;
break;
case STRING_TYPE:
*(std::string*)data = "";
break;
default:
fprintf(stderr, "WARNING: Unrecognized parameter type for flag \"%s\"\n", flag);
return;
}
};
// read first uncommented line
void ReadFromFile(const char* fileName) {
FILE* fp = fopen(fileName, "r");
if (!fp) {
fprintf(stderr, "ERROR: Cannot open parameter file \"%s\"\n", fileName);
return ;
}
char line[1024];
int index;
int len;
bool isCommentLine = true;
while (isCommentLine){
fscanf(fp, "%s", line);
len = strlen(line);
if (!len) continue;
for (index = 0; index < len; index++) {
if ( line[index] == ' ')
continue;
if ( line[index] == '#')
break;
isCommentLine = false;
break;
}
}
int argLen = 10;
int argc = 1; // we will put each parsed results from argc = 1.
char** argv = (char**) malloc( sizeof(char*) * argc) ;
if (!argv) {
fprintf(stderr, "ERROR: malloc() failed!\n");
return;
}
char prog[] = "DUMMY_PROG";
argv[0] = prog;
// parse line[index ... (len-1)] to argc and argv
std::string arg;
int numPrefixQuote = 0;
char quoteChar = '\"';
for (int i = index; i < len; i++) {
// if inside the quote
if (numPrefixQuote > 0) {
if (line[index] != quoteChar) {
arg.push_back(line[index]);
continue;
} else {
argv[argc] = (char*) malloc(sizeof(char) * arg.size());
if (!argv[argc]) {
fprintf(stderr, "ERROR: malloc() failed!\n");
return;
}
strncpy(argv[argc], arg.c_str(), arg.size());
arg = "";
}
} else {
// not inside the quote
if (line[index] == ' ') {
if (arg.size() == 0)
continue;
argv[argc] = (char*) malloc(sizeof(char) * arg.size());
if (!argv[argc]) {
fprintf(stderr, "ERROR: malloc() failed!\n");
return;
}
strncpy(argv[argc], arg.c_str(), arg.size());
arg = "";
} else {
arg.push_back(line[index]);
}
}
// allocate new space
if (argc == argLen) {
argLen *= 2;
char** temp = (char**)realloc(argv, sizeof(char*)*argLen);
if (!temp) {
fprintf(stderr, "ERROR: realloc() failed!\n");
return;
}
if (argv != temp)
free(argv);
argv = temp;
}
}
// read in parameter
this->Read(argc, argv);
// clean up memory
for (int i = 1; i < argc; ++i) {
if (argv[i])
free(argv[i]);
}
if (argv)
free(argv);
};
void WriteToFile(const char* fileName) {
this->WriteToFileWithComment(fileName, "");
};
// write all transated parameters to @param fileName
// the first line with be @param comment
// or the information when we write to file.
void WriteToFileWithComment(const char* fileName, const char* comment) {
FILE* fp = fopen(fileName, "w");
if (strlen(comment) > 0) {
fprintf(fp, "# %s\n", comment);
} else {
char hostName[128] = "";
if (gethostname(hostName, 128) == 0) {
// succes
} else {
// failed
sprintf(hostName, "Unknown");
}
time_t tt = time(0);
// ctime() will output an extra \n
fprintf(fp, "# ParameterList created by %s on %s at %s",
getlogin(), hostName, ctime(&tt));
}
int numFlagOutputted = 0;
for (int i = 0; i < this->flagVec.size(); i++){
FlagInfo& fi = this->flagInfoMap[i];
if (fi.isParsed) {
// separate different flags
if (numFlagOutputted)
fprintf(fp, " ");
if (fi.isLongParam)
fprintf(fp, "--");
else
fprintf(fp, "-");
fprintf(fp, "%s", flagVec[i].c_str());
switch(fi.pt) {
case BOOL_TYPE:
break;
case INT_TYPE:
fprintf(fp, " %d", *(int*)fi.data);
break;
case DOUBLE_TYPE:
fprintf(fp, " %lf", *(double*)fi.data);
break;
case STRING_TYPE:
fprintf(fp, " \"%s\"", ((std::string*)fi.data)->c_str());
break;
default:
fprintf(stderr, "WARNING: Unrecognized parameter type for flag \"%s\"\n", this->flagVec[i].c_str());
return;
}
numFlagOutputted ++;
}
}
for (int i = 0; i < this->ptrRemainingArg->size(); i++) {
// separate different flags
if (numFlagOutputted)
fprintf(fp, " ");
// output each remaining argument
fprintf(fp, " \"%s\"", (*this->ptrRemainingArg)[i].c_str());
}
fprintf(fp, "\n");
fclose(fp);
};
void Read(int argc, char** argv) {
std::string flag;
for (int i = 1; i < argc; i++) {
// removing leading - or -- for flags
flag = argv[i];
// flag = '--' meaning the end of all parameter
if (flag == "--") {
++i ;
while (i < argc)
this->ptrRemainingArg->push_back(argv[i++]);
return;
}
// flag = '-' meaning standard input, we will put it in the remaingArg
if (flag == "-") {
this->ptrRemainingArg->push_back(argv[i]);
continue;
}
bool isLongParam = false; // I did not make use of this variable.
unsigned int choppedLeadingDash = 0;
// user may input ---flag, ----flag, ..., and we will chop all leading -
// user may also input ---, ----, but I don't understand what that means, so report error
while (flag.size() > 0){
if (flag[0] == '-') {
choppedLeadingDash ++;
flag = flag.substr(1);
} else {
break;
}
}
if (flag.size() > 0) {
if (choppedLeadingDash > 1) {
isLongParam = true;
} else {
isLongParam = false;
}
} else { // (flag.size() == 0
fprintf(stderr, "ERROR: we don't understand the argument \"%s\"\n", argv[i]);
return;
}
// check if variable flag is a predefined flag or not
std::vector<std::string>::iterator it;
for (it = flagVec.begin(); it != flagVec.end(); it++) {
if (*it == flag){
break;
}
}
// variable flag will be added to remainingArg
if (it == this->flagVec.end()) {
this->ptrRemainingArg->push_back(argv[i]);
continue;
}
// NOTE:
// we could check if user misuse -- and -
// but i did not see much use here.
// parse data
unsigned int idx = it - this->flagVec.begin();
void* data = this->flagInfoMap[idx].data;
if (this->flagInfoMap[idx].isParsed) {
fprintf(stdout, "WARNING: flag \"%s\" provided more than once, the previous value will be overwritten\n", argv[i]);
}
this->flagInfoMap[idx].isParsed = true;
switch(this->flagInfoMap[idx].pt) {
case BOOL_TYPE:
*(bool*)data = true;
break;
case INT_TYPE:
if (!str2int(argv[i+1], (int*)data)) {
fprintf(stderr, "WARNING: arg \"%s %s\" does not give valid integer\n", argv[i], argv[i+1]);
}
++i;
break;
case DOUBLE_TYPE:
if (!str2double(argv[i+1], (double*)data)) {
fprintf(stderr, "WARNING: arg \"%s %s\" does not give valid double\n", argv[i], argv[i+1]);
}
++i;
break;
case STRING_TYPE:
*(std::string*)data = argv[++i];
break;
default:
fprintf(stderr, "ERROR: Unrecognized parameter type for flag %s\n", argv[i]);
return;
}
}
};
void Status() {
fprintf(stdout, "The following parameters are available. Ones with \"[]\" are in effect:\n");
fprintf(stdout, "\n");
fprintf(stdout, "Available Options\n");
/*
Format illustration:
Individual Filter : --indvDepthMin [], --indvDepthMax [], --indvQualMin []
<- group name widt -> <----------- flag name width ------------------------->
20 3 55
*/
const int GROUP_WIDTH = 20;
const int FLAG_WIDTH = 55;
const char SEP[] = " : ";
const char EMPTY_SEP[] = " ";
for (unsigned int groupIndex = 0; groupIndex < this->groupNameFlagIndexMap.size(); ++groupIndex){
std::string k;
std::vector<unsigned int> v;
this->groupNameFlagIndexMap.at(groupIndex, &k, &v);
// print group header
fprintf(stdout, "%*s", GROUP_WIDTH, k.c_str());
fprintf(stdout, "%s", SEP);
int availableFlagWidth = FLAG_WIDTH;
char flagBuffer[FLAG_WIDTH * 4] = "";
bool firstFlagInLine = true;
for (unsigned int i = 0; i < v.size(); i++) {
int idx = v[i];
std::string flag = this->flagVec[idx];
FlagInfo &fi = this->flagInfoMap[idx];
void* data = fi.data;
int flagWidth = 0;
// print the flag to the flagBuffer
if (!firstFlagInLine)
flagWidth += sprintf(flagBuffer+flagWidth, ", ");
else
firstFlagInLine = false;
if (fi.isLongParam) {
flagWidth += sprintf(flagBuffer+flagWidth, "--%s [", flag.c_str());
} else {
flagWidth += sprintf(flagBuffer+flagWidth, "-%s [", flag.c_str());
}
if (fi.isParsed) {
switch(fi.pt){
case BOOL_TYPE:
if (*(bool*)data)
flagWidth += sprintf(flagBuffer+flagWidth, "true");
else
flagWidth += sprintf(flagBuffer+flagWidth, "false");
break;
case INT_TYPE:
flagWidth += sprintf(flagBuffer+flagWidth, "%d", *(int*)data);
break;
case DOUBLE_TYPE:
flagWidth += sprintf(flagBuffer+flagWidth, "%lf", *(double*)data);
break;
case STRING_TYPE:
flagWidth += sprintf(flagBuffer+flagWidth, "%s", ((std::string*)data)->c_str());
break;
default:
fprintf(stderr, "ERROR: That should be a bug, report to [email protected]");
return;
}
}
flagWidth += sprintf(flagBuffer+flagWidth, "]");
// if there are not enough spaces and th, we will output a new line
if (availableFlagWidth < flagWidth && flagWidth < FLAG_WIDTH) {
fprintf(stdout, "\n");
fprintf(stdout, "%*s", GROUP_WIDTH, " ");
fprintf(stdout, "%s", EMPTY_SEP);
availableFlagWidth = FLAG_WIDTH;
if (flagBuffer[0] == ',') {
// print flag
fprintf(stdout, "%s", flagBuffer + 2); // 2 is length of ", "
availableFlagWidth -= (flagWidth - 2);
} else {
fprintf(stdout, "%s", flagBuffer); // 2 is length of ", "
availableFlagWidth -= flagWidth;
}
firstFlagInLine = false;
} else {
fprintf(stdout, "%s", flagBuffer);
availableFlagWidth -= flagWidth;
}
}
fprintf(stdout, "\n");
}
};
void Help() {
const int FLAG_WIDTH = 25;
const int DOC_WIDTH = 55;
const char SEP[] = " : ";
const char EMPTY_SEP[] = " ";
for (unsigned int groupIndex = 0; groupIndex < this->groupNameFlagIndexMap.size(); ++groupIndex){
std::string k;
std::vector<unsigned int> v;
this->groupNameFlagIndexMap.at(groupIndex, &k, &v);
// print group header
fprintf(stdout, "%s\n", k.c_str());
for (unsigned int i = 0; i < v.size(); i++) {
int idx = v[i];
std::string flag = this->flagVec[idx];
FlagInfo &fi = this->flagInfoMap[idx];
void* data = fi.data;
int flagWidth = 0;
if (fi.isLongParam) {
fprintf(stdout, "%*s%s%s", (int)(FLAG_WIDTH - flag.size() ), "--", flag.c_str(), SEP);
} else {
fprintf(stdout, "%*s%s%s", (int)(FLAG_WIDTH - flag.size() ), "-", flag.c_str(), SEP);
}
for (int docIndex = 0; docIndex < fi.doc.size(); docIndex++) {
if (docIndex != 0 && docIndex % DOC_WIDTH == 0) {
fprintf(stdout, "\n");
fprintf(stdout, "%*s%s", FLAG_WIDTH, " ", EMPTY_SEP);
}
fprintf(stdout, "%c", fi.doc[docIndex]);
}
fprintf(stdout, "\n");
}
}
};
private:
// all flags are stored here, "--flag" will store "flag" in flagVec
std::vector<std::string> flagVec;
// store flag -> flagInfo
std::map<unsigned int, FlagInfo> flagInfoMap;
// store current group name
std::string currentParameterGroupName;
// store group and flags (their indices in flagVec)that belongs to that group
OrderedMap<std::string, std::vector<unsigned int> > groupNameFlagIndexMap;
// aka. positional argument,
// those not processed argument
std::vector<std::string>* ptrRemainingArg;
};
#define BEGIN_PARAMETER_LIST(pp) \
ParameterParser pp;
#define END_PARAMETER_LIST(pp) \
std::vector<std::string> FLAG_REMAIN_ARG; \
pp.AddRemainingArg(&FLAG_REMAIN_ARG);
// pp is an instane of ParameterParser
#define ADD_PARAMETER_GROUP(pp, x) \
(pp).AddParameterGroup(x);
#define ADD_BOOL_PARAMETER(pp, x, flag, doc) \
bool FLAG_##x; \
(pp).AddParameter(BOOL_TYPE, &FLAG_##x, flag, doc);
#define ADD_INT_PARAMETER(pp, x, flag, doc) \
int FLAG_##x; \
(pp).AddParameter(INT_TYPE, &FLAG_##x, flag, doc);
#define ADD_DOUBLE_PARAMETER(pp, x, flag, doc) \
double FLAG_##x; \
(pp).AddParameter(DOUBLE_TYPE, &FLAG_##x, flag, doc);
#define ADD_STRING_PARAMETER(pp, x, flag, doc) \
std::string FLAG_##x; \
(pp).AddParameter(STRING_TYPE, &FLAG_##x, flag, doc);
#endif /* _ARGUMENT_H_ */