-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopt.h
149 lines (139 loc) · 3.87 KB
/
opt.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
#ifndef OPT_H_INCLUDED
#define OPT_H_INCLUDED
#include <string.h>
typedef unsigned long long ulonglong;
typedef long long longlong;
class Options
{
const char *m_progname;
uint m_used_options;
uint m_verbose;
bool m_error;
bool m_vm;
bool m_int32api;
uint m_big_test;
ulonglong m_count;
void shift(int *ac, char ***av)
{
(*ac)--;
(*av)++;
}
static bool get_ulonglong(ulonglong *to,
const char *name,
const char *value,
char **endptr)
{
long long val= strtoll(value, endptr, 10);
if (val < 0)
{
printf("Bad %s value\n", name);
return true;
}
*to= (ulonglong) val;
return false;
}
static bool get_ulonglong(ulonglong *to, const char *name, const char *value)
{
char *endptr;
return get_ulonglong(to, name, value, &endptr);
}
static bool get_ulonglong_size(ulonglong *to, const char *name, const char *value)
{
char *endptr;
if (get_ulonglong(to, name, value, &endptr))
return true;
switch (endptr[0]) {
case 'k':
case 'K':
*to*= 1024;
break;
case 'm':
case 'M':
*to*= 1024*1024;
default:
break;
}
return false;
}
bool get_ulonglong(ulonglong *var,
const char *av,
const char *name,
size_t length)
{
if (strncmp(av, name, length) || av[length] != '=')
return true;
m_error|= get_ulonglong(var, name, av + length + 1);
return false;
}
public:
Options(int ac, char **av)
:m_progname(av[0]),
m_used_options(0),
m_verbose(0),
m_error(0),
m_vm(0),
m_big_test(0),
m_int32api(false),
m_count(10*1000*1000ULL)
{
shift(&ac, &av);
m_used_options++;
for ( ; ac > 0; shift(&ac, &av))
{
if (!get_one_option(av[0]))
m_used_options++;
else
break;
}
}
bool get_one_option(const char *av)
{
if (!strcmp(av, "-?")) m_error= true;
else if (!strncmp(av, "--verbose=", 10)) m_verbose= atoi(av+10);
else if (!strcmp(av, "--verbose")) m_verbose= 1;
else if (!strcmp(av, "--vm")) m_vm= true;
else if (!strcmp(av, "--int32")) m_int32api= true;
else if (!strncmp(av, "--int32=", 8)) m_int32api= atoi(av+8);
else if (!strncmp(av, "--big-test=", 11)) m_big_test= atoi(av+11);
else if (!strcmp(av, "--big-test")) m_big_test++;
else if (!strncmp(av, "--help", 6)) m_error= true;
else if (!strncmp(av, "--count=", 8))
{
if (get_ulonglong_size(&m_count, "--count", av + 8))
m_error= true;
}
else
{
if (!strncmp(av, "--", 2))
m_error= true; // Unknown option
return true; // Not an option - normal arguments started
}
return false;
}
uint used_options() const { return m_used_options; }
bool error() const { return m_error; }
bool vm() const { return m_vm; }
bool int32api() const { return m_int32api; }
uint verbose() const { return m_verbose; }
uint big_test() const { return m_big_test; }
ulonglong count() const { return m_count; }
void set_int32api(bool val) { m_int32api= val; }
void usage() const
{
printf("\n");
printf("Usage:\n%s [option...] [test ...]\n", m_progname);
printf("\n");
printf("Options:\n");
printf(" --count=val - run val iterations\n");
printf(" --big-test[=val] - big tests:\n");
printf(" 0 - disabled\n");
printf(" 1 - enabled\n");
printf(" 2 - only big tests\n");
printf(" --vm - use VM when possible (experimental)\n");
printf(" --int32[=val] - test INT32 API\n");
printf(" 0 - disabled (default)\n");
printf(" 1 - enabled\n");
printf("\n");
}
};
#endif