diff --git a/src/CSVClasses.cpp b/src/CSVClasses.cpp index 336740e..28daf1f 100644 --- a/src/CSVClasses.cpp +++ b/src/CSVClasses.cpp @@ -4,6 +4,7 @@ #include "CSVClasses.hpp" #include "Lesson.hpp" #include <algorithm> +#include <cstdlib> #include <iostream> #include <fstream> #include <iomanip> @@ -27,7 +28,14 @@ CSVClasses::CSVClasses(const std::string& csv) { std::stringstream s(contents); std::string line; this->entries = std::vector<Lesson>(); - getline(s, line, '\n'); + if (!getline(s, line, '\n')) { + std::cerr << "ERROR: CRITICAL: INVALID FILE CSVCLASSES" << std::endl; + std::exit(1); + } + if (line != "ClassCode,UcCode,Weekday,StartHour,Duration,Type") { + std::cerr << "ERROR: CRITICAL: INVALID FILE CSVCLASSES" << std::endl; + std::exit(1); + } line.clear(); while (std::getline(s, line, '\n')) { this->entries.push_back(Lesson(line)); diff --git a/src/CSVClassesPerUC.cpp b/src/CSVClassesPerUC.cpp index 1fefe5e..b742fde 100644 --- a/src/CSVClassesPerUC.cpp +++ b/src/CSVClassesPerUC.cpp @@ -3,73 +3,81 @@ */ #include "CSVClassesPerUC.hpp" #include "ClassesPerUC.hpp" -#include <sstream> -#include <string> #include <algorithm> #include <fstream> - +#include <sstream> +#include <string> /** - * @brief This constructor receives a string containing all the lines of a csv file and creates the AppClassPerUC from it. + * @brief This constructor receives a string containing all the lines of a csv + * file and creates the AppClassPerUC from it. * @details The cap parameter is the capacity of each class (30 by default). * Theoretical complexity: O(n), where n is the number of lines in the csv file. * @param csv * @param cap */ -CSVClassPerUC::CSVClassPerUC(const std::string& csv, uint8_t cap) { - this->cap = cap; +CSVClassPerUC::CSVClassPerUC(const std::string &csv, uint8_t cap) { + this->cap = cap; - // CSV file into memory - std::ifstream file = std::ifstream(csv); - std::string contents; - std::ostringstream sstr; - sstr << file.rdbuf(); - contents = sstr.str(); + // CSV file into memory + std::ifstream file = std::ifstream(csv); + std::string contents; + std::ostringstream sstr; + sstr << file.rdbuf(); + contents = sstr.str(); - // Parse string - std::stringstream s(contents); - std::string line; - this->entries = std::vector<ClassPerUC>(); - getline(s, line, '\n'); - line.clear(); - while (std::getline(s, line, '\n')) { - this->entries.push_back(ClassPerUC(line)); - } -} + // Parse string + std::stringstream s(contents); + std::string line; + this->entries = std::vector<ClassPerUC>(); + if (!getline(s, line, '\n')) { + std::cerr << "ERROR: CRITICAL: INVALID FILE CSVCLASSESPERUC" << std::endl; + std::exit(1); + } + if (line != "UcCode,ClassCode") { + std::cerr << "ERROR: CRITICAL: INVALID FILE CSVCLASSESPERUC" << std::endl; + std::exit(1); + } -CSVClassPerUC::CSVClassPerUC() { - this->cap = 30; + line.clear(); + while (std::getline(s, line, '\n')) { + this->entries.push_back(ClassPerUC(line)); + } } +CSVClassPerUC::CSVClassPerUC() { this->cap = 30; } + /** * @brief This method prints the csv file. */ void CSVClassPerUC::display() { - std::cout << "UcCode" << ',' << "ClassCode" << '\n'; - for (auto e : this->entries) { - e.display(); - } + std::cout << "UcCode" << ',' << "ClassCode" << '\n'; + for (auto e : this->entries) { + e.display(); + } } /** * @brief Sort the entries vector. - * @details Order (from most important to least important): uc_code > class_code. - * Theoretical complexity: O(n log n), where n is the number of entries in the vector. + * @details Order (from most important to least important): uc_code > + * class_code. Theoretical complexity: O(n log n), where n is the number of + * entries in the vector. */ void CSVClassPerUC::sort() { - std::sort(this->entries.begin(), this->entries.end(), [](const ClassPerUC &first, const ClassPerUC &second) { - return first.get_id() < second.get_id(); - }); + std::sort(this->entries.begin(), this->entries.end(), + [](const ClassPerUC &first, const ClassPerUC &second) { + return first.get_id() < second.get_id(); + }); } /** * @brief Getter for the vector of ClassPerUC. * @return Pointer to entries. */ -std::vector<ClassPerUC> *CSVClassPerUC::get_classes() {return &this->entries;} +std::vector<ClassPerUC> *CSVClassPerUC::get_classes() { return &this->entries; } /** * @brief Getter for the capacity of every class. * @return cap */ -uint8_t CSVClassPerUC::get_cap() const {return cap;} +uint8_t CSVClassPerUC::get_cap() const { return cap; } diff --git a/src/CSVStudentsClasses.cpp b/src/CSVStudentsClasses.cpp index 86994f9..32f6b56 100644 --- a/src/CSVStudentsClasses.cpp +++ b/src/CSVStudentsClasses.cpp @@ -29,7 +29,14 @@ CSVStudentsClasses::CSVStudentsClasses(const std::string &csv) { std::stringstream s(contents); std::string line; this->entries = std::vector<StudentsClasses>(); - getline(s, line, '\n'); + if (!getline(s, line, '\n')) { + std::cerr << "ERROR: CRITICAL: INVALID FILE CSVSTUDENTCLASSES" << std::endl; + std::exit(1); + } + if (line != "StudentCode,StudentName,UcCode,ClassCode") { + std::cerr << "ERROR: CRITICAL: INVALID FILE CSVSTUDENTCLASSES" << std::endl; + std::exit(1); + } line.clear(); while (std::getline(s, line, '\n')) { this->entries.push_back(StudentsClasses(line));