-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (86 loc) · 3.33 KB
/
main.cpp
File metadata and controls
112 lines (86 loc) · 3.33 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
#include "./src/app/encryptDecrypt/Cryption.hpp"
#include "./src/app/fileHandling/IO.hpp"
#include "./src/app/processes/ProcessManagement.hpp"
#include "./src/threadPool/threadPool.hpp"
#include "src/app/processes/Task.hpp"
#include <algorithm>
#include <chrono>
#include <filesystem>
#include <iostream>
#include <memory>
namespace fs = std::filesystem;
int main(int argc, char const *argv[]) {
std::string directory;
std::string action;
std::cout << "Enter the directory path: " << std::endl;
std::getline(std::cin, directory);
std::cout << "Enter the action (ENCRYPT/DECRYPT): " << std::endl;
std::getline(std::cin, action);
// create an instance of the thread pool
ThreadPool pool;
ProcessManagement pm;
auto start = std::chrono::high_resolution_clock::now();
if (action == "DECRYPT") {
try {
// first check if it exists and is a directory
if (fs::exists(directory) && fs::is_directory(directory)) {
// loop over all the files inside that folder
for (const auto &entry : fs::recursive_directory_iterator(directory)) {
if (entry.is_regular_file()) {
// take the filepath
std::string filePath = entry.path().string();
// io opens/closes that file and gives file stream
IO io(filePath);
// take the ownership of the file stream
std::fstream fstream = io.getFileStream();
if (fstream.is_open()) {
std::unique_ptr<Task> task = std::make_unique<Task>(
std::move(fstream), Action::DECRYPT, filePath);
pm.submitToQueue(std::move(task));
} else {
std::cout << "Unable to open the file: " << filePath << std::endl;
}
}
}
} else {
std::cout << "Invalid directory path: " << std::endl;
}
pm.executeTasks();
} catch (const fs::filesystem_error &e) {
std::cout << "FileSystem error: " << e.what() << std::endl;
}
} else {
try {
// first check if it exists and is a directory
if (fs::exists(directory) && fs::is_directory(directory)) {
// loop over all the files inside that folder
for (const auto &entry : fs::recursive_directory_iterator(directory)) {
if (entry.is_regular_file()) {
// take the filepath
std::string filePath = entry.path().string();
// io opens/closes that file and gives file stream
IO io(filePath);
// take the ownership of the file stream
std::fstream fstream = io.getFileStream();
if (fstream.is_open()) {
std::string taskData = filePath + "::" + action;
pool.addTask([taskData]() mutable { executeCryption(taskData); });
} else {
std::cout << "Unable to open the file: " << filePath << std::endl;
}
}
}
} else {
std::cout << "Invalid directory path: " << std::endl;
}
} catch (const fs::filesystem_error &e) {
std::cout << "FileSystem error: " << e.what() << std::endl;
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::clog << "Took " << duration.count() << " ms" << " for " << action
<< std ::endl;
return 0;
}