uni

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

manufacturer.cpp (1529B)


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