-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
351 lines (298 loc) · 7.91 KB
/
main.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
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
#include <regex>
#include <string>
#include <iostream>
#include <filesystem>
#include <string_view>
#include <gsl/gsl>
int main(int argc, const char* argv[]);
static void output_help_str(std::ostream& output, std::string_view exe_name) {
output
<< exe_name << " <directory_path> <match_pattern> <new_name> [flags]"
<< R"HELPSTR(
Where:
<directory_path> is the directory in which the
search for matching files with take place;
<match_pattern> is the regex search pattern
that matches the files that are to be renamed;
<new_name> is the pattern that the filenames
will be replaced into.
The program will terminate if there is already
a file with the same name.
Flags (can be specified separately or in groups):
-i: ignore cases.
-d: match directories and exclude files.
-p: preview changes; none will actually be made.
The syntax of the regex is roughly the same as the
one used by ECMAScript.
Arguments must be passed in the specified order
because the developer is lazy.
Example:
)HELPSTR" << exe_name << R"HELPSTR( . 123(.+)321(\.?.*) "$$file_$1$2" -pi -d
Which will rename "123abc321.a" to "$file_abc.a"
and "1233321" to "$file_3".
)HELPSTR";
}
struct Options {
std::regex match_pattern;
std::filesystem::path directory;
std::string new_name;
bool // Flag...
#ifdef _DEBUG
is_test = false,
#endif // _DEBUG
is_directory_only = false,
is_preview = false;
bool test_entry(const std::filesystem::directory_entry& entry) {
// Mutually excusive test pairs
const std::pair<bool, bool> tests[] = {
{ is_directory_only, entry.is_directory() },
{ !is_directory_only, entry.is_regular_file() },
};
for (auto& test_pair : tests) {
if (test_pair.first && test_pair.second)
return true;
}
return false;
}
};
class quoted_string {
std::string_view str;
public:
constexpr quoted_string(std::string_view s) noexcept : str(std::move(s)) {}
friend std::ostream& operator<<(std::ostream& os, const quoted_string& self) {
return os << '"' << self.str << '"';
}
};
static void print_help(std::string_view self_name) {
const auto exe_name_pos = self_name.find_last_of("/\\") + 1;
const std::string_view exe_name =
self_name.substr(exe_name_pos, self_name.length() - exe_name_pos);
output_help_str(std::cout, exe_name);
}
int parse_args(const gsl::span<const char*>& args, Options& options)
{
using namespace std::string_literals;
using namespace std::string_view_literals;
namespace fs = std::filesystem;
using std::cerr, std::endl;
static constexpr const std::string_view help_args[] = {
"--help", "-help", "-h", "-?", "/?", "/help"
};
static const auto print_arg_err = [](const std::string& message) {
std::cerr
<< message << endl
<< "Use --help for usage information." << endl;
};
if (args.size() <= 1 ||
std::find(
std::begin(help_args), std::end(help_args), args[1]
) != std::end(help_args)) {
print_help(
(args.empty() || std::strlen(args[0]) == 0) ? "regren" : args[0]
);
return 1;
}
#ifdef _DEBUG
else if (args[1] == "--test"sv) {
options.is_test = true;
return 0;
}
#endif // _DEBUG
else if (args.size() <= 3) {
print_arg_err("Expected 3 arguments.");
return -1;
}
options.directory = fs::path(args[1]);
std::error_code file_err;
if (!fs::is_directory(options.directory, file_err)) {
cerr << "Error: ";
if (file_err) {
cerr << file_err << endl;
}
else {
cerr
<< options.directory
<< " is not a directory!"
<< endl;
}
return file_err.value();
}
file_err.clear();
auto regex_flags = std::regex_constants::ECMAScript
| std::regex_constants::optimize;
for (size_t i = 4; i < args.size(); i++) {
std::string_view arg = args[i];
if (arg.size() <= 1 || arg[0] != '-') {
print_arg_err("Invalid argument \""s + arg[0] + "\": flag(s) expected.");
return -1;
}
for (size_t j = 1; j < arg.size(); j++) {
switch (arg[j])
{
case 'i':
regex_flags |= std::regex_constants::icase;
break;
case 'd':
options.is_directory_only = true;
break;
case 'p':
options.is_preview = true;
break;
default:
print_arg_err("Unknown flag \"-"s + arg[j] + '\"');
return -1;
}
}
}
try {
options.match_pattern = std::regex(args[2], regex_flags);
}
catch (const std::regex_error& e) {
cerr << "RegExp Error(" << e.code() << "): " << e.what() << endl;
return -2;
}
options.new_name = args[3];
return 0;
}
#ifdef _DEBUG
int test() {
using test_t = bool (*)();
using args_t = gsl::span<const char*>;
using namespace std::string_literals;
using namespace std::string_view_literals;
static constexpr char TEST_ARG0[] = "<test:argv[0]>";
constexpr test_t tests[]{
[]() {
Options o;
const char new_name[] = "$$$1$$";
const char* args[] = { TEST_ARG0, ".", "^(.*)\\.@name", new_name };
int r = parse_args(args, o);
const auto result = std::regex_replace(
"f_I+l%e.@name!", o.match_pattern, o.new_name
);
const char expected[] = "$f_I+l%e$!";
return r == 0
&& o.directory == "."
&& o.new_name == new_name
&& result == expected;
},
[]() {
Options o;
const char* args[] = { TEST_ARG0, ".", "#", "@", "-dpi" };
int r = parse_args(args, o);
return r == 0
&& o.is_directory_only
&& o.is_preview
&& o.match_pattern.flags() & std::regex_constants::icase;
},
[]() {
const char* args[] = {
TEST_ARG0, ".", "([^.]+)(\\.?.*)", "$$$1_renamed$2", "-pi"
};
std::cout << std::endl;
return main(static_cast<int>(std::size(args)), args) == 0;
},
};
int failed_test_count = 0;
for (size_t i = 0; i < std::size(tests); i++) {
std::cout << "Test #" << i << ": ";
if (!tests[i]()) {
failed_test_count++;
std::cout << "Failed.";
}
else {
std::cout << "Passed.";
}
std::cout << std::endl;
}
std::cout
<< "Tests: "
<< failed_test_count << '/' << std::size(tests)
<< " failed." << std::endl;
return failed_test_count;
}
#endif // _DEBUG (test content)
int main(int argc, const char* argv[])
{
namespace fs = std::filesystem;
constexpr char TAB_CHARS[] = " ";
Options options{};
const auto args = gsl::span(argv, argc);
if (int err = parse_args(args, options))
return err;
#ifdef _DEBUG
if (options.is_test) {
return test();
}
#endif // _DEBUG
std::error_code err;
size_t renamed_count = 0, total_count = 0;
for (const auto& entry : fs::directory_iterator(options.directory)) {
using std::cout, std::cerr, std::endl;
if (!options.test_entry(entry)) {
continue;
}
const std::string old_name = entry.path().filename().string();
std::string new_name;
try {
if (!std::regex_search(old_name, options.match_pattern))
continue; // skip; does not match
new_name = std::regex_replace(
old_name, options.match_pattern, options.new_name
);
}
catch (const std::regex_error& e) {
std::cerr
<< "RegExp error: "
<< e.what()
<< endl;
return e.code();
}
const fs::path new_path = options.directory.append(new_name);
if (fs::exists(new_path, err) || err) {
if (err) {
cerr
<< "Unable to check whether " << new_path << " exists: "
<< endl
<< TAB_CHARS << err
<< endl;
return err.value();
}
cout
<< "File "
<< quoted_string(new_name)
<< " already exists; skipping..."
<< endl;
continue;
}
err.clear();
if (!options.is_preview) {
fs::rename(entry.path(), new_path, err);
}
if (err) {
cerr
<< "Error: Unable to rename file "
<< quoted_string(old_name)
<< " to "
<< quoted_string(new_name) << ':'
<< endl
<< TAB_CHARS << err
<< endl;
}
else {
cout
<< quoted_string(old_name)
<< " --> "
<< quoted_string(new_name)
<< endl;
renamed_count++;
}
total_count++;
}
std::cout << std::endl;
std::cout << renamed_count << '/' << total_count << " files ";
if (options.is_preview)
std::cout << "will be ";
std::cout << "renamed. " << std::endl;
return static_cast<int>(total_count - renamed_count);
}