uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

datahandler.hpp (3762B)


      1 #ifndef DATA_HANDLER_HPP
      2 #define DATA_HANDLER_HPP
      3 
      4 #include <algorithm>
      5 #include <iostream>
      6 #include <fstream>
      7 #include <map>
      8 #include <vector>
      9 
     10 #include "course.hpp"
     11 #include "errlog.hpp"
     12 #include "student.hpp"
     13 #include "xstring.hpp"
     14 
     15 using equivalencies = std::map<lab::xstring, lab::xstring>;
     16 
     17 class DataHandler 
     18 {
     19     private:
     20         static constexpr const char *datapath = "res/grades.csv";
     21         static constexpr const char *reppath = "res/report.csv";
     22         std::map<lab::xstring, Course *> courses;
     23         std::map<lab::xstring, Student *> studs;
     24         std::map<Course *, float> grds;
     25         std::map<Student *, std::map<Course *, float>> data;
     26         std::vector<lab::xstring> errs;
     27         std::vector<lab::xstring> missing;
     28         equivalencies eqvs;
     29         ErrLog errlog;
     30         int misscount, errcount;
     31 
     32     public:
     33         DataHandler();
     34         ~DataHandler();
     35 
     36         template<typename T> void import_data(const char *fpath);
     37         void load_grades();
     38         void make_report() const;
     39         void summary() const;
     40         
     41     private:
     42         void analyze(
     43                 const lab::xstring& currid,
     44                 lab::xstring& id,
     45                 lab::xstring& code,
     46                 float grade);
     47         void miss(lab::xstring id, lab::xstring code, float grade);
     48         void diffr(lab::xstring id, lab::xstring code, float grade);
     49         const lab::xstring err_read (const char *fpath) const;
     50         const lab::xstring err_write(const char *fpath) const;
     51         template<typename T> void dealloc(std::map<lab::xstring, T *>& vec);
     52 };
     53 
     54 template<typename T> void
     55 DataHandler::import_data(const char *fpath)
     56 {
     57     std::ifstream f;
     58     f.exceptions(std::ifstream::badbit);
     59     try
     60     {
     61         f.open(fpath);
     62         if (f.is_open())
     63         {
     64             std::printf("Importing data from \'%s\'\n", fpath);
     65             lab::xstring skip;
     66             lab::getline(f, skip);
     67             while (f.good())
     68             {
     69                 if constexpr (std::is_same_v<T, Course>)
     70                 {
     71                     lab::xstring code, name, curriculum;
     72                     lab::getline(f, code, ';');
     73                     lab::getline(f, name, ';');
     74                     lab::getline(f, curriculum);
     75                     if (f.eof()) break;
     76                     courses.insert(std::make_pair(code, new Course(code,
     77                                     name,
     78                                     curriculum == "4" ? true : false)));
     79                 }
     80                 else if constexpr (std::is_same_v<T, Student>)
     81                 {
     82                     lab::xstring id, lname, fname;
     83                     lab::getline(f, id, ';');
     84                     lab::getline(f, lname, ';');
     85                     lab::getline(f, fname);
     86                     if (f.eof()) break;
     87                     studs.insert(std::make_pair(id, new Student(id, lname, fname)));
     88                 }
     89                 else if constexpr (std::is_same_v<T, equivalencies>)
     90                 {
     91                     lab::xstring oldcurr, newcurr;
     92                     lab::getline(f, oldcurr, ';');
     93                     lab::getline(f, newcurr);
     94                     if (f.eof()) break;
     95                     eqvs.insert(std::make_pair(oldcurr, newcurr));
     96                 }
     97             }
     98         }
     99         f.close();
    100     }
    101     catch (const std::ifstream::failure& e)
    102     {
    103         errlog.write(ErrLog::ErrType::RUNTIME_ERR, err_read(fpath)); 
    104         throw std::runtime_error(err_read(fpath).cstr());
    105     }
    106 }
    107 
    108 template<typename T> void
    109 DataHandler::dealloc(std::map<lab::xstring, T *>& vec)
    110 {
    111     if (!vec.empty())
    112     {
    113         for (auto&& v : vec)
    114             if (v.second != nullptr)
    115                 delete v.second;
    116         vec.clear();
    117     }
    118 }
    119 
    120 #endif /* DATA_HANDLER_HPP */