-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPSEngine.cpp
More file actions
491 lines (406 loc) · 14.4 KB
/
MPSEngine.cpp
File metadata and controls
491 lines (406 loc) · 14.4 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
/*
* This file is part of MemphisNow.
*
* MemphisNow is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MemphisNow is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MemphisNow. If not, see <https://www.gnu.org/licenses/>.
*/
#include "MPSEngine.hpp"
#include <sstream>
#include <boost/filesystem.hpp>
#include <boost/functional/hash.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "transforms/MPSActionChangeCase.hpp"
/* Utility Structures */
struct MPSNotify
{
void operator () (MPSEngineObserver* obs)
{
if (obs)
obs->notify();
}
};
/* End Utility Structures */
MPSEngine::MPSEngine()
: m_master_token (0),
m_rename_to (L""),
// selection related
m_selected_subtoken (0),
// settings
m_default_separators (L""),
m_always_lowcase_extension (false)
{
}
MPSEngine::~MPSEngine()
{
}
void MPSEngine::set_default_separators (const std::wstring& separators)
{
m_default_separators = separators;
}
std::wstring MPSEngine::remove_strings_from_text (const std::wstring& text)
{
// result
std::wstring res = text;
// in case you want to repace with something other than empty string
std::wstring to = L"";
for (int idx = 0; idx < m_strings_to_remove.count(); ++idx) {
QString from = m_strings_to_remove.at(idx);
if (from.count() == 0)
return res;
size_t start_pos = 0;
while((start_pos = res.find(from.toStdWString(), start_pos)) != std::string::npos) {
res.replace(start_pos, from.count(), to);
// In case 'to' contains 'from', like replacing 'x' with 'yx'
start_pos += to.length();
}
}
return res;
}
void MPSEngine::select_master_token (const std::wstring& file_name)
{
// an empty file name forces a deselect
if (file_name.empty()) {
m_master_token = 0;
m_selected_subtoken = 0;
m_rename_to.clear();
return;
}
// compute the hash for the file name
boost::hash<std::wstring> hasher;
size_t hash = hasher (file_name);
MPSFilesToRenameMap::const_iterator iter = m_files_map.find (hash);
if (iter != m_files_map.end()) {
// use data from the internal map
m_master_token = iter->second.root;
m_rename_to = iter->second.rename_to;
} else {
// create new entry and add it into the internal map
m_master_token = new MPSToken (NULL, file_name, m_default_separators, false);
// remove list of expressions
std::wstring fn_clean = remove_strings_from_text(file_name);
if (fn_clean != file_name) {
// create one subtoken and split that
m_master_token->insert_subtoken(fn_clean, 0);
// get the first subtoken
MPSToken* sub = *m_master_token->sub_tokens_begin();
if (sub != 0) {
// must split the first subtoken, not the master token
sub->set_separators(m_default_separators);
sub->split();
// apply transforms
if (is_apply_transforms())
apply_transforms (sub);
}
} else {
m_master_token->split();
// apply transforms
if (is_apply_transforms())
apply_transforms (m_master_token);
}
// reconstruct the output
m_rename_to = reconstruct_output (m_master_token);
// add the entry
m_files_map.insert (MPSFilesToRenameMap::value_type (hash, MPSFilesMapStruct (m_master_token, m_rename_to)));
}
m_selected_subtoken = m_master_token;
}
void MPSEngine::select_subtoken (MPSToken* token, bool updateOutput)
{
if (!token)
return;
m_selected_subtoken = token;
if (updateOutput) {
// reconstruct the output
m_rename_to = reconstruct_output (m_master_token);
}
// notify observers that selected token has changed
std::for_each (m_observers.begin(), m_observers.end(), MPSNotify ());
}
void MPSEngine::update_token (MPSToken*& token,
const std::wstring& text,
const std::wstring& separators,
bool discard,
bool force_update)
{
if (!token)
return;
if (token->text() != text || token->separators() != separators || force_update) {
MPSToken* parent = token->parent ();
token->clear_subtokens ();
token->set_parent (parent);
token->set_text (text);
token->set_separators (separators);
token->set_discard (discard);
token->split();
} else if (token->is_discard() != discard || force_update) {
if (token != m_master_token) // disallow discarding on root token - does not make sense
token->set_discard(discard);
}
}
void MPSEngine::update_selected_subtoken (
const std::wstring& text,
const std::wstring& separators,
bool discard,
bool force_update)
{
if (!m_selected_subtoken)
return;
bool is_root = (m_selected_subtoken == m_master_token);
update_token(m_selected_subtoken, text, separators, discard, force_update);
if (is_root) m_master_token = m_selected_subtoken;
// reconstruct rename_to
m_rename_to = reconstruct_output(m_master_token);
// update the new name in the map
boost::hash<std::wstring> hasher;
size_t hash = hasher (m_master_token->text());
MPSFilesToRenameMap::iterator iter = m_files_map.find(hash);
if (iter != m_files_map.end()) {
iter->second.rename_to = m_rename_to;
}
}
void MPSEngine::shift_selected_subtoken (EMPSDirection direction)
{
if (!m_selected_subtoken)
return;
MPSToken* parent = m_selected_subtoken->parent();
if (!parent)
return;
parent->shift_subtoken (m_selected_subtoken, direction);
m_rename_to = reconstruct_output(m_master_token);
}
void MPSEngine::change_case (bool upcase, bool only_first, bool recursive)
{
// find the rename entry in the rename map first
boost::hash<std::wstring> hasher;
size_t hash = hasher (m_master_token->text());
MPSFilesToRenameMap::iterator iter = m_files_map.find (hash);
change_case(m_selected_subtoken, upcase, only_first, recursive);
m_rename_to = reconstruct_output(m_master_token);
if (iter != m_files_map.end())
iter->second.rename_to = m_rename_to;
}
void MPSEngine::insert_text (const std::wstring& text_to_insert, EMPSDirection direction)
{
if (!m_selected_subtoken)
return;
if (m_selected_subtoken == m_master_token)
return;
MPSToken* parent = m_selected_subtoken->parent();
if (!parent)
return;
size_t sel_token_pos = 0;
MPSTokensContainer::iterator iter = m_selected_subtoken->parent ()->sub_tokens_begin ();
for ( ; iter != m_selected_subtoken->parent ()->sub_tokens_end (); ++iter) {
if (*iter == m_selected_subtoken)
break;
++sel_token_pos;
}
if (direction == ELeft)
parent->insert_subtoken (text_to_insert, sel_token_pos);
else
parent->insert_subtoken (text_to_insert, sel_token_pos + 1);
m_rename_to = reconstruct_output (m_master_token);
}
void MPSEngine::change_case (MPSToken* token, bool upcase, bool only_first, bool recursive)
{
MPSActionChangeCase change_case (*this, upcase, !only_first, recursive);
change_case (token);
}
std::wstring MPSEngine::reconstruct_output (MPSToken* token)
{
std::wstring name;
std::wstring separator;
if (token != 0) {
if (token->sub_tokens_empty()) {
if (!token->is_discard()) {
bool is_first = (token == m_master_token->find_first_leaf_subtoken (false));
bool is_last = (token == m_master_token->find_last_leaf_subtoken(false));
separator = (is_first) ? L"" : ((is_last) ? L"." : L" ");
name += separator;
name += token->text();
}
} else {
MPSTokensContainer::const_iterator iter = token->sub_tokens_begin();
for ( ; iter != token->sub_tokens_end(); ) {
name += reconstruct_output(*iter);
++iter;
}
}
}
return name;
}
bool MPSEngine::has_rename_to (const std::wstring& file_name, std::wstring& rename_to)
{
bool has = false;
// compute the hash for the file name
boost::hash<std::wstring> hasher;
size_t hash = hasher (file_name);
// find it
MPSFilesToRenameMap::const_iterator iter = m_files_map.find (hash);
if (iter != m_files_map.end()) {
rename_to = iter->second.rename_to;
has = true;
} else {
rename_to.clear();
has = false;
}
return has;
}
bool MPSEngine::has_files_to_rename () const
{
MPSFilesToRenameMap::const_iterator iter = m_files_map.begin();
for ( ; iter != m_files_map.end(); ++iter) {
if (iter->second.root->text () != iter->second.rename_to)
return true;
}
return false;
}
void MPSEngine::clear_files_map ()
{
MPSFilesToRenameMap::iterator iter = m_files_map.begin();
for ( ; iter != m_files_map.end(); ++iter) {
delete iter->second.root;
iter->second.root = 0;
iter->second.rename_to.clear();
}
m_files_map.clear();
}
bool MPSEngine::rename_one ( const std::wstring& path,
const std::wstring& src_file,
const std::wstring& dst_file,
bool update_map_entry ) const
{
using namespace boost::filesystem;
std::wstring err_msg = L"Failed to rename file \"" + src_file + L"\" because of error ";
// construct source path
std::wstring src = path;
src += L"\\";
src += src_file;
// construct destination path
std::wstring dst = path;
dst += L"\\";
dst += dst_file;
// does src_file file exist?
if (!exists(src)) {
err_msg += L"[Source file does not exist]";
m_messages.push(err_msg);
return false;
}
// is src_file a regular file?
if (!is_regular_file(src)) {
err_msg += L"[Source file is not regular]";
m_messages.push(err_msg);
return false;
}
// if dst_file empty?
if (dst.empty()) {
err_msg += L"[Destination file name is empty]";
m_messages.push(err_msg);
return false;
}
// does src_file equal dst_file?
if (src == dst) {
err_msg += L"[Source file is same as destination file]";
m_messages.push(err_msg);
return false;
}
// don't let it overwrite an already existing file unless it's an upcase change
if (exists(dst) && !boost::iequals (src, dst)) {
err_msg += L"[A file with that name already exists in this folder]";
m_messages.push (err_msg);
return false;
}
// rename
try {
rename(src, dst);
if (update_map_entry) {
// search for a map entry for src and update the LHS
boost::hash<std::wstring> hasher;
size_t hash = hasher (src_file);
// find it
MPSFilesToRenameMap::const_iterator iter = m_files_map.find (hash);
if (iter != m_files_map.end()) {
MPSFilesMapStruct record = (*iter).second;
record.root->set_separators (L"");
record.root->set_text (dst_file);
}
}
err_msg.clear ();
err_msg += L"Renamed \"";
err_msg += src_file;
err_msg += L"\" to \"";
err_msg += dst_file;
err_msg += L"\"";
m_messages.push (err_msg);
} catch (boost::filesystem::filesystem_error& fserr) {
std::string err = fserr.what();
std::wstring werr;
werr.assign(err.begin(), err.end());
err_msg += L"[" + werr + L"]";
m_messages.push(err_msg);
return false;
}
return true;
}
void MPSEngine::rename_all (const std::wstring& path) const
{
using namespace boost::filesystem;
MPSFilesToRenameMap::const_iterator iter = m_files_map.begin();
for ( ; iter != m_files_map.end(); ++iter) {
std::wstring src = (*iter).second.root->text();
src = path + L"\\" + src; // prepend folder path
std::wstring dst = (*iter).second.rename_to;
dst = path + L"\\" + dst; // prepend folder path
// is dst empty?
if (((*iter).second.rename_to).empty ()) {
std::wstring err_msg = L"Failed to rename [";
err_msg += (*iter).second.root->text();
err_msg += L"], destination file name is null string.";
m_messages.push(err_msg);
continue;
}
// skip same source and destination
if ((*iter).second.root->text() == (*iter).second.rename_to) {
std::wstring err_msg = L"Skipped rename [";
err_msg += (*iter).second.root->text();
err_msg += L"], destination same as source";
m_messages.push(err_msg);
continue;
}
// dst should not exist, but allow case changes updates
if (exists(dst) && !boost::iequals ((*iter).second.root->text(), (*iter).second.rename_to)) {
std::wstring err_msg = L"Failed to rename [";
err_msg += (*iter).second.root->text();
err_msg += L"] to [";
err_msg += (*iter).second.rename_to;
err_msg += L"], a file with that name already exists in current folder.";
m_messages.push(err_msg);
continue;
}
// rename one file
(void) rename_one (path, (*iter).second.root->text(), (*iter).second.rename_to, true);
}
}
void MPSEngine::update (MPSToken* token)
{
// force update token
update_token(token, token->text(), token->separators(), token->is_discard(), true);
}
bool MPSEngine::is_token_current_root(const MPSToken* token) const
{
if (!token)
return false;
if (!m_master_token)
return false;
return (token == m_master_token);
}