-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.cpp
More file actions
710 lines (556 loc) · 19.2 KB
/
Copy pathfunctions.cpp
File metadata and controls
710 lines (556 loc) · 19.2 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#include "functions.h"
#include "conversions.h"
#include "unordered_set"
//interal linkage by anon namespace
namespace
{
//represents the total present files in the directory
int filecount = 0;;
//represents the total present folders in the directory
int foldercount = 0;;
////represents the final amount of searched files by all threads
int processedFiles = 0;;
//represents the final amount of searched folders by all threads
int processedFolders = 0;;
//At any point in time this represents the skipped files combined by all threads.
std::vector<std::filesystem::directory_entry> vec_skipped_files = {};
//At any point in time this represents the total number of skipped folders combined by all threads.
int skipped_folders = 0;;
//At any point in time this represents the ratio between the currentfilecount and the totalfilecount in percent
double percentage = (0);
std::mutex mtx;
std::filesystem::directory_entry search_path;
}
//bool skipFiles(std::filesystem::recursive_directory_iterator& iter, std::error_code& ec, uint64_t& p_free_threads)
//{
// for (int i = 0; i < p_free_threads - 1; i++) //skips x files every turn so all files only get processesed by one thread ever
// iter.increment(ec);
// return true;
//}
static void lagfree_terminal_cout(const std::string& string)
{
COORD c = { 0, 0 };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
static std::vector<std::string> prev_buffer = {};
std::vector<std::string> buffer = {};
std::string temp = "";
//for each present newline in the string a new string is appended to the vector
//Each Element equals 1 line of the desired terminal output
for (int i = 0; i < string.length(); i++)
{
if (string[i] == '\n')
{
buffer.push_back(temp);
temp = "";
}
else
temp += string[i];
}
temp = "";
//now we can replace the old buffer with this new buffer data but replacing the excessive old elements (when present) in it with empty strings
int y = 0;
for (; y < buffer.size(); y++)
{
temp += buffer[y];
if (y < prev_buffer.size())
{
for (int x = 0; x < prev_buffer[y].length(); x++)
{
temp += " ";
}
prev_buffer.at(y) = temp;
}
else
prev_buffer.push_back(temp);
}
for (; y < prev_buffer.size(); y++)
{
for (int x = 0; x < prev_buffer[y].length(); x++)
{
temp += " ";
}
prev_buffer.at(y) = (temp);
}
temp = "";
//then we iterate over the old buffer containing the desired output and adding them together with newline chars to a single string to then std::cout.
for (auto& it : prev_buffer)
{
temp += it;
temp += "\n";
}
std::cout << temp;
//now our given string in form of the vector is our "old" buffer for the next ieration
prev_buffer = buffer;
}
//TODO Let it track a static uint64_t of how many chars have been written the last time, then just set position to 0,0 and overwrite the locations to whatever string to write
// is longer the old one or the new one
void printProgress()
{
//auto start = std::chrono::high_resolution_clock::now();
//auto stop = std::chrono::high_resolution_clock::now();
//auto duration = duration_cast<std::chrono::microseconds>(stop - start);
system("CLS");
while (percentage < 100)
{
double maxPrint = 20;
percentage = (static_cast<double>(processedFiles) / filecount) * 100;
double toPrint = percentage / 5;
std::string print = "";
print += "[";
for (uint8_t i = 0; i < toPrint; i++)
{
print += '\|';
maxPrint--;
}
for (uint8_t i = 0; i < maxPrint; i++)
{
print += ' ';
}
print += "]" + std::to_string(percentage) + "%\n";
lagfree_terminal_cout(print);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
system("CLS");
}
//void printProgress(double& percentage)
//{
// while (percentage < 100)
// {
// double maxPrint = 20;
// double toPrint = percentage / 5;
// system("CLS");
// std::cout << "[";
// for (uint64_t i = 0; i < toPrint; i++)
// {
// std::cout << '\|';
// maxPrint--;
// }
// for (uint64_t i = 0; i < maxPrint; i++)
// {
// std::cout << ' ';
// }
// std::cout << "]" << percentage << "%\n";
// std::this_thread::sleep_for(std::chrono::milliseconds(500));
// }
// system("CLS");
//}
bool validateInputStringForInitialInput(std::string input, std::vector<std::string>& output)
{
if (input.empty())
return true;
else
output.push_back(to_lower_string(input));
return false;
}
std::string getFolderPath(std::string pathOfFile)
{
std::string pathOfFolder = "";
for (uint64_t i = 0; i < pathOfFile.find_last_of("\\"); i++)
{
pathOfFolder += pathOfFile.at(i);
}
return pathOfFolder;
}
void BrowseToFile(LPCWSTR filename)
{
ITEMIDLIST* pidl = ILCreateFromPathW(filename);
if (pidl) {
SHOpenFolderAndSelectItems(pidl, 0, 0, 0);
ILFree(pidl);
}
}
void BrowseToFolder(LPCWSTR filename) //thx to "Jonathan Potter" on stackoverflow
{
PIDLIST_ABSOLUTE pidl;
if (SUCCEEDED(SHParseDisplayName(filename, 0, &pidl, 0, 0)))
{
// we don't want to actually select anything in the folder, so we pass an empty
// PIDL in the array. if you want to select one or more items in the opened
// folder you'd need to build the PIDL array appropriately
ITEMIDLIST idNull = { 0 };
LPCITEMIDLIST pidlNull[1] = { &idNull };
SHOpenFolderAndSelectItems(pidl, 1, pidlNull, 0);
ILFree(pidl);
}
}
//validates inputstring (returns false if wrong input) and transfers input string to vector
bool validateInputStringForOpeningFinalEntriesInExplorer(std::string input, std::vector<uint64_t>& output)
{
std::string set = "";
for (uint64_t i = 0; i < input.length(); i++)
{
if (isdigit(input.at(i)))
{
set += (input.at(i));
}
else
if (input.at(i) == ',' || input.at(i) == ' ')
{
output.push_back(stoi(set));
set = "";
continue;
}
else
return false;
}
if (set != "")
{
output.push_back(stoi(set));
}
return true;
}
// Base case for recursion termination
static void sortPathVectors() {
// Do nothing
}
// Using recursive calls to this variadic template function
template<typename First, typename ... Rest>
void sortPathVectors(First& arg, Rest&... rest) {
std::vector<std::filesystem::directory_entry> copyVec;
std::unordered_set<std::filesystem::path> mapped_path_vector;
for (auto& it : arg)
{
mapped_path_vector.insert(it.path());
}
for (std::filesystem::recursive_directory_iterator iter(search_path.path()); iter != std::filesystem::end(iter); iter++)
{
if (mapped_path_vector.find((*iter).path()) != mapped_path_vector.end())
{
copyVec.push_back(*iter);
}
}
arg = copyVec;
sortPathVectors(rest...); // Recursively sort the rest
}
void display(
auto seconds,
std::vector<std::filesystem::directory_entry>& vec_folder_path,
std::vector<std::filesystem::directory_entry>& vec_content_path,
std::vector<std::filesystem::directory_entry>& vec_file_path)
{
std::vector<uint64_t> vec_to_be_opened; //storing the identifier for files/folders to be opened
system("CLS");
int i = 0;
std::cout << "\nProcessed an astounding " << filecount - vec_skipped_files.size() << " files (of " << filecount << ") and "
<< processedFolders << " foldernames (of " << foldercount << ") in just " << seconds / 1000000
<< " seconds.\nSkipped Files : " << vec_skipped_files.size() << "\n\n";
sortPathVectors(vec_content_path, vec_file_path, vec_folder_path);
if (!vec_folder_path.empty())
{
std::cout << "Found Folders\n****************************************************************\n";
for (int j = 0; j < vec_folder_path.size(); j++, i++)
{
std::string spaces(std::to_string(vec_folder_path.size()).length() - std::to_string(i+1).length(), ' ');
std::cout << i + 1 << ": "<< spaces << wide_string_to_string(vec_folder_path.at(j).path()) << std::endl;
}
std::cout << "****************************************************************\n" << std::endl;
}
if (!vec_file_path.empty())
{
std::cout << "Found Files\n****************************************************************\n";
for (int j = 0; j < vec_file_path.size(); j++, i++)
{
std::string spaces(std::to_string(vec_file_path.size()).length() - std::to_string(i + 1).length(), ' ');
std::cout << i + 1 << ": " << spaces << wide_string_to_string(vec_file_path.at(j).path()) << std::endl;
}
std::cout << "****************************************************************\n" << std::endl;
}
if (!vec_content_path.empty())
{
std::cout << "Found Content\n****************************************************************\n";
for (int j = 0; j < vec_content_path.size(); j++, i++)
{
std::string spaces(std::to_string(vec_content_path.size()).length() - std::to_string(i + 1).length(), ' ');
std::cout << i + 1 << ": " << spaces << wide_string_to_string(vec_content_path.at(j).path()) << std::endl;
}
std::cout << "****************************************************************\n" << std::endl;
}
std::cout << "Skipped Files:\n";
for (const auto& entry : vec_skipped_files)
{
std::string temp = wide_string_to_string(entry.path());
std::cout << temp << std::endl; // Tab at the front, newline at the end
}
std::cout << "\n\n";
std::string to_be_opened = ""; //Stringinput of what files the user wants to be opened
if (!(vec_content_path.empty() && vec_file_path.empty() && vec_folder_path.empty()))
{
do
{
std::cout << "What should be opened? Input-Example: \"1,2,3,40,53\"" << std::endl;
std::getline(std::cin, to_be_opened);
} while (!validateInputStringForOpeningFinalEntriesInExplorer(to_be_opened, vec_to_be_opened));
}
else
{
std::cout << "No files match the searching criteria :(" << std::endl;
std::cout << "Press \"Enter\" to exit.";
std::cin.ignore();
abort;
}
for (auto& it : vec_to_be_opened)
{
if (vec_folder_path.size() >= it)
{
auto x = (vec_folder_path.at(it - 1));// (LPCSTR((char*)(filesystem::path(vec_folder_path.at(it - 1)).c_str())))
std::cout << x << "\n";
std::string askdjasd = ((x).path().string() + "\\\\");
const char* y = askdjasd.c_str();
ShellExecuteA(NULL, "open", y, NULL, NULL, SW_SHOWDEFAULT);
}
else
if (vec_file_path.size() + vec_folder_path.size() >= it)
{
BrowseToFile(LPCTSTR((char*)((vec_file_path.at(it - vec_folder_path.size() - 1).path()).c_str())));
}
else
if (vec_content_path.size() + vec_file_path.size() + vec_folder_path.size() >= it)
{
BrowseToFile(LPCTSTR((char*)((vec_content_path.at(it - 1 - vec_folder_path.size() - vec_file_path.size()).path()).c_str())));
}
}
}
void startWinXSearch(const std::filesystem::path pathToFolder, bool searchFolders, bool searchContent, std::vector<std::string> vecSearchValue)
{
search_path.assign(pathToFolder);
auto start = std::chrono::high_resolution_clock::now();
uint64_t current_thread_count = 1;
uint64_t processor_count = std::thread::hardware_concurrency();
debug_log << "Found " << processor_count << " CPUs.\n";
if (!(std::filesystem::directory_entry(pathToFolder.c_str()).is_directory()))
{
std::cout << "PATH IS NOT A DIRECTORY!" << std::endl;
std::cin.ignore();
abort;
}
std::vector<std::filesystem::directory_entry> vec_folder_path; //stores folder paths of folders including atleast one of the searched strings
std::vector<std::filesystem::directory_entry> vec_content_path; //stores file paths of files whichs content includes atleast one of the searched strings
std::vector<std::filesystem::directory_entry> vec_file_path; //stores file paths of files including atleast one of the searched strings
std::vector<std::thread>thread_list;
auto ec = std::error_code();
//Calculates Amount of Files
std::cout << "Scanning files\n";
for (std::filesystem::recursive_directory_iterator iter(pathToFolder); iter != std::filesystem::end(iter); iter.increment(ec))
{
if ((*iter).is_regular_file())
filecount++;
else if ((*iter).is_directory())
foldercount++;
}
//Starts ProgressBar Thread
std::thread t1(printProgress);
thread_list.push_back(std::move(t1));
current_thread_count++;
debug_log << "Starting big_for_loop\n";
//std::thread tnew(big_for_loop,
// 1,
// 1,
// std::ref(currentfilecount),
// std::ref(pathToFolder),
// std::ref(percentage),
// std::ref(filecount),
// std::ref(searchFolders),
// std::ref(searchContent),
// std::ref(vecSearchValue),
// std::ref(vec_folder_path),
// std::ref(vec_content_path),
// std::ref(vec_file_path));
//thread_list.push_back(std::move(tnew));
//current_thread_count++;
for (uint64_t i = 1; i <= processor_count; i++)
{
std::thread tnew(big_for_loop,
i,
std::ref(processor_count),
std::ref(pathToFolder),
std::ref(searchFolders),
std::ref(searchContent),
std::ref(vecSearchValue),
std::ref(vec_folder_path),
std::ref(vec_content_path),
std::ref(vec_file_path));
thread_list.push_back(std::move(tnew));
current_thread_count++;
}
for (uint64_t i = 1; i < thread_list.size(); i++)
{
if (thread_list.at(i).joinable())
{
thread_list.at(i).join();
}
}
percentage = 100;
thread_list.at(0).join();
auto stop = std::chrono::high_resolution_clock::now();
auto duration = duration_cast<std::chrono::microseconds>(stop - start);
display(duration.count(), vec_folder_path, vec_content_path, vec_file_path);
}
void big_for_loop(uint64_t p_i, uint64_t total_threads, const std::filesystem::path pathToFolder, bool& searchFolders, bool& searchContent,
std::vector<std::string>& vecSearchValue,
std::vector<std::filesystem::directory_entry>& vec_folder_path,
std::vector<std::filesystem::directory_entry>& vec_content_path,
std::vector<std::filesystem::directory_entry>& vec_file_path)
{
//tracks this threads processed files
//tracks this threads skipped files
bool onetime_skipped = false;
// Iterates over every file/folder in the path of the executable and its subdiretories
debug_log << "Started new Thread with: p_i:" << p_i << ", Total_Threads:" << total_threads << ".\n";
//file iterator loop
// Check if the given path exists and is a directory
if (std::filesystem::exists(pathToFolder) && std::filesystem::is_directory(pathToFolder))
{
for (std::filesystem::recursive_directory_iterator dirIter(pathToFolder), end; dirIter != end; ++dirIter)
{
bool skipped_file = false;
// Print the path of the current file or directory
try
{
/*
This if/else block implements a logic based parallelism.
For X threads introduced every thread skipps X files continously. By that every file gets processed excactly once.
At the very beginning the first thread doesn't skip at all, the second thread skips 1 file ... the last created thread skips X-1 files.
This approach supersedes runtime assinging of files to specific threads, but can lead to some threads having a bigger load to bear than others (randomly gets assigned 2 of the largest files).
This downside is only a potential issue when filecontentsearch is enabled.
*/
if (onetime_skipped)
{ //Continued Skipping for each Iteration (every thread skips the threadsamount of files, so that each file only is searched by one thread during the whole runtime
uint64_t x = 1;
for (; x < total_threads; x++)
{
++dirIter;
if (dirIter == end) //we reached the End of this threads file iteration, gotta do a final update of processed files etc
{
return;
}
}
//if (x > 1) debug_log << "Thread " << p_i << " skipped " << x << " files at this iteration.\n";
}
else
{ //onetime skip at the start for each thread (first thread doesnt skip a file ; the 5. does skip 4 files
uint64_t counter = 0;
for (uint64_t i = 0; i < (p_i - 1); i++)
{
++dirIter;
if (dirIter == end) //we reached the End of this threads file iteration, gotta do a final update of processed files etc
{
return;
}
counter++;
}
//debug_log << "Thread Nr.:" << p_i << " skipped " << counter << " files at the start.\n";
//Now we did our one time skip, gotta let the loop know that.
onetime_skipped = true;
//we gotta keep going, because otherwise the first (total_threads) files would get ignored.
}
//the current file
const auto& entry = *dirIter;
//debug_log << "Thread Nr.:" << p_i << " is processing File: \"" << entry.path().string() << "\"\n";
//Skip if it isnt a file or a folder
if (!is_regular_file(entry) && !is_directory(entry))
{
debug_log << "Thread Nr.:" << p_i << " skipped Item: \"" << entry.path().string() << "\" Reason: no Folder or File\n";
if (!skipped_file)
mutual_vector_pushback(vec_skipped_files, entry);
skipped_file = true;
continue;
}
bool found = false;
if (std::filesystem::is_directory(entry)) // optional: Comparing Folder Name
{
if (searchFolders == false)
{
debug_log << "Skipped Folder: \"" << entry.path().string() << "\" Reason: FolderNameSearch deactivated\n";
continue;
}
//debug_log << "Processing Folder: \"" << entry.path().string() << "\"\n";
mutual_increment(processedFolders);
for (auto& it : vecSearchValue)
{
if (std::string::npos != to_lower_string(entry.path().filename().string()).find(to_lower_string(it)))
{
mutual_vector_pushback(vec_folder_path, entry);
found = true;
break;
}
}
continue;
}
else //since its not a directory, it has to be a file
{
mutual_increment(processedFiles);
//debug_log << "Processing File: \"" << entry.path().string() << "\"\n";
//Comparing File Name
for (auto& it : vecSearchValue)
{
if (std::string::npos != to_lower_string(entry.path().filename().string()).find(it)) {
mutual_vector_pushback(vec_file_path, entry);
found = true;
break;
}
}
if (found || searchContent == false) //continue iterating if search on filename was successfull or if content search is disabled
continue;
//Open File
std::ifstream inputfile;
inputfile.open(entry, std::ifstream::in);
// Check File State
if (inputfile && inputfile.rdstate() == std::ios_base::goodbit && inputfile.good())
{
std::string checkedString;
while (std::getline(inputfile, checkedString))
{
for (auto& searchIt : vecSearchValue)
{
if (std::string::npos != to_lower_string(checkedString).find(searchIt))
{
mutual_vector_pushback(vec_content_path, entry);
found = true;
break;
}
}
if (found)
break;
}
inputfile.close();
}
else
{
try
{
inputfile.close();
}
catch (const std::exception&)
{
std::cout << "Couldnt close file!" << std::endl;
}
if (!skipped_file)
{
debug_log << "Skipped Content Search on File: \"" << entry.path().string() << "\" Reason: File was not ready\n";
mutual_vector_pushback(vec_skipped_files, entry);
skipped_file = true;
}
continue;
}
continue;
}
}
catch (const std::filesystem::filesystem_error& e) {
//std::cerr << e.what() << std::endl;
if (!skipped_file)
mutual_vector_pushback(vec_skipped_files, (*dirIter));
continue;
}
catch (const std::exception& e) {
//std::cerr << "An error occurred: " << e.what() << std::endl;
if (!skipped_file)
mutual_vector_pushback(vec_skipped_files, (*dirIter));
continue;
}
}
}
else
{
std::cout << "The path does not exist or is not a directory." << std::endl;
}
}