uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

app.cpp (1635B)


      1 #include "app.hpp"
      2 
      3 App::App()
      4     :serialnum(nullptr), name(""), os(""), manf(nullptr), price(0) {}
      5 
      6 App::App(const char *serialnum, const std::string& name,
      7     const std::string& os, Manufacturer *manf, const int price)
      8     :serialnum(convsn(serialnum)), name(name), os(os), manf(manf), price(price)
      9 {
     10     if (!std::strcmp(serialnum, ""))
     11         errlog.write("Missing app serial number");
     12     if (name.empty())
     13         errlog.write("App: " + std::string(serialnum) + ": Missing app name");
     14     if (os.empty())
     15         errlog.write("App: " + std::string(serialnum) + ": Missing OS version");
     16     if (manf == nullptr)
     17         errlog.write("App: " + std::string(serialnum) + ": Missing Manufacturer");
     18     if (price < 0)
     19         errlog.write("App: " + std::string(serialnum) + ": Negative price");
     20 }
     21 
     22 App::App(const App& app)
     23     :serialnum(convsn(app.serialnum)), name(app.name), os(app.os), manf(app.manf),
     24     price(app.price) {}
     25 
     26 App::~App()
     27 {
     28     if (serialnum != nullptr) delete[] serialnum;
     29     if (!reviews.empty())
     30     {
     31         for (auto&& rev : reviews)
     32             if (rev != nullptr)
     33                 delete rev;
     34         reviews.clear();
     35     }
     36 }
     37 
     38 /* 
     39  * Makes a copy of a const char array and returns
     40  * a temporary array which is meant to be stored in
     41  * a member variable.
     42  */
     43 char *
     44 App::convsn(const char *serialnum)
     45 {
     46     int len = std::strlen(serialnum);
     47     char *tmp = new char[len + 1];
     48     std::copy(serialnum, serialnum + len + 1, tmp);
     49     return tmp; 
     50 }
     51 
     52 void
     53 App::set_serialnum(const char *serialnum)
     54 {
     55     if (this->serialnum != nullptr) delete[] this->serialnum;
     56     this->serialnum = convsn(serialnum);
     57 }