forked from abduld/libwb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbArg.cpp
103 lines (87 loc) · 2.43 KB
/
wbArg.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
#include <wb.h>
EXTERN_C wbArg_t wbArg_new(void) {
wbArg_t arg;
wb_init();
wbArg_setInputCount(arg, 0);
wbArg_setInputFiles(arg, NULL);
wbArg_setOutputFile(arg, NULL);
wbArg_setType(arg, NULL);
wbArg_setExpectedOutputFile(arg, NULL);
return arg;
}
EXTERN_C void wbArg_delete(wbArg_t arg) {
if (wbArg_getInputCount(arg) > 0 && wbArg_getInputFiles(arg) != NULL) {
int ii;
for (ii = 0; ii < wbArg_getInputCount(arg); ii++) {
wbDelete(wbArg_getInputFile(arg, ii));
}
wbDelete(wbArg_getInputFiles(arg));
wbArg_setInputCount(arg, 0);
wbArg_setInputFiles(arg, NULL);
}
if (wbArg_getOutputFile(arg)) {
wbDelete(wbArg_getOutputFile(arg));
wbArg_setOutputFile(arg, NULL);
}
if (wbArg_getExpectedOutputFile(arg)) {
wbDelete(wbArg_getExpectedOutputFile(arg));
wbArg_setExpectedOutputFile(arg, NULL);
}
if (wbArg_getType(arg)) {
wbDelete(wbArg_getType(arg));
wbArg_setType(arg, NULL);
}
return;
}
static int getInputFileCount(char *arg) {
int count = 1;
while (*arg != '\0' && *arg != '-') {
if (*arg == ',') {
count++;
}
arg++;
}
return count;
}
static char **parseInputFiles(char *arg, int *resCount) {
int count;
int ii = 0;
char **files;
char *token;
count = getInputFileCount(arg);
files = wbNewArray(char *, count);
token = strtok(arg, ",");
while (token != NULL) {
files[ii++] = wbString_duplicate(token);
token = strtok(NULL, ",");
}
*resCount = ii;
return files;
}
static char *parseString(char *arg) { return wbString_duplicate(arg); }
EXTERN_C wbArg_t wbArg_read(int argc, char **argv) {
int ii;
wbArg_t arg;
arg = wbArg_new();
for (ii = 0; ii < argc; ii++) {
if (wbString_startsWith(argv[ii], "-i")) {
int fileCount;
char **files;
files = parseInputFiles(argv[ii + 1], &fileCount);
wbArg_setInputCount(arg, fileCount);
wbArg_setInputFiles(arg, files);
} else if (wbString_startsWith(argv[ii], "-o")) {
char *file = parseString(argv[ii + 1]);
wbArg_setOutputFile(arg, file);
} else if (wbString_startsWith(argv[ii], "-e")) {
char *file = parseString(argv[ii + 1]);
wbArg_setExpectedOutputFile(arg, file);
} else if (wbString_startsWith(argv[ii], "-t")) {
char *type = parseString(argv[ii + 1]);
wbArg_setType(arg, type);
} else if (argv[ii][0] == '-') {
wbLog(ERROR, "Unexpected program option ", argv[ii]);
}
}
return arg;
}