This repository was archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
521 lines (474 loc) · 12.3 KB
/
Copy pathutils.cpp
File metadata and controls
521 lines (474 loc) · 12.3 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
* @file utils.cpp
* @brief The implementation for the utilities.
*
* @author Evan Elias Young
* @date 2019-03-12
* @date 2020-02-29
* @copyright Copyright 2019-2020 Evan Elias Young. All rights reserved.
*/
#include "pch.h"
#include "argh.h"
#include "utils.h"
/**
* @brief Splits a string into a vector of its parts
*
* @param s The string to slit
* @param d The delimiter to split upon
* @param v The vector to add to
*/
void splitStringVector(const std::string &s, const std::string &d, std::vector<std::string> *v)
{
std::string::size_type beg = std::string::size_type(0);
std::string::size_type end = std::string::size_type(s.find(d, 1));
if (s.find_last_of(d) != std::string::npos)
{
while (beg < s.size() && end <= s.size())
{
(*v).push_back(s.substr(beg, end - beg));
beg = s.find(d, end) + d.length();
end = s.find(d, beg);
}
}
(*v).push_back(s.substr(beg, s.length() - beg));
}
/**
* @brief Removes any duplicates from the specified vector
*
* @param v The vector to make unique
*/
void removeDuplicates(std::vector<std::string> *v)
{
if ((*v).size() <= 1)
{
return;
}
for (int i = (*v).size() - 1; i > 0; --i)
{
if (i != std::distance((*v).begin(), std::find((*v).begin(), (*v).end(), (*v)[i])))
{
(*v).erase((*v).begin() + i);
}
}
}
/**
* @brief Determines whether or not a vector contains the specified element
*
* @param v The vector to search
* @param s The string to stearch for
* @return true The vector contains the string
* @return false The vector does NOT contain the string
*/
bool contains(std::vector<std::string> *v, const std::string &s)
{
return std::find(v->begin(), v->end(), s) != v->end();
}
/**
* @brief Trims away the specified characters from the end of a string
*
* @param s A pointer to the string to trim
* @param t A collection of chars to remove
*/
void rtrim(std::string *s, const char *t)
{
s->erase(s->find_last_not_of(t) + 1);
}
/**
* @brief Trims away the specified characters from the beginning of a string
*
* @param s A pointer to the string to trim
* @param t A collection of chars to remove
*/
void ltrim(std::string *s, const char *t)
{
s->erase(0, s->find_first_not_of(t));
}
/**
* @brief Trims away the specified characters from both ends of a string
*
* @param s A pointer to the string to trim
* @param t A collection of chars to remove
*/
void trim(std::string *s, const char *t)
{
rtrim(s, t);
ltrim(s, t);
}
/**
* @brief Trims away the specified characters from the end of a string
*
* @param s A pointer to the string to trim
* @param t A collection of chars to remove
*/
std::string rtrim(std::string s, const char *t)
{
return s.erase(s.find_last_not_of(t) + 1);
}
/**
* @brief Trims away the specified characters from the beginning of a string
*
* @param s A pointer to the string to trim
* @param t A collection of chars to remove
*/
std::string ltrim(std::string s, const char *t)
{
return s.erase(0, s.find_first_not_of(t));
}
/**
* @brief Trims away the specified characters from both ends of a string
*
* @param s A pointer to the string to trim
* @param t A collection of chars to remove
*/
std::string trim(std::string s, const char *t)
{
return ltrim(rtrim(s, t), t);
}
/**
* @brief Determines whether or not a string s starts with string r
*
* @param s The string to analyze
* @param r The string to search for
* @return true The string starts with the other string
* @return false The string does NOT start with the other string
*/
bool startswith(const std::string &s, const std::string &r)
{
if (r.size() > s.size())
{
return false;
}
if (s.substr(0, r.size()) == r)
{
return true;
}
return false;
}
/**
* @brief Determines whether or not a string s ends with string r
*
* @param s The string to analyze
* @param r The string to search for
* @return true The string ends with the other string
* @return false The string does NOT end with the other string
*/
bool endswith(const std::string &s, const std::string &r)
{
if (r.size() > s.size())
{
return false;
}
if (s.substr(s.size() - r.size()) == r)
{
return true;
}
return false;
}
/**
* @brief Splits a key/value pair base on a delimiter
*
* @param base The base string to derive the key and value from
* @param k The key
* @param v The value
* @param doTrim Whether or not to trim the output
* @param spl The character to split upon
* @return true The split was successful
* @return false The split was NOT successful
*/
bool splitKeyValuePair(const std::string &base, std::string *k, std::string *v, const bool &doTrim, const char &spl)
{
if (base.find_first_of(spl) == std::string::npos)
{
return false;
}
(*k) = base.substr(0, base.find_first_of(spl));
(*v) = base.substr(base.find_first_of(spl) + 1);
if (doTrim)
{
trim(k);
trim(v);
}
return true;
}
/**
* @brief Attempts to read a file and get its contents
*
* @param p The path to the file to read
* @param o The output of the file, if read
* @return true The file was read successfully
* @return false The file was NOT read successfully
*/
bool readFile(const std::string &p, std::string *o)
{
std::ifstream t(p);
std::stringstream buffer;
if (!t.good())
{
return false;
}
try
{
buffer << t.rdbuf();
(*o) = buffer.str();
}
catch (...)
{
return false;
}
return true;
}
/**
* @brief Prefixes a number with the SI units
*
* @param num The number to prefix
* @param suff The appendage
* @param plc The number of decimal places
* @return std::string The formatted number
*/
std::string siUnits(const std::uint64_t &num, const std::string &suff, const std::uint8_t &plc)
{
std::stringstream ss;
char suf[9] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
for (std::size_t i = 5; i > 0; --i)
{
if (num >= pow(1000, i))
{
ss << std::fixed << std::setprecision(plc) << num / pow(1000, i) << ' ' << suf[i] << suff;
return ss.str();
}
}
ss << std::fixed << std::setprecision(plc) << num << ' ' << suff;
return ss.str();
}
/**
* @brief Prefixes a number with the SI units
*
* @param num The number to prefix
* @param suff The appendage
* @param plc The number of decimal places
* @return std::string The formatted number
*/
std::string siUnits(const std::uint32_t &num, const std::string &suff, const std::uint8_t &plc)
{
std::stringstream ss;
char suf[9] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
for (std::size_t i = 5; i > 0; --i)
{
if (num >= pow(1000, i))
{
ss << std::fixed << std::setprecision(plc) << num / pow(1000, i) << ' ' << suf[i] << suff;
return ss.str();
}
}
ss << std::fixed << std::setprecision(plc) << num << ' ' << suff;
return ss.str();
}
/**
* @brief Prefixes a number with the SI units
*
* @param num The number to prefix
* @param suff The appendage
* @param plc The number of decimal places
* @return std::string The formatted number
*/
std::string siUnits(const float &num, const std::string &suff, const std::uint8_t &plc)
{
std::stringstream ss;
char suf[9] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
for (std::size_t i = 5; i > 0; --i)
{
if (num >= pow(1000, i))
{
ss << std::fixed << std::setprecision(plc) << num / pow(1000, i) << ' ' << suf[i] << suff;
return ss.str();
}
}
ss << std::fixed << std::setprecision(plc) << num << ' ' << suff;
return ss.str();
}
/**
* @brief Outputs the version of CGoggles
*/
void outputVersion()
{
std::cout << ((CGOGGLES_VERSION & 0xFF0000) >> (4 * 4)) << '.'
<< ((CGOGGLES_VERSION & 0x00FF00) >> (2 * 4)) << '.'
<< ((CGOGGLES_VERSION & 0x0000FF) >> (0 * 4)) << ' '
<< 'x' << CGOGGLES_ENVIRONMENT << std::endl;
}
/**
* @brief Outputs the help for CGoggles
*/
void outputHelp()
{
std::cout << "usage: cgoggles [-v|--ver|--version] [-h|--help] [-l|--list|--value] [-r|--raw] <command> [<args>]" << '\n'
<< " get Makes a query to the computer's internals" << '\n'
<< " list List the values that you can query to CGoggles" << '\n'
<< '\n'
<< "example: cgoggles get cpu.Brand, cpu.Cores, os.Version" << std::endl;
}
/**
* @brief Outputs the list of accessible data (or from a category)
*
* @param cat The category to output
*/
void outputList(const std::string &cat)
{
std::vector<std::string> osList = {
"os", "os.All", "os.Platform",
"os.Caption", "os.Serial", "os.Bit",
"os.InstallTime", "os.BootTime", "os.CurTime",
"os.Kernel", "os.Version"};
std::vector<std::string> sysList = {
"sys", "sys.All", "sys.Manufacturer",
"sys.Model", "sys.Version", "sys.Serial",
"sys.UUID"};
std::vector<std::string> cpuList = {
"cpu", "cpu.All", "cpu.Manufacturer",
"cpu.Architecture", "cpu.SocketType", "cpu.Brand",
"cpu.Family", "cpu.Model", "cpu.Stepping",
"cpu.Cores", "cpu.Threads", "cpu.Speed",
"cpu.MaxSpeed"};
std::vector<std::string> gpuList = {
"gpu", "gpu.All", "gpu.Vendor",
"gpu.Model", "gpu.Bus", "gpu.VRAM",
"gpu.Dynamic"};
std::vector<std::string> ramList = {
"ram", "ram.All", "ram.Size",
"ram.Bank", "ram.Type", "ram.Speed",
"ram.FormFactor", "ram.Manufacturer", "ram.Part",
"ram.Serial", "ram.VoltageConfigured", "ram.VoltageMin",
"ram.VoltageMax"};
std::vector<std::string> storageList = {
"storage", "storage.All", "storage.Name",
"storage.Identifier", "storage.Type", "storage.FileSystem",
"storage.Mount", "storage.Total", "storage.Physical",
"storage.UUID", "storage.Label", "storage.Model",
"storage.Serial", "storage.Removable", "storage.Protocol"};
std::vector<std::string> fsList = {
"fs", "fs.All", "fs.FS",
"fs.Type", "fs.Size", "fs.Used",
"fs.Mount"};
std::vector<std::vector<std::string>> liList = {
osList, sysList, cpuList, gpuList,
ramList, storageList, fsList};
if (cat == "" || cat == "all" || cat == "All")
{
std::cout << "All available categories for CGoggles." << '\n';
for (std::size_t i = 0; i < liList.size(); ++i)
{
std::cout << liList[i][0] << '\n';
}
return;
}
std::cout << "Available queries for the specified category." << '\n';
if (cat == "os")
{
for (std::size_t i = 0; i < osList.size(); ++i)
{
std::cout << osList[i] << '\n';
}
}
if (cat == "cpu")
{
for (std::size_t i = 0; i < cpuList.size(); ++i)
{
std::cout << cpuList[i] << '\n';
}
}
if (cat == "gpu")
{
for (std::size_t i = 0; i < gpuList.size(); ++i)
{
std::cout << gpuList[i] << '\n';
}
}
if (cat == "ram")
{
for (std::size_t i = 0; i < ramList.size(); ++i)
{
std::cout << ramList[i] << '\n';
}
}
if (cat == "storage")
{
for (std::size_t i = 0; i < storageList.size(); ++i)
{
std::cout << storageList[i] << '\n';
}
}
if (cat == "fs")
{
for (std::size_t i = 0; i < fsList.size(); ++i)
{
std::cout << fsList[i] << '\n';
}
}
}
/**
* @brief Handles the input arguments
*
* @param argc The count of arguments
* @param argv The value of arguments
* @param request The request string
* @return int The exit code
*/
int handleArgs(int argc, const char *argv[], std::string *request)
{
argh::parser cmdl(argv);
bool getArgs = false;
if (argc <= 1)
{
outputHelp();
std::exit(EXIT_FAILURE);
}
if (cmdl[{"v", "ver", "version"}])
{
outputVersion();
std::exit(EXIT_SUCCESS);
}
if (cmdl[{"h", "help"}])
{
outputHelp();
std::exit(EXIT_SUCCESS);
}
if (cmdl[{"l", "list"}])
{
style = OutputStyle::List;
}
if (cmdl[{"value"}])
{
style = OutputStyle::Value;
}
if (cmdl[{"r", "raw"}])
{
pretty = false;
}
for (int i = 0; i < argc; ++i)
{
if (!std::strcmp(argv[i], "list"))
{
outputList(i + 1 < argc ? argv[++i] : "");
return EXIT_SUCCESS;
}
if (!getArgs || i == 0 || argv[i][0] == '-')
{
continue;
}
}
for (int i = 0; i < argc; ++i)
{
if (!std::strcmp(argv[i], "get"))
{
getArgs = true;
continue;
}
if (!getArgs || i == 0 || argv[i][0] == '-')
{
continue;
}
(*request) += argv[i];
}
std::transform(request->begin(), request->end(), request->begin(), ::toupper);
return EXIT_SUCCESS;
}