-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileManager.hpp
234 lines (200 loc) · 6.25 KB
/
fileManager.hpp
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
#ifndef FILEMANAGER_HPP
#define FILEMANAGER_HPP
#include <iostream>
#include <vector>
#include <chrono>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <random>
#include <ctime>
#include <algorithm>
#include <filesystem>
#include "interface.hpp"
#include "algorithms.hpp"
class FileManager
{
public:
FileManager();
~FileManager();
std::string generateFileAddress(AlgorithmType algorithm, InputType input, int inputSize, FileType fileType);
std::string generateFile(AlgorithmType algorithm, InputType inputStyle, int inputSize);
std::vector<int> loadFile(const std::string &fileName);
bool checkIfDirectoryExists(const std::string &address);
void saveFile(const std::vector<int> &arr, AlgorithmType algorithm, InputType inputStyle, int inputSize);
void saveTime(AlgorithmType algorithm, InputType inputStyle, int inputSize, std::chrono::milliseconds time);
};
FileManager::FileManager() {}
FileManager::~FileManager() {}
std::string FileManager::generateFile(AlgorithmType algorithm, InputType inputStyle, int inputSize)
{
std::string fileName = generateFileAddress(algorithm, inputStyle, inputSize, Input);
std::ofstream file(fileName);
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<int> range(0, 1000000);
if (!file.is_open())
{
std::cerr << "Error opening the file -> " << fileName << "." << std::endl;
return "";
}
file << inputSize << std::endl;
std::vector<int> arr(inputSize);
switch (inputStyle)
{
case Random:
std::generate(arr.begin(), arr.end(), [&]()
{ return range(generator); });
break;
case Crescente:
std::generate(arr.begin(), arr.end(), [&]()
{ return range(generator); });
std::sort(arr.begin(), arr.end());
break;
case Decrescente:
std::generate(arr.begin(), arr.end(), [&]()
{ return range(generator); });
std::sort(arr.begin(), arr.end(), std::greater<int>());
break;
}
for (int num : arr)
{
file << num << std::endl;
}
file.close();
std::cout << "Input file saved successfully -> " << fileName << std::endl;
return fileName;
}
std::vector<int> FileManager::loadFile(const std::string &fileName)
{
std::ifstream file(fileName);
std::vector<int> arr;
int size, num;
if (!file.is_open())
{
std::cerr << "Error opening the file -> " << fileName << "." << std::endl;
return arr;
}
file >> size;
for (int i = 0; i < size; i++)
{
file >> num;
arr.push_back(num);
}
file.close();
return arr;
}
bool FileManager::checkIfDirectoryExists(const std::string &address)
{
std::filesystem::path normalizedPath = address;
normalizedPath = normalizedPath.make_preferred(); // Use the proper path separator for the operating system
try
{
std::filesystem::create_directories(normalizedPath);
std::cout << "Folder(s) created successfully -> " << address << std::endl;
return true;
}
catch (const std::exception &ex)
{
std::cerr << "Failed to create folder(s) -> " << normalizedPath.string() << " - " << ex.what() << std::endl;
return false;
}
}
void FileManager::saveFile(const std::vector<int> &arr, AlgorithmType algorithm, InputType inputStyle, int inputSize)
{
std::string fileName = generateFileAddress(algorithm, inputStyle, inputSize, Output);
std::ofstream file(fileName);
if (!file.is_open())
{
std::cerr << "Error opening the file for writing." << std::endl;
return;
}
file << inputSize << std::endl;
for (int num : arr)
{
file << num << std::endl;
}
file.close();
std::cout << "Output file saved successfully -> " << fileName << std::endl;
}
void FileManager::saveTime(AlgorithmType algorithm, InputType inputStyle, int inputSize, std::chrono::milliseconds time)
{
std::string fileName = generateFileAddress(algorithm, inputStyle, inputSize, Time);
std::ofstream file(fileName);
if (!file.is_open())
{
throw std::runtime_error("Error opening the file -> " + fileName);
}
std::string algorithmName = includedAlgorithms[algorithm];
std::string inputStyleText;
switch (inputStyle)
{
case Random:
inputStyleText = std::to_string(inputSize) + " random element(s)";
break;
case Crescente:
inputStyleText = std::to_string(inputSize) + " random element(s) in ascending order";
break;
case Decrescente:
inputStyleText = std::to_string(inputSize) + " random element(s) in descending order";
break;
default:
throw std::runtime_error("Invalid input style.");
}
file << "Algorithm: " << algorithmName << std::endl;
file << "Input: " << inputStyleText << std::endl;
file << "Time spent: " << time.count() << " milliseconds." << std::endl;
file.close();
std::cout << "Time saved successfully -> " << fileName << std::endl;
}
std::string FileManager::generateFileAddress(AlgorithmType algorithm, InputType input, int inputSize, FileType fileType)
{
std::string algorithmName = includedAlgorithms[algorithm];
std::string inputName;
switch (input)
{
case Random:
inputName = "Random";
break;
case Crescente:
inputName = "Crescente";
break;
case Decrescente:
inputName = "Decrescente";
break;
}
std::string fileTypeName;
switch (fileType)
{
case Input:
fileTypeName = "Arquivos de Entrada";
break;
case Output:
fileTypeName = "Arquivos de Saida";
break;
case Time:
fileTypeName = "Arquivos de Tempo";
break;
}
std::string typeName = algorithmName + "/" + fileTypeName + "/" + inputName + "/";
if (!checkIfDirectoryExists(typeName))
{
return "";
}
switch (fileType)
{
case Input:
fileTypeName = "Entrada";
break;
case Output:
fileTypeName = "Saida";
break;
case Time:
fileTypeName = "Tempo";
break;
}
typeName += fileTypeName + inputName + std::to_string(inputSize) + ".txt";
return typeName;
}
#endif