-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.c
210 lines (191 loc) · 5.14 KB
/
cli.c
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
#include "cli.h"
#include "stdbool.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "core/diag.h"
static int arg_index;
static int arg_count;
static const char **args;
static const char *current_arg;
struct cli_options options;
static const char *log_levels[3] = {
[LOGLEVEL_NONE] = "none",
[LOGLEVEL_NORMAL] = "normal",
[LOGLEVEL_VERBOSE] = "verbose",
};
static void
usage(void)
{
printf("Usage: %s <file1> <file2> [<options>]\nfile1 - problem statement, file2 - where to output the solution\n", args[0]);
printf("\n");
printf("Options:\n");
printf(" --beta_correction - Enables beta-correction mechanism.\n");
printf(" --log_level=<option> - Log level: none, normal, verbose.\n");
printf(" --n_near=<value> - Sets the preferred n_near.\n");
printf(" --k_max=<value> - Sets the preferred k_max.\n");
printf(" --t_max=<value> - Sets the preferred t_max (in secs).\n");
printf(" --i_rand=<value> - Sets the preferred i_rand.\n");
printf(" --lower_bound=<value> - Sets the preferred lower_bound.\n");
}
bool
str_eq(const char *str1, const char *str2)
{
return str1 == str2 || (str1 && str2 && strcmp(str1, str2) == 0);
}
int
str_findlist(const char *value, unsigned count, const char** elements)
{
for (unsigned i = 0; i < count; i++)
{
if (strcmp(value, elements[i]) == 0) return (int)i;
}
return -1;
}
static inline bool
at_end(void)
{
return arg_index == arg_count - 1;
}
static inline const char *
next_arg(void)
{
assert(!at_end());
current_arg = args[++arg_index];
return current_arg;
}
bool
match_longopt(const char *name)
{
return str_eq(¤t_arg[2], name);
}
static inline bool
match_shortopt(const char *name)
{
return str_eq(¤t_arg[1], name);
}
static inline const char *
match_argopt(const char *name)
{
size_t len = strlen(name);
if (memcmp(¤t_arg[2], name, len) != 0) return false;
if (current_arg[2 + len] != '=') return false;
return ¤t_arg[2 + len + 1];
}
static int
parse_multi_option(const char *start, unsigned count, const char **elements)
{
const char *arg = current_arg;
int select = str_findlist(start, count, elements);
if (select < 0) panic("error: %.*s invalid option '%s' given.", (int)(start - arg), start, arg);
return select;
}
static void
parse_option(void)
{
const char *argopt;
switch (current_arg[1]) {
//case '?':
// if (match_shortopt("?")) {
// usage();
// exit(0);
// }
// break;
case '-':
if (match_longopt("beta_correction")) {
options.beta_correction = true;
return;
}
if ((argopt = match_argopt("log_level"))) {
options.log_level = (log_level) parse_multi_option(argopt, 3, log_levels);
return;
}
if (match_longopt("n_near"))
{
if (at_end())
panic("error: --n_near needs a valid integer.");
const char *n_near_string = next_arg();
char *p_end;
options.n_near = (int)strtol(n_near_string, &p_end, 10);
if (p_end != n_near_string + strlen(n_near_string))
panic("error: --n_near needs a valid integer.");
return;
}
if (match_longopt("k_max"))
{
if (at_end())
panic("error: --k_max needs a valid integer.");
const char *k_max_string = next_arg();
char *p_end;
options.k_max = (int)strtol(k_max_string, &p_end, 10);
if (p_end != k_max_string + strlen(k_max_string))
panic("error: --k_max needs a valid integer.");
return;
}
if (match_longopt("t_max"))
{
if (at_end()) panic("error: --t_max needs a valid integer.");
const char *t_max_string = next_arg();
char *p_end;
options.t_max = (int)strtol(t_max_string, &p_end, 10);
if (p_end != t_max_string + strlen(t_max_string))
panic("error: --t_max needs a valid integer.");
return;
}
if (match_longopt("i_rand"))
{
if (at_end())
panic("error: --i_rand needs a valid integer.");
const char *i_rand_string = next_arg();
char *p_end;
options.i_rand = (int)strtol(i_rand_string, &p_end, 10);
if (p_end != i_rand_string + strlen(i_rand_string))
panic("error: --k_max needs a valid integer.");
return;
}
if (match_longopt("lower_bound"))
{
if (at_end())
panic("error: --lower_bound needs a valid integer.");
const char *lower_bound_string = next_arg();
char *p_end;
options.lower_bound = (int)strtol(lower_bound_string, &p_end, 10);
if (p_end != lower_bound_string + strlen(lower_bound_string))
panic("error: --k_max needs a valid integer.");
return;
}
default:
break;
}
panic("Cannot process the unknown option \"%s\".", current_arg);
}
void
parse_arguments(int argc, const char *argv[])
{
arg_count = argc;
args = argv;
if (argc < 3)
{
usage();
exit(0);
}
options.problem_file = args[1];
options.solution_file = args[2];
options.beta_correction = false;
options.log_level = LOGLEVEL_VERBOSE;
options.n_near = 100;
options.k_max = 5;
options.t_max = (clock_t)365 * 86400 * 100;
options.i_rand = 100000;
options.lower_bound = 0;
for (arg_index = 3; arg_index < arg_count; arg_index++)
{
current_arg = args[arg_index];
if (current_arg[0] == '-')
{
parse_option();
continue;
}
panic("Found the unexpected argument \"%s\".", current_arg);
}
}