-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.h
246 lines (206 loc) · 5.63 KB
/
utils.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
#ifndef _UTILS_H
#define _UTILS_H
/*
Hash of a pair
*/
struct pair_hash {
template <class T1, class T2>
std::size_t operator () (const std::pair<T1,T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
return h1 ^ h2;
}
};
/*
CHECKS
*/
#define CHECK(cond, message) \
if (!(cond)) { \
std::cerr << "\033[1;31m[FATAL ERROR] " << #cond << " : " << message << "\033[0m" << std::endl << "\t\t" << std::endl; \
std::abort(); \
}
/*
Strings
*/
#include <sys/stat.h>
#include <errno.h>
#include <string>
bool pathIsDir(const std::string& filePath) {
struct stat st_buf;
stat(filePath.c_str(), &st_buf);
return S_ISDIR(st_buf.st_mode);
}
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
template<typename Out>
void split(const std::string &s, char delim, Out result) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
#ifndef __MINGW32__
/*
Vector Glob
*/
#include <glob.h>
#include <vector>
// From http://dannys-tech-musings.blogspot.com/2008/06/glob-example.html
std::vector<std::string> vGlob(const std::string& pattern) {
std::vector<std::string> fileList;
//Declare glob_t for storing the results of globbing
glob_t globbuf;
//Glob.. GLOB_TILDE tells the globber to expand "~" in the pattern to the home directory
glob(pattern.c_str(), GLOB_TILDE, NULL, &globbuf);
for (size_t i = 0; i < globbuf.gl_pathc; ++i) {
fileList.push_back(globbuf.gl_pathv[i]);
}
//Free the globbuf structure
if (globbuf.gl_pathc > 0)
globfree( &globbuf );
return fileList;
}
#endif
/*
FLAGS
*/
std::string FLAGhelpstring = "";
#define DEFINE_FLAG(type, flag_name, default_value, description) \
std::string FLAGname_##flag_name = #flag_name; \
std::string FLAGhelp_##flag_name = description; \
std::string FLAGdefaultvalue_##flag_name = #default_value; \
type FLAG_##flag_name = default_value;
#define DEFINE_bool(flag_name, default_value, description) \
DEFINE_FLAG(bool, flag_name, default_value, description)
#define DEFINE_string(flag_name, default_value, description) \
DEFINE_FLAG(std::string, flag_name, default_value, description)
#define DEFINE_double(flag_name, default_value, description) \
DEFINE_FLAG(double, flag_name, default_value, description)
#define DEFINE_int(flag_name, default_value, description) \
DEFINE_FLAG(int, flag_name, default_value, description)
#define DEFINE_vec_double(flag_name, default_value, description) \
DEFINE_FLAG(std::vector<double>, flag_name, default_value, description)
#define REGISTER_FLAG(argc, argv, flag_name) \
FLAGhelpstring.append("\n--" #flag_name " ["); \
FLAGhelpstring.append(FLAGdefaultvalue_##flag_name); \
FLAGhelpstring.append("]:\n\t"); \
FLAGhelpstring.append(FLAGhelp_##flag_name); \
parseFlag(argc, argv, FLAGname_##flag_name, FLAG_##flag_name);
#define FLAGHELP() \
std::cout << FLAGhelpstring << std::endl;
#include <cstdlib> // std::atof
void parseFlag(int argc, char** argv, std::string name, bool& val) {
std::string token;
bool found = false;
for (int i = 0; i < argc; ++i) {
token = std::string(argv[i]);
if (found) {
if(token.find("--") == 0) {
val = true;
return;
}
val = (token.compare("true") == 0);
return;
}
if (token.compare("--" + name) == 0) {
if(i == argc - 1) {
// If this is the last flag in the list
val = true;
return;
}
found = true;
}
if(token.compare("--no" + name) == 0) {
val = false;
return;
}
}
}
void parseFlag(int argc, char** argv, std::string name, double& val) {
std::string token;
bool found = false;
for (int i = 0; i < argc; ++i) {
token = std::string(argv[i]);
if (found) {
val = std::atof(argv[i]);
return;
}
if (token.compare("--" + name) == 0) {
found = true;
}
}
}
void parseFlag(int argc, char** argv, std::string name, int& val) {
std::string token;
bool found = false;
for (int i = 0; i < argc; ++i) {
token = std::string(argv[i]);
if (found) {
val = std::atoi(argv[i]);
return;
}
if (token.compare("--" + name) == 0) {
found = true;
}
}
}
void parseFlag(int argc, char** argv, std::string name, std::string& val) {
std::string token;
bool found = false;
for (int i = 0; i < argc; ++i) {
token = std::string(argv[i]);
if (found) {
val = token;
return;
}
if (token.compare("--" + name) == 0) {
found = true;
}
}
}
#include <cstring> // std::strtok
void parseFlag(int argc, char** argv, std::string name, std::vector<double>& val) {
std::string token;
bool found = false;
for (int i = 0; i < argc; ++i) {
token = std::string(argv[i]);
if (found) {
char* tk = std::strtok(argv[i], ":");
while (tk != NULL) {
val.push_back(std::atof(tk));
tk = strtok (NULL, ":");
}
return;
}
if (token.compare("--" + name) == 0) {
found = true;
}
}
}
void parseFlag(int argc, char** argv, std::string name, std::vector<std::string>& val) {
std::string token;
bool found = false;
for (int i = 0; i < argc; ++i) {
token = std::string(argv[i]);
if (found) {
char* tk = std::strtok(argv[i], ":");
while (tk != NULL) {
val.push_back(std::string(tk));
}
return;
}
if (token.compare("--" + name) == 0) {
found = true;
}
}
}
#endif // _UTILS_H