-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.cpp
More file actions
174 lines (141 loc) · 5.44 KB
/
FileReader.cpp
File metadata and controls
174 lines (141 loc) · 5.44 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
//
// Created by luise on 5/3/2020.
//
#include "FileReader.h"
// Default constructor
FileReader::FileReader(): networkFile("") {
auto inputFiles_folder = "InputFiles/";
filesystem::path file_name = inputFiles_folder;
if (file_name == "") {
cout << "Directory Error: Invalid path to a folder, exiting..." << endl;
exit(EXIT_FAILURE);
}
parseCurrentFile(file_name);
string networkFiles = file_name.generic_string();
networkFiles.pop_back();
setCommands(networkFiles + "networks.txt");
}
void FileReader::setCommands(string file_name) {
//setFileName(std::move(file_name));
// Creates a directory called "/OutputFiles" if it does not
// already exist.
if (mkdir("OutputFiles", 0777) == -1){
cerr << "Error : " << strerror(errno) << " : 'OutputFiles' Folder" << endl;
}
// Iterate through the file given in the command line and
// create output files for each size/algorithm.
// Store input files and output file names in separate vectors
string filename, tempFilename;
// fileID of type integer helps keep count just in case there are more than 3 inputted network data files
int fileID = 1;
for(int i = 0; i < inputFiles.size(); i++){
addOutputFileSet(fileID);
fileID++;
}
/*while (!cfin.eof()) {
// Creates a set of 3 .txt files for each file
cfin >> filename;
if(tempFilename != filename){
inputFiles.push_back(filename);
addOutputFileSet(fileID);
fileID++;
}
tempFilename = filename;
}
cfin.close();*/
}
// setFileName sets the given file name in the command line to the main file as well as checking if it is the
// correct format type.
void FileReader::setFileName(string filename){
networkFile = std::move(filename);
int outputFilename_size = networkFile.size();
// Create a backupnetworkFile (simple original filename with .txt added to the end)
string backupnetworkFile = networkFile + ".txt";
// Save the last 4 characters of the string to verify it is a .txt file
string file_extension;
for(int i = 4; i > 0; i--){
file_extension += networkFile[outputFilename_size - i];
}
// if given file does not have .txt file extension, set networkFile equal to the backupnetworkFile
if(file_extension != ".txt"){
cfin.open(backupnetworkFile.c_str());
if(!cfin.is_open()){
cout << "'" << networkFile << "' or '" << backupnetworkFile << "' could not be opened. Please check command line." << endl;
exit(-1);
}
networkFile = backupnetworkFile;
}
else{
cfin.open(networkFile.c_str());
if(!cfin.is_open()){
cout << "'" << networkFile << "' could not be opened. Please check command line." << endl;
exit(-1);
}
}
}
// addOutputFileSet creates a set of 3 output files with a custom ID number at the front
void FileReader::addOutputFileSet(int fileID) {
string tempFilename;
// FILE *fp = fopen( ("OutputFiles/" + tempFilename).c_str(), "w+"); fclose(fp);
{ tempFilename = to_string(fileID) + "_Trivial.txt";
outputFiles.push_back(tempFilename);
fstream newFile(("OutputFiles/" + tempFilename).c_str(), ios::out); }
{ tempFilename = to_string(fileID) + "_BellmanFord.txt";
outputFiles.push_back(tempFilename);
fstream newFile(("OutputFiles/" + tempFilename).c_str(), ios::out); }
{ tempFilename = to_string(fileID) + "_FloydWarshall.txt";
outputFiles.push_back(tempFilename);
fstream newFile(("OutputFiles/" + tempFilename).c_str(), ios::out); }
}
// Returns inputFiles vector as reference
vector<string>& FileReader::getInputFiles(){
return this->inputFiles;
}
// Returns outputFiles vector as reference
vector<string>& FileReader::getOutputFiles(){
return this->outputFiles;
}
void FileReader::parseCurrentFile(const filesystem::path& fileName){
filesystem::directory_iterator end;
//fstream fout;
string networksData_Files = fileName.generic_string();
networksData_Files.pop_back();
/*networksData_Files += "networks.txt";
fout.open(networksData_Files.c_str(), std::ios::out);
if (!fout.is_open()) {
cout << "'" << networksData_Files << "' could not be opened. Please check input files." << endl;
exit(-1);
}*/
string tempFile;
for(filesystem::directory_iterator theIterator(fileName) ; theIterator!= end; ++theIterator){
// directory iterator is first converted to a path
filesystem::path dirToPath = *theIterator;
tempFile = dirToPath.generic_string();
if(tempFile != "InputFiles/networks.txt"){
tempFile = "/" + parsePathName(tempFile);
tempFile = parsePathName(tempFile);
tempFile.erase(0,1);
//fout << tempFile << endl;
}
inputFiles.push_back(tempFile);
}
//fout.close();
}
string FileReader::parsePathName(string section){
for(size_t start = 0; start < section.size(); start++){
if(section[start] == '/'){
size_t end = start + 1;
while(section[end] != '/' && end < section.size()){
end++;
if(end == section.size()){
return section;
}
}
section.erase(start, end - start);
start--;
}
}
return section;
}
// FileReader destructor
FileReader::~FileReader()= default;