-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataParse.cpp
116 lines (94 loc) · 3.5 KB
/
dataParse.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
#include "dataParse.h"
vector<string> dir_reader(const char *dirname){
vector<string> filenames;
DIR* dir = opendir(dirname);
if(dir == NULL){
string msg = "Error opening the directory";
cerr << "An empty directory "<< dirname << endl;
throw runtime_error(msg);
}
else{
struct dirent* inside_dir;
inside_dir = readdir(dir);
while(inside_dir != NULL) {
char full_path[300] = {0};
strcat(full_path, dirname);
strcat(full_path, inside_dir->d_name);
if (strlen(full_path) > 300) {
throw length_error("The path to nested folder exceedes the size of 300");
}
if(inside_dir->d_type == DT_REG && strcmp(inside_dir->d_name, ".DS_Store")!= 0){
filenames.push_back(full_path);
}
if(inside_dir->d_type == DT_DIR && strcmp(inside_dir->d_name, ".")!= 0 && strcmp(inside_dir->d_name, "..")!= 0){
dir_reader(full_path);
}
inside_dir = readdir(dir);
}
}
closedir(dir);
return filenames;
}
void read_record(vector<string> files, StudentList *student_list)
{
for(int i = 0; i < files.size(); i++)
{
ifstream fin;
fin.open(files[i], ios::in);
if(!fin.is_open()){
string msg = "Failed to open input file" + files[i];
throw runtime_error(msg);
}
string c;
getline(fin, c);
int initialCount = stoi(c);
int count = 0;
vector<string> record;
string line, word;
while (getline(fin, line)) {
count++;
record.clear();
stringstream s(line);
// read every column data of a row and store it in a string variable, 'word'
while (getline(s, word, ',')) {
record.push_back(word);
}
student_list -> add(create_student(record));
}
if(count != initialCount){
string msg = "The count of students written at the beginning of file " + c + " does not match the real count of records in the file " + to_string(count);
throw out_of_range(msg);
}
fin.close();
}
}
// includes validation
Student *create_student(vector<string> data){
if(data[0].empty() || data[0] == " "){
string msg = "The first record in a table row (here: "+ data[0] + ")should be the name of a student, not an empty string.";
throw invalid_argument(msg);
}
int contractValueInd = data.size() - 1;
int grades_count = contractValueInd - 1;
vector<float> grades_list;
bool isOnContract;
for(int i = 1 ; i < grades_count + 1; i++){
grades_list.push_back(stof(data[i]));
if(errno == ERANGE){
string msg = "Error when transforming file content to float number at column " + to_string(i) + " at student " + data[0];
throw runtime_error(msg);
}
}
if(data[contractValueInd] == "FALSE" || data[contractValueInd] == "fasle"){
isOnContract = false;
}
else if (data[contractValueInd] == "TRUE" || data[contractValueInd] == "true")
{
isOnContract = true;
}
else{
throw invalid_argument("The last value in a file row shoudl be either TRUE or FALSE\nError occured at row with student " + data[0]);
}
Student *st1 = new Student(data[0], grades_list, isOnContract);
return st1;
}