-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.h
350 lines (284 loc) · 8.39 KB
/
tools.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
//==============================================================================
// PROJECT: zqloader
// FILE: tools.h
// DESCRIPTION: Random stuff, including Random
//
// Copyright (c) 2023 Daan Scherft [Oxidaan]
// This project uses the MIT license. See LICENSE.txt for details.
//==============================================================================
#pragma once
#include <string>
#include <iomanip>
#include <algorithm> // transform
#include <sstream>
#include <optional>
#include <vector>
#include <random>
template <class TString>
std::string ToLower(TString p_string)
{
std::string s = std::move(p_string);
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) { return char(std::tolower(c)); });
return s;
}
template <class TString>
std::string ToUpper(TString p_string)
{
std::string s = std::move(p_string);
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) { return char(std::toupper(c)); });
return s;
}
class CommandLine
{
public:
/// Initialize with argc and argv taken from main.
CommandLine(int argc, char** argv) noexcept
{
std::string s;
for (int n = 0; n < argc; n++)
{
m_args.push_back(std::string(argv[n]));
}
}
/// Has it commandline parameters? Convenience.
bool HasParameters() const noexcept
{
return m_args.size() > 1;
}
/// Get # parameters
unsigned GetNumParameters() const noexcept
{
return unsigned(m_args.size() - 1);
}
/// Get last parameter
std::string GetLastParameter() const noexcept
{
if (HasParameters())
{
return m_args.back();
}
return "";
}
/// Get nth parameter
std::string GetParameter(int p_num) const noexcept
{
return m_args[p_num];
}
/// Get value of given named commandine parameter overload for const char *.
/// Return given default when not found.
std::string GetParameter(std::string_view p_param, const char* p_default) noexcept
{
auto opt = TryGetParameter(p_param);
if (opt.has_value())
{
return opt.value();
}
return p_default;
}
/// Get value of given named commandine parameter as TData.
/// Return given default when not found.
template <class TData>
TData GetParameter(std::string_view p_param, TData p_default) noexcept
{
auto opt = TryGetParameter<TData>(p_param);
if (opt.has_value())
{
return opt.value();
}
return p_default;
}
/// Get value of given named commandine parameter.
/// Return nullopt when not found.
std::optional<std::string> TryGetParameter(std::string_view p_command) const noexcept;
template <class TData>
std::optional<TData> TryGetParameter(std::string_view p_command) const noexcept
{
auto opt = TryGetParameter(p_command);
if (opt.has_value())
{
std::stringstream ss(opt.value());
TData d;
ss >> d;
return d;
}
return std::nullopt;
}
/// Convenience
bool HasParameter(std::string_view p_param) const noexcept
{
return TryGetParameter(p_param).has_value();
}
private:
std::vector<std::string> m_args;
};
namespace iomanip
{
class skip
{
public:
skip(std::string_view p_to_skip) :
m_to_skip(p_to_skip)
{}
void Skip(std::istream& p_stream) const
{
if (p_stream.good())
{
do
{
char c = char(p_stream.peek());
if (m_to_skip.find(c) == std::string::npos)
{
break;
}
p_stream.ignore(1);
} while (p_stream.good());
p_stream.clear(p_stream.rdstate() & ~std::ios::failbit);
}
}
private:
std::string_view m_to_skip;
};
/// Skips stream reading as long as any of given characters encountered.
/// eg:
/// istream >> iomanip::skip("\n\r,; ");
inline std::istream& operator >>(std::istream& p_stream, const iomanip::skip& p_ignore)
{
p_ignore.Skip(p_stream);
return p_stream;
}
class readuntil
{
public:
readuntil(std::string& p_string, std::string_view p_read_until) :
m_string(p_string),
m_read_until(p_read_until)
{}
void Read(std::istream& p_stream) const
{
if (p_stream.good())
{
m_string.clear();
do
{
char c = char(p_stream.peek());
if (m_read_until.find(c) != std::string::npos)
{
break;
}
p_stream.ignore(1);
if (!p_stream.fail())
{
m_string += c;
}
} while (p_stream.good());
p_stream.clear(p_stream.rdstate() & ~std::ios::failbit);
}
}
private:
std::string& m_string;
std::string_view m_read_until;
};
/// Reads into a target string intil one of given characters encountered.
/// eg:
/// istream >> iomanip::readuntil(target_string, "\n\r,; ");
inline std::istream& operator >>(std::istream& p_stream, const iomanip::readuntil& p_read_until)
{
p_read_until.Read(p_stream);
return p_stream;
}
class quoted
{
public:
quoted(std::string& p_str, char p_escape = '\\') :
m_read(p_str), m_escape(p_escape)
{}
void Read(std::istream& p_stream) const
{
p_stream >> skip(" \n\r\t");
char c = char(p_stream.peek());
if (c == '"' || c == '\'')
{
p_stream >> std::quoted(m_read, c, m_escape);
}
else
{
using namespace std::string_literals;
p_stream >> readuntil(m_read, " \n\r\t\0"s);
}
}
private:
std::string& m_read;
char m_escape;
};
/// As std::quoted but delim is autmatically determined eg "'" or '"', can be nothing as well.
/// eg:
/// istream >> iomanip::skip("\n\r,; ");
inline std::istream& operator >>(std::istream& p_stream, const quoted& p_quoted)
{
p_quoted.Read(p_stream);
return p_stream;
}
} // iomanip
template<class TEngine = std::minstd_rand>
auto& GetSeededRandomEngine()
{
static std::random_device seed;
static TEngine gen(seed());
return gen;
}
// Eg Dice: Random(1,6)
template<class TInt1, class TInt2,
typename std::enable_if<std::is_integral<TInt1>::value, int>::type = 0,
typename std::enable_if<std::is_integral<TInt2>::value, int>::type = 0>
TInt1 Random(TInt1 p_min, TInt2 p_max)
{
std::uniform_int_distribution distrib(p_min, TInt1(p_max));
return distrib(GetSeededRandomEngine());
}
/// Get value of given named commandine parameter.
/// Return nullopt when not found.
inline std::optional<std::string> CommandLine::TryGetParameter(std::string_view p_command) const noexcept
{
using namespace std::string_literals;
for (int n = 1; n < m_args.size(); n++)
{
std::string arg = m_args[n];
if ((arg == p_command || arg == ("--" + std::string(p_command))) && n < (m_args.size() - 1) )
{
std::string value = m_args[n+1];
if((value == "=" || value == ":") && n < (m_args.size() - 2))
{
// work around command = value / command : value
// note command= value and command =value still not working
value = m_args[n + 2];
}
return value;
}
else if (p_command.length() == 1 && arg.length() > 1 && arg[0] == '-' && arg[1] != '-')
{
// Commandline given single - followed by letters eg -abcd = flags: mere presence of letter indicates 'true'
if (arg.find(p_command) != std::string::npos)
{
return "1";
}
}
else
{
// parse it a bit eg cmd=value
std::stringstream ss(arg);
std::string s;
ss >> iomanip::skip(" \n\r\t\0=:\""s);
ss >> iomanip::readuntil(s, " \n\r\t\0=:\""s);
if (s == p_command ||
s == ("--" + std::string(p_command)) )
{
std::string value;
ss >> iomanip::skip(" \n\r\t\0=:"s) >> iomanip::readuntil(value, ""); // read rest
return value;
}
}
}
return std::nullopt;
}