uni

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

appsystem.cpp (4127B)


      1 #include "appsystem.hpp"
      2 
      3 AppSystem::AppSystem() {errlog.fclear();}
      4 
      5 AppSystem::~AppSystem()
      6 {
      7     dealloc<App>(apps);
      8     dealloc<Manufacturer>(manfs);
      9 }
     10 
     11 /* 
     12  * Adds a new App object to the apps vector in case it doesn't already exist.
     13  * If it exists, an error is written to the error log.
     14  */
     15 AppSystem&
     16 AppSystem::operator+= (App *app)
     17 {
     18     if (!exists<App>(apps, app)) apps.push_back(app);
     19     else errlog.write("App: " + std::string(app->get_serialnum()) +
     20             ": exists already");
     21     return *this;
     22 }
     23 
     24 /* Same as above, but for a Manufacturer object */
     25 AppSystem&
     26 AppSystem::operator+= (Manufacturer *manf)
     27 {
     28     if (!exists<Manufacturer>(manfs, manf)) manfs.push_back(manf);
     29     else errlog.write("Manufacturer: " + std::string(manf->get_serialnum()) +
     30             ": exists already");
     31     return *this;
     32 }
     33 
     34 /* 
     35  * Parses a given file containing file extensions for an Office object
     36  * which are formatted as: .ext1|.ext2|.
     37  * Using STL's sstream we can extract each extension as a substring
     38  * by setting the delimeter to | and then push them back to
     39  * a vector which contains each extension.
     40  */
     41 const std::vector<std::string>
     42 AppSystem::parse_office_exts(std::ifstream& f)
     43 {
     44     std::vector<std::string> exts;
     45     std::string ext;
     46     std::getline(f, ext);
     47     std::istringstream iss(ext);    
     48     while (std::getline(iss, ext, '|')) exts.push_back(ext);
     49     return exts;
     50 }
     51 
     52 /* Writes file extensions to a file with the above format */
     53 void
     54 AppSystem::write_office_exts(const Office *of, std::ofstream& f)
     55 {
     56     std::vector<std::string> exts = of->get_exts();
     57     for (const auto& ext : exts)
     58         f << ext << '|';    
     59 }
     60 
     61 /* 
     62  * Searches through the apps vector to see if a given Manufacturer
     63  * object exists. If it does, it deletes every single app related
     64  * to that manufacturer.
     65  */
     66 void
     67 AppSystem::removebad(const Manufacturer *manf)
     68 {
     69     auto lambda = [&](App *app) -> bool
     70     {
     71         Manufacturer m = app->get_manf();
     72         if (!std::strcmp(m.get_serialnum(), manf->get_serialnum()))
     73             delete app;
     74         return !std::strcmp(m.get_serialnum(), manf->get_serialnum());
     75     };
     76     apps.erase(std::remove_if(apps.begin(), apps.end(), lambda), apps.end());
     77 }
     78 
     79 /* 
     80  * Same as above but this time we only pass the
     81  * manufacturer's serial number
     82  */
     83 void
     84 AppSystem::removebad(const char *manfsn)
     85 {
     86     auto lambda = [&](App *app) -> bool
     87     {
     88         Manufacturer m = app->get_manf();
     89         if (!std::strcmp(m.get_serialnum(), manfsn))
     90             delete app;
     91         return !std::strcmp(m.get_serialnum(), manfsn);
     92     };
     93     apps.erase(std::remove_if(apps.begin(), apps.end(), lambda), apps.end());
     94 }
     95 
     96 /* 
     97  * Returns a vector of Office objects whose prices are 0 (free).
     98  * dynamic_cast is required in order to only take Office objects
     99  * into account (the vector contains App objects).
    100  */
    101 const std::vector<Office *>
    102 AppSystem::get_freeapps() const
    103 {
    104     std::vector<Office *> fapps;
    105     for (const auto& app : apps)
    106         if (Office *o = dynamic_cast<Office *>(app))
    107             if (o->get_price() == 0)
    108                 fapps.push_back(o);
    109     return fapps;
    110 }
    111 
    112 /* 
    113  * Returns a vector with all the Game objects that have an
    114  * average rating bigger than 4. dynamic_cast is required
    115  * for the same reason as above.
    116  */
    117 const std::vector<Game *>
    118 AppSystem::get_goodgames() const
    119 {
    120     std::vector<Game *> ggames;
    121     for (const auto& app : apps)
    122     {
    123         if (Game *o = dynamic_cast<Game *>(app))
    124         {
    125             std::vector<Review *> revs = o->get_revs();
    126             if (!revs.empty())
    127             {
    128                 int sum = 0, count = 0; 
    129                 for (const auto& rev : revs)
    130                 {
    131                     sum += rev->get_stars();
    132                     count++;
    133                 }
    134                 if (sum / count > 4) ggames.push_back(o);
    135             }
    136         }
    137     }
    138     return ggames;
    139 }
    140 
    141 const std::string
    142 AppSystem::err_read(const char *fpath)
    143 {
    144     return "Error reading file \'" + std::string(fpath) + "\'.";
    145 }
    146 
    147 const std::string
    148 AppSystem::err_write(const char *fpath)
    149 {
    150     return "Error writing to file \'" + std::string(fpath) + "\'.";
    151 }