-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.cpp
184 lines (159 loc) · 5.44 KB
/
utils.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <sys/stat.h>
#include "utils.hpp"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <conio.h>
#include <direct.h>
#endif
#ifdef _WIN32
// Check if process is running on a terminal by checking the console processes
inline bool isRunningOnTerminal()
{
DWORD buffer[1];
return GetConsoleProcessList(buffer, 1) > 1;
}
#endif
// Display the 'press any key to exit' if process is not running in a terminal (Windows only)
void pressAnyKey()
{
#ifdef _WIN32
if (isRunningOnTerminal())
return;
std::cout << "\nPress any key to exit..." << std::endl;
_getch();
#endif
}
// Display error and exit
void throwError(const std::string &error)
{
std::cout.flush();
std::cerr << "\nERROR: " << error << std::endl;
pressAnyKey();
exit(1);
}
// Format path by removing whitespace and double quotes
std::string formatPath(std::string path)
{
// Trim whitespace and quotes in string
path.erase(path.find_last_not_of(" \t\n\r\f\v\"") + 1);
path.erase(0, path.find_first_not_of(" \t\n\r\f\v\""));
return path;
}
// Split the given string using the given delimiter
std::vector<std::string> splitString(std::string stringToSplit, const char delimiter)
{
std::vector<std::string> resultVector;
size_t pos;
std::string part;
while ((pos = stringToSplit.find(delimiter)) != std::string::npos) {
part = stringToSplit.substr(0, pos);
resultVector.push_back(part);
stringToSplit.erase(0, pos + 1);
}
resultVector.push_back(stringToSplit);
return resultVector;
}
// Recursive mkdir
#ifdef _WIN32
int mkpath(const fs::path &filePath, size_t startPos)
{
auto *path = _wcsdup(filePath.c_str());
const char separator = fs::path::preferred_separator;
for (wchar_t *p = wcschr(path + startPos, separator); p != nullptr; p = wcschr(p + 1, separator)) {
*p = '\0';
if (_wmkdir(path) == -1 && errno != EEXIST) {
*p = separator;
return -1;
}
*p = separator;
}
free(path);
return 0;
}
#else
int mkpath(const fs::path &filePath, size_t startPos)
{
auto *path = strdup(filePath.c_str());
const char separator = fs::path::preferred_separator;
for (char *p = strchr(path + startPos, separator); p != nullptr; p = strchr(p + 1, separator)) {
*p = '\0';
if (mkdir(path, 0777) == -1) {
if (errno == EEXIST) {
if (fs::is_regular_file(path)) {
fs::path newPath(path);
newPath += " (1)";
fs::rename(path, newPath);
if (mkdir(path, 0777) == -1) {
*p = separator;
return -1;
}
}
}
else {
*p = separator;
return -1;
}
}
*p = separator;
}
free(path);
return 0;
}
#endif
// Populate regex resources from parameters
void compileRegexes(std::vector<std::regex> ®exesToMatch, std::vector<std::regex> ®exesNotToMatch, const std::vector<std::pair<std::string, std::string>> ¶ms)
{
const std::string charsToEscape = "\\^$*+?.()|{}[]";
for (const auto& param : params) {
if (param.first == "r" || param.first == "regex") {
for (const auto& regex : splitString(param.second, ';')) {
// Push regex to vector
try {
if (regex[0] == '!')
regexesNotToMatch.emplace_back(regex.substr(1), std::regex_constants::ECMAScript | std::regex_constants::optimize);
else
regexesToMatch.emplace_back(regex, std::regex_constants::ECMAScript | std::regex_constants::optimize);
}
catch (const std::exception& e) {
throwError("Failed to parse " + regex + " regular expression: " + e.what());
}
}
}
else if (param.first == "f" || param.first == "filter") {
for (const auto& filter : splitString(param.second, ';')) {
// Convert filter into valid regex
std::string regex;
for (const auto& c : filter) {
switch (c) {
case '?':
regex.push_back('.');
break;
case '*':
regex += ".*";
break;
default:
if (charsToEscape.find(c) != std::string::npos)
regex.push_back('\\'); // Escape character with backslash
regex.push_back(c);
break;
}
}
// Push regex to vector
try {
if (regex[0] == '!')
regexesNotToMatch.emplace_back(regex.substr(1), std::regex_constants::ECMAScript | std::regex_constants::optimize);
else
regexesToMatch.emplace_back(regex, std::regex_constants::ECMAScript | std::regex_constants::optimize);
}
catch (const std::exception &e) {
throwError("Failed to parse " + filter + " filter: " + e.what());
}
}
}
}
}