-
Notifications
You must be signed in to change notification settings - Fork 17
/
Argument.h
613 lines (580 loc) · 18.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#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 <iostream>
#include "OrderedMap.h"
#include "TypeConversion.h"
#include "LineBreaker.h"
#define UNUSED(x) ((void)(x))
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
size_t 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){
int ret = fscanf(fp, "%s", line);
UNUSED(ret);
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(FILE* fp){
assert(fp);
this->WriteToFileWithComment(fp, "");
};
void WriteToFile(const char* fileName) {
this->WriteToFileWithComment(fileName, "");
};
void WriteToFileWithComment(FILE* fp, const char* comment) {
assert(fp);
if (strlen(comment) > 0) {
fprintf(fp, "# %s\n", comment);
} else {
char username[128] = "unknown_user";
char* pUsername = getenv("LOGNAME");
if (pUsername != NULL) {
strncpy(username, pUsername, 128);
};
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",
username, hostName, ctime(&tt));
}
int numFlagOutputted = 0;
for (size_t 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 (size_t 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");
}
// 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");
assert(fp);
this->WriteToFileWithComment(fp, comment);
fclose(fp);
};
/**
* WriteToStreamWithComment() is EXACTLY the rewrite of WriteToFileWithComment()
* This repetative work is for output LOG file
*/
void WriteToStreamWithComment(std::ostream& fout, const char* comment) {
if (strlen(comment) > 0) {
fout << "# " << comment << "\n";
} else {
char username[128] = "unknown_user";
char* pUsername = getenv("LOGNAME");
if (pUsername != NULL) {
strncpy(username, pUsername, 128);
};
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
fout << "# ParameterList created by " << username << " on "<< hostName << " at " << ctime(&tt);
}
int numFlagOutputted = 0;
for (size_t i = 0; i < this->flagVec.size(); i++){
FlagInfo& fi = this->flagInfoMap[i];
if (fi.isParsed) {
// separate different flags
if (numFlagOutputted)
fout << " ";
if (fi.isLongParam)
fout << "--";
else
fout << "-";
fout << flagVec[i];
switch(fi.pt) {
case BOOL_TYPE:
break;
case INT_TYPE:
fout << " " << *(int*)fi.data;
break;
case DOUBLE_TYPE:
fout << " " << *(double*)fi.data;
break;
case STRING_TYPE:
fout << " \""<< *((std::string*)fi.data) << "\"";
break;
default:
fprintf(stderr, "WARNING: Unrecognized parameter type for flag \"%s\"\n", this->flagVec[i].c_str());
return;
}
numFlagOutputted ++;
}
}
for (size_t i = 0; i < this->ptrRemainingArg->size(); i++) {
// separate different flags
if (numFlagOutputted)
fout << " ";
// output each remaining argument
fout << " \"" << (*this->ptrRemainingArg)[i] << "\"";
}
fout << "\n";
}
void WriteToStream(std::ostream& fout) {
this->WriteToStreamWithComment(fout, "");
}
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(stderr, "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(stderr, "The following parameters are available. Ones with \"[]\" are in effect:\n");
fprintf(stderr, "\n");
fprintf(stderr, "Available Options\n");
/*
Format illustration:
Individual Filter : --indvDepthMin [], --indvDepthMax [], --indvQualMin []
<- group name widt -><- SEP -><----------- flag name width ------------------------->
GROUP_WIDTH FLAG_WIDTH
*/
LineBreaker leftColumn(FLAG_WIDTH);
LineBreaker rightColumn(DOC_WIDTH);
rightColumn.setSeparator(",");
std::string left;
std::string right;
std::string rightItem;
for (size_t groupIndex = 0; groupIndex < this->groupNameFlagIndexMap.size(); ++groupIndex){
std::string k;
std::vector<unsigned int> v;
this->groupNameFlagIndexMap.at(groupIndex, &k, &v);
// process group header
left = k;
left += " :";
right.clear();
for (size_t i = 0; i < v.size(); i++) { // loop flags under the same param group
int idx = v[i];
const std::string& flag = this->flagVec[idx];
FlagInfo &fi = this->flagInfoMap[idx];
void* data = fi.data;
if (fi.isLongParam) {
rightItem += " --";
rightItem += flag;
if (fi.pt != BOOL_TYPE || fi.isParsed) {
rightItem += " [";
}
} else {
rightItem += " -";
rightItem += flag;
if (fi.pt != BOOL_TYPE || fi.isParsed) {
rightItem += " [";
}
}
if (fi.isParsed) {
switch(fi.pt){
case BOOL_TYPE:
if (*(bool*)data) {
rightItem += "true";
} else {
rightItem += "false";
}
break;
case INT_TYPE:
rightItem += toString(*(int*)data);
break;
case DOUBLE_TYPE:
rightItem += toString(*(double*)data);
break;
case STRING_TYPE:
rightItem += *(std::string*)data;
break;
default:
fprintf(stderr, "ERROR: That should be a bug, report to [email protected]");
return;
}
}
if (fi.pt != BOOL_TYPE || fi.isParsed) {
rightItem += "]";
}
right += rightItem;
right += ",";
rightItem.clear();
} // end loop per param group
// fprintf(stderr, "\n");
leftColumn.setContent(left);
rightColumn.setContent(right);
printTwoColumn(stderr, leftColumn, rightColumn, "");
}
};
void Help() {
LineBreaker leftColumn(FLAG_WIDTH);
LineBreaker rightColumn(DOC_WIDTH);
std::string left;
for (size_t 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(stderr, "%s\n", k.c_str());
for (size_t i = 0; i < v.size(); i++) {
int idx = v[i];
const std::string& flag = this->flagVec[idx];
FlagInfo &fi = this->flagInfoMap[idx];
if (fi.isLongParam) {
left = "--";
left += flag;
} else {
left = "-";
left += flag;
}
left += " :";
leftColumn.setContent(left);
rightColumn.setContent(fi.doc);
printTwoColumn(stderr, leftColumn, rightColumn, " ");
}
}
};
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;
const static int FLAG_WIDTH = 25;
const static int DOC_WIDTH = 55;
};
#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);
////////////////////////////////////////////////////////////////////////////////
// Helper functions
static void REQUIRE_STRING_PARAMETER(const std::string& flag, const char* msg){
if (flag.size() == 0){
fprintf(stderr, "%s\n", msg);
abort();
}
};
#endif /* _ARGUMENT_H_ */