commit 0254972c4a3fd9e4cb35488f6035d2d7fbabb3db parent a12cb82f80d726096a34367afb212921f7568007 Author: Christos Margiolis <christos@margiolis.net> Date: Mon, 11 May 2020 18:55:12 +0300 minor updates Diffstat:
20 files changed, 173 insertions(+), 147 deletions(-)
diff --git a/assignment-2.4-inheritance/bin/inheritance b/assignment-2.4-inheritance/bin/inheritance Binary files differ. diff --git a/assignment-2.4-inheritance/obj/game.o b/assignment-2.4-inheritance/obj/game.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/office.o b/assignment-2.4-inheritance/obj/office.o Binary files differ. diff --git a/assignment-2.4-inheritance/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -217,5 +217,5 @@ AppSystem::err_read(const char *fpath) const std::string AppSystem::err_write(const char *fpath) { - return "Error writing to file (" + std::string(fpath) + ")."; + return "Error writing to file \'" + std::string(fpath) + "\'."; } diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -55,8 +55,8 @@ class AppSystem template<typename T> void dealloc(std::vector<T *>& vec); }; -template<typename T> -bool AppSystem::parse(std::ifstream& f) +template<typename T> bool +AppSystem::parse(std::ifstream& f) { try { @@ -133,8 +133,8 @@ bool AppSystem::parse(std::ifstream& f) return true; } -template<typename T> -bool AppSystem::import_data(const char *fpath) +template<typename T> bool +AppSystem::import_data(const char *fpath) { std::ifstream f; f.exceptions(std::ifstream::badbit); @@ -187,8 +187,8 @@ bool AppSystem::import_data(const char *fpath) return true; } -template<typename T> -bool AppSystem::export_data(const char *fpath) +template<typename T> bool +AppSystem::export_data(const char *fpath) { std::ofstream f; f.exceptions(std::ofstream::failbit | std::ofstream::badbit); @@ -256,8 +256,8 @@ bool AppSystem::export_data(const char *fpath) return true; } -template<typename T> -void AppSystem::dealloc(std::vector<T *>& vec) +template<typename T> void +AppSystem::dealloc(std::vector<T *>& vec) { for (auto& v : vec) if (v != nullptr) diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/app.o b/assignment-2.5-spreadsheets/obj/app.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/grade.o b/assignment-2.5-spreadsheets/obj/grade.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/main.o b/assignment-2.5-spreadsheets/obj/main.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/xstring.o b/assignment-2.5-spreadsheets/obj/xstring.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/res/matchings.csv b/assignment-2.5-spreadsheets/res/equivalencies.csv diff --git a/assignment-2.5-spreadsheets/src/app.cpp b/assignment-2.5-spreadsheets/src/app.cpp @@ -5,37 +5,17 @@ App::App() {} App::~App() { dealloc<Course>(courses); - dealloc<Grades>(grades); + dealloc<Grade>(grades); dealloc<Student>(studs); } -bool -App::import_matchings(const char *fpath) -{ - std::ifstream f; - f.open(fpath); - // exceptions - lab::xstring skip; - lab::getline(f, skip); - while (f.good()) - { - lab::xstring newcurr, oldcurr; - lab::getline(f, newcurr, ';'); - lab::getline(f, oldcurr); - if (f.eof()) break; - matchings.emplace(newcurr, oldcurr); - } - f.close(); - return true; -} - const std::vector<Course *>& App::get_courses() const { return courses; } -const std::vector<Grades *>& +const std::vector<Grade *>& App::get_grades() const { return grades; @@ -47,8 +27,38 @@ App::get_studs() const return studs; } -const std::map<lab::xstring, lab::xstring>& -App::get_matchings() const +const equivalencies& +App::get_eqvs() const +{ + return eqvs; +} + +bool +App::valid_path(const char *fpath) +{ + return (strstr(fpath, ".csv") != nullptr); +} + +const lab::xstring +App::err_csv(const char *fpath) +{ + lab::xstring err = "Error. File must be of format \'.csv\'. ()."; + err.insert(fpath, 39); + return err; +} + +const lab::xstring +App::err_read(const char *fpath) +{ + lab::xstring err = "Error reading file \'\'."; + err.insert(fpath, 20); + return err; +} + +const lab::xstring +App::err_write(const char *fpath) { - return matchings; + lab::xstring err = "Error writing to file \'\'."; + err.insert(fpath, 23); + return err; } diff --git a/assignment-2.5-spreadsheets/src/app.h b/assignment-2.5-spreadsheets/src/app.h @@ -7,84 +7,110 @@ #include <vector> #include "course.h" -#include "grades.h" +#include "grade.h" #include "student.h" #include "xstring.h" +typedef std::map<lab::xstring, lab::xstring> equivalencies; + class App { private: std::vector<Course *> courses; - std::vector<Grades *> grades; + std::vector<Grade *> grades; std::vector<Student *> studs; - std::map<lab::xstring, lab::xstring> matchings; + equivalencies eqvs; public: App(); ~App(); template<typename T> bool import_data(const char *fpath); - bool import_matchings(const char *fpath); const std::vector<Course *>& get_courses() const; - const std::vector<Grades *>& get_grades() const; + const std::vector<Grade *>& get_grades() const; const std::vector<Student *>& get_studs() const; - const std::map<lab::xstring, lab::xstring>& get_matchings() const; + const equivalencies& get_eqvs() const; private: + bool valid_path(const char *fpath); + const lab::xstring err_csv(const char *fpath); + const lab::xstring err_read(const char *fpath); + const lab::xstring err_write(const char *fpath); template<typename T> void dealloc(std::vector<T *>& vec); }; -template<typename T> -bool App::import_data(const char *fpath) +template<typename T> bool +App::import_data(const char *fpath) { std::ifstream f; f.exceptions(std::ifstream::badbit); - f.open(fpath); - lab::xstring skip; - lab::getline(f, skip); - while (f.good()) + try { - if (std::is_same<T, Course>::value) - { - lab::xstring code, name; - lab::getline(f, code, ';'); - lab::getline(f, name); - if (f.eof()) break; - courses.push_back(new Course(code, name)); - } - else if (std::is_same<T, Grades>::value) - { - lab::xstring AM, code, grade; - lab::getline(f, AM, ';'); - lab::getline(f, code, ';'); - lab::getline(f, grade); - if (f.eof()) break; - grades.push_back(new Grades(AM, code, std::atof(grade.cstr()))); - - } - else if (std::is_same<T, Student>::value) + if (!valid_path(fpath)) + throw std::runtime_error(err_csv(fpath).cstr()); + f.open(fpath); + if (f.is_open()) { - lab::xstring AM, lname, fname; - lab::getline(f, AM, ';'); - lab::getline(f, lname, ';'); - lab::getline(f, fname); - if (f.eof()) break; - studs.push_back(new Student(AM, lname, fname)); + lab::xstring skip; + lab::getline(f, skip); + while (f.good()) + { + if (std::is_same<T, Course>::value) + { + lab::xstring code, name; + lab::getline(f, code, ';'); + lab::getline(f, name); + if (f.eof()) break; + courses.push_back(new Course(code, name)); + } + else if (std::is_same<T, Grade>::value) + { + lab::xstring AM, code, grade; + lab::getline(f, AM, ';'); + lab::getline(f, code, ';'); + lab::getline(f, grade); + if (f.eof()) break; + grades.push_back(new Grade(AM, code, + std::atof(grade.cstr()))); + } + else if (std::is_same<T, Student>::value) + { + lab::xstring AM, lname, fname; + lab::getline(f, AM, ';'); + lab::getline(f, lname, ';'); + lab::getline(f, fname); + if (f.eof()) break; + studs.push_back(new Student(AM, lname, fname)); + } + else if (std::is_same<T, equivalencies>::value) + { + lab::xstring newcurr, oldcurr; + lab::getline(f, newcurr, ';'); + lab::getline(f, oldcurr); + if (f.eof()) break; + eqvs.emplace(newcurr, oldcurr); + } + } } - - } - f.close(); + f.close(); + } + catch (const std::ifstream::failure& e) + { + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; + return false; + } return true; } -template<typename T> -void App::dealloc(std::vector<T *>& vec) +template<typename T> void +App::dealloc(std::vector<T *>& vec) { for (auto& v : vec) if (v != nullptr) delete v; - vec.clear(); + if (!vec.empty()) + vec.clear(); } #endif /* APP_H */ diff --git a/assignment-2.5-spreadsheets/src/grade.cpp b/assignment-2.5-spreadsheets/src/grade.cpp @@ -0,0 +1,4 @@ +#include "grade.h" + +Grade::Grade(const lab::xstring& AM, const lab::xstring& code, float grade) + :AM(AM), code(code), grade(grade) {} diff --git a/assignment-2.5-spreadsheets/src/grade.h b/assignment-2.5-spreadsheets/src/grade.h @@ -0,0 +1,17 @@ +#ifndef GRADE_H +#define GRADE_H + +#include "xstring.h" + +class Grade +{ + private: + lab::xstring AM; + lab::xstring code; + float grade; + + public: + Grade(const lab::xstring& AM, const lab::xstring& code, float grade); +}; + +#endif /* GRADE_H */ diff --git a/assignment-2.5-spreadsheets/src/grades.cpp b/assignment-2.5-spreadsheets/src/grades.cpp @@ -1,4 +0,0 @@ -#include "grades.h" - -Grades::Grades(const lab::xstring& AM, const lab::xstring& code, float grade) - :AM(AM), code(code), grade(grade) {} diff --git a/assignment-2.5-spreadsheets/src/grades.h b/assignment-2.5-spreadsheets/src/grades.h @@ -1,17 +0,0 @@ -#ifndef GRADES_H -#define GRADES_H - -#include "xstring.h" - -class Grades -{ - private: - lab::xstring AM; - lab::xstring code; - float grade; - - public: - Grades(const lab::xstring& AM, const lab::xstring& code, float grade); -}; - -#endif diff --git a/assignment-2.5-spreadsheets/src/main.cpp b/assignment-2.5-spreadsheets/src/main.cpp @@ -7,16 +7,15 @@ main(int argc, char **argv) { App app; if (!app.import_data<Course>("res/courses.csv")) return -1; - if (!app.import_data<Grades>("res/grades.csv")) return -1; + if (!app.import_data<Grade>("res/grades.csv")) return -1; if (!app.import_data<Student>("res/students.csv")) return -1; - if (!app.import_matchings("res/matchings.csv")) return -1; - //std::cout << app << std::endl; + if (!app.import_data<equivalencies>("res/equivalencies.csv")) return -1; - std::map<lab::xstring, lab::xstring> a = app.get_matchings(); - std::map<lab::xstring, lab::xstring>::const_iterator it; + equivalencies a = app.get_eqvs(); + equivalencies::const_iterator it; for (it = a.begin(); it != a.end(); it++) std::cout << it->first << " " << it->second << std::endl; - + return 0; } diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -2,9 +2,10 @@ namespace lab { xstring::xstring() - :str(new char[1]), len(0) { + str = new char[0]; str[0] = '\0'; + len = 0; } xstring::xstring(const char *s) @@ -17,11 +18,7 @@ xstring::xstring(const xstring& s) str = conv(s.str); len = s.len; } - else - { - len = 0; - str = new char[1]; - } + else clear(); } xstring::~xstring() @@ -39,11 +36,7 @@ xstring::operator= (const xstring& s) str = conv(s.str); len = s.len; } - else - { - str = new char[1]; - len = 0; - } + else clear(); return *this; } @@ -193,7 +186,26 @@ xstring::append(const xstring& s) } xstring& -xstring::append(const xstring& s, std::size_t i) +xstring::append(const char *s) +{ + if (!strempty(s)) + { + resize(strlen(s)); + strcat(str, s); + } + len = length(); + return *this; +} + +xstring& +xstring::append(char c) +{ + push_back(c); + return *this; +} + +xstring& +xstring::insert(const xstring& s, std::size_t i) { if (!s.empty() && i < len) { @@ -214,19 +226,7 @@ xstring::append(const xstring& s, std::size_t i) } xstring& -xstring::append(const char *s) -{ - if (!strempty(s)) - { - resize(strlen(s)); - strcat(str, s); - } - len = length(); - return *this; -} - -xstring& -xstring::append(const char *s, std::size_t i) +xstring::insert(const char *s, std::size_t i) { if (!strempty(s) && i < len) { @@ -246,13 +246,6 @@ xstring::append(const char *s, std::size_t i) return *this; } -xstring& -xstring::append(char c) -{ - push_back(c); - return *this; -} - void xstring::push_back(char c) { @@ -277,8 +270,7 @@ xstring::pop_back() else if (len - 1 == 0) { delete[] str; - str = new char[1]; - str[0] = '\0'; + clear(); } else return; } @@ -338,7 +330,6 @@ xstring::clear() if (!this->empty()) delete[] str; str = new char[1]; str[0] = '\0'; - //*this = ""; len = 0; } diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -43,10 +43,10 @@ class xstring friend std::istream& operator>> (std::istream& stream, const xstring& s); xstring& append(const xstring& s); - xstring& append(const xstring& s, std::size_t i); xstring& append(const char *s); - xstring& append(const char *s, std::size_t i); xstring& append(char c); + xstring& insert(const xstring& s, std::size_t i); + xstring& insert(const char *s, std::size_t i); void push_back(char c); void pop_back(); @@ -71,24 +71,24 @@ template<typename T> xstring to_xstr(T val); template<typename T> xstring to_xstr(const char *fs, T val); template<typename T> const char *getformat(); -template<typename T> -xstring to_xstr(T val) +template<typename T> xstring +to_xstr(T val) { char buf[25]; snprintf(buf, sizeof(buf), getformat<T>(), val); return xstring(buf); } -template<typename T> -xstring to_xstr(const char *fs, T val) +template<typename T> xstring +to_xstr(const char *fs, T val) { char buf[25]; snprintf(buf, sizeof(buf), fs, val); return xstring(buf); } -template<typename T> -const char *getformat() +template<typename T> const char * +getformat() { if (std::is_same<T, short>::value) return "%hi"; if (std::is_same<T, int>::value) return "%d";