-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArquivo.cpp
258 lines (211 loc) · 8.85 KB
/
Arquivo.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
#include "Arquivo.h"
using namespace std;
const char* DIRETORIO_RAIZ = "Arquivos_IO/";
void Arquivo::salvar_entrada(string nome_algorithm, DadosEntrada entrada){
char entrada_arquivo[200];
FILE* arquivo = NULL;
const char* caminho_arquivo;
string nome_pasta;
const char* path_pasta;
if(entrada.tipo_entrada == CRESCENTE){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_entrada/crescente/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta);
string numStr = to_string(entrada.tamanho);
nome_pasta += ("entrada_crescente_" + numStr + ".txt");
}if(entrada.tipo_entrada == DECRESCENTE){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_entrada/decrescente/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta);
string numStr = std::to_string(entrada.tamanho);
nome_pasta += ("entrada_decrescente_" + numStr + ".txt");
}else if(entrada.tipo_entrada == RANDOM){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_entrada/random/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta);
string numStr = std::to_string(entrada.tamanho);
nome_pasta += ("entrada_random_" + numStr + ".txt");
}
caminho_arquivo = nome_pasta.c_str();
arquivo = fopen(caminho_arquivo, "w");
if(arquivo == NULL){cout << "Nao foi possivel abrir o arquivo" << endl;exit(1);}
sprintf(entrada_arquivo, "%d\n", entrada.tamanho);
fprintf(arquivo,"%s", entrada_arquivo);
for(int i = 0; i < entrada.tamanho ; i++){
sprintf(entrada_arquivo, "%d\n", entrada.vector[i]);
fprintf(arquivo,"%s", entrada_arquivo);
}
fclose(arquivo);
}
void Arquivo::salvar_saida(string nome_algorithm, DadosEntrada entrada){ //to do neguin
char entrada_arquivo[200];
FILE* arquivo = NULL;
const char* path_pasta;
const char* caminho_arquivo;
string nome_pasta;
string tamanho_Str;
if(entrada.tipo_entrada == CRESCENTE){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_saida/crescente/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta);
tamanho_Str = std::to_string(entrada.tamanho);
nome_pasta += ("saida_crescente_" + tamanho_Str + ".txt");
}else if(entrada.tipo_entrada == DECRESCENTE){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_saida/decrescente/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta);
tamanho_Str = std::to_string(entrada.tamanho);
nome_pasta += ("saida_decrescente_" + tamanho_Str + ".txt");
}else if(entrada.tipo_entrada == RANDOM){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_saida/random/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta); //confere se tem a pasta criada e cria se nao tiver
tamanho_Str = std::to_string(entrada.tamanho);
nome_pasta += ("saida_random_" + tamanho_Str + ".txt");
}
caminho_arquivo = nome_pasta.c_str();
arquivo = fopen(caminho_arquivo, "w");
if(arquivo == NULL){cout << "Nao foi possivel abrir o arquivo" << endl;exit(1);}
for(int i = 0; i < entrada.tamanho ; i++){
sprintf(entrada_arquivo, "%d\n", entrada.vector[i]);
fprintf(arquivo,"%s", entrada_arquivo);
}
fclose(arquivo);
}
void Arquivo::salvar_tempo(string nome_algorithm, DadosEntrada entrada, double duracao){
FILE *arq;
char tempos_execucao[100];
string nome_pasta;
const char* path_pasta;
const char* caminho_arquivo;
string tamanho_Str;
if(entrada.tipo_entrada == CRESCENTE){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_tempo/crescente/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta);
tamanho_Str = to_string(entrada.tamanho);
nome_pasta += ("tempo_crescente_" + tamanho_Str + ".txt");
}else if(entrada.tipo_entrada == DECRESCENTE){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_tempo/decrescente/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta);
tamanho_Str = to_string(entrada.tamanho);
nome_pasta += ("tempo_decrescente_" + tamanho_Str + ".txt");
}else if(entrada.tipo_entrada == RANDOM){
nome_pasta = DIRETORIO_RAIZ + (nome_algorithm + "/arquivos_tempo/random/");
path_pasta = nome_pasta.c_str();
criar_pasta(path_pasta);
tamanho_Str = to_string(entrada.tamanho);
nome_pasta += ("tempo_random_" + tamanho_Str + ".txt");
}
caminho_arquivo = nome_pasta.c_str();
arq = fopen(caminho_arquivo, "a");
sprintf(tempos_execucao, "Tempo: %lf Tamanho: %d\n", abs(duracao), entrada.tamanho);
fprintf(arq,"%s", tempos_execucao);
fclose(arq);
}
bool existe_diretorio(const char* path){
DWORD fileAttributes = GetFileAttributesA(path);
return (fileAttributes != INVALID_FILE_ATTRIBUTES) && (fileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}
void Arquivo::criar_pasta(const char* path){
if (existe_diretorio(path)) {
} else {
char command[100]; // Ajuste o tamanho conforme necessário
snprintf(command, sizeof(command), "mkdir \"%s\"", path);
system(command);
}
}
bool listarDiretoriosExistents(const char* rootPath, std::vector<std::string>& diretorios) {
string searchPath = string(rootPath) + "\\*";
WIN32_FIND_DATAA findFileData;
HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findFileData);
if (hFind == INVALID_HANDLE_VALUE) {
cout << "Nenhum diretorio encontrado." << endl;
return false;
}
do {
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (strcmp(findFileData.cFileName, ".") != 0 && strcmp(findFileData.cFileName, "..") != 0) {
diretorios.push_back(findFileData.cFileName);
}
}
} while (FindNextFileA(hFind, &findFileData) != 0);
FindClose(hFind);
return true;
}
void Arquivo::apagar_pasta_especifica(){
const char* rootPath = "Arquivos_IO";
vector<string> diretorios;
if (listarDiretoriosExistents(rootPath, diretorios)) {
cout << "Diretorios existentes em " << rootPath << ":" << endl;
for (size_t i = 0; i < diretorios.size(); ++i) {
cout << i + 1 << ". " << diretorios[i] << endl;
}
}
cout << "Escolha o diretorio a ser apagado (0 para sair): ";
int escolha;
cin >> escolha;
if (escolha > 0 && escolha <= static_cast<int>(diretorios.size())) {
string dirPath = string(rootPath) + "\\" + diretorios[escolha - 1];
cout << "Apagando o diretorio " << dirPath << "..." << endl;
string command = "rmdir /s /q \"" + dirPath + "\"";
system(command.c_str());
}
}
void exibirConteudoArquivoTxt(const string& filePath) {
ifstream arquivo(filePath);
if (!arquivo.is_open()) {
return;
}
string linha;
while (getline(arquivo, linha)) {
cout << linha << endl;
}
arquivo.close();
}
void listarArquivosTxt(const string& dirPath, string tipo_entrada) {
// Abre o diretório
DIR* directory = opendir(dirPath.c_str());
if (directory == nullptr) {
return;
}
cout << "Tempo " + tipo_entrada << ":" << endl;
struct dirent* entry;
while ((entry = readdir(directory)) != nullptr) {
string fileName = entry->d_name;
// Verifica se o arquivo possui extensão .txt
if (fileName.length() > 4 && fileName.substr(fileName.length() - 4) == ".txt") {
string filePath = dirPath + "\\" + fileName;
exibirConteudoArquivoTxt(filePath);
}
}
// Fecha o diretório
closedir(directory);
}
void Arquivo::visualizar_tempos(){
const char* rootPath = "Arquivos_IO";
vector<string> diretorios;
string num;
if (listarDiretoriosExistents(rootPath, diretorios)) {
cout << "Diretorios disponiveis para visualizacao: " << endl;
for (size_t i = 0; i < diretorios.size(); ++i) {
cout << i + 1 << ". " << diretorios[i] << endl;
}
}
cout << "Escolha o algoritmo para ser visualizado (0 para sair): ";
int escolha;
cin >> escolha;
if (escolha > 0 && escolha <= static_cast<int>(diretorios.size())) {
string dirPath = string(rootPath) + "\\" + diretorios[escolha - 1];
dirPath += "\\arquivos_tempo"; // Adiciona "arquivos_tempo" ao caminho
cout << "Diretorio: " << dirPath << endl << endl;
// Listar arquivos .txt dentro das subpastas
listarArquivosTxt(dirPath + "\\crescente", "crescente");
cout << endl;
listarArquivosTxt(dirPath + "\\decrescente", "decrescente");
cout << endl;
listarArquivosTxt(dirPath + "\\random", "random");
system("pause");
}
}