Files
tft/person.cpp
T
2022-08-27 16:35:40 +03:00

113 lines
2.6 KiB
C++

#include <iostream>
#include <assert.h>
#include"person.h"
#include "help.h"
using std::cout;
BirthTime::BirthTime(U16 Year, U8 Month, U8 Day, U8 Hour, U8 Minute)
: year(Year)
, month(Month)
, day(Day)
, hour(Hour)
, minute(Minute)
{
assert(month <= 12 && day <= 31 && hour <= 23 && minute <= 59 && "Invalid BirthTime data");
}
void BirthTime::print() const
{
cout << "Birth: ";
if (day != UNKNOWN)
cout << (I32)day;
else
cout << "??";
cout << '.';
if (month != UNKNOWN)
cout << (I32)month;
else
cout << "??";
cout << '.';
if (year != UNKNOWN)
cout << (I32)year;
else
cout << "????";
cout << '\n';
}
Person::Person(const char* Name, BirthTime Birth, bool IsMale)
: name(Name)
, numRel(0)
, birth(Birth)
, isMale(IsMale)
{
}
/*Person& Person::operator=(const Person &other)
{
if (&other!=this)
{
rename(other.name);
numRel = other.numRel;
isMale = other.isMale;
day = other.day;
month = other.month;
year = other.year;
}
return *this;
}*/
bool Person::operator==(const Person &other)const
{
return name == other.name && isMale == other.isMale; //TODO && day == other.day || !day || !other.day) && (month == other.month || !month || !other.month) && (year == other.year || !year || !other.year);
}
void Person::save(std::ofstream &file)const
{
//
char nameLen = name.size();
file.write(reinterpret_cast<char*>(&nameLen), sizeof(nameLen));
file.write(name.data(), nameLen * sizeof(name[0]));
file.write(reinterpret_cast<const char*>(&numRel), sizeof(numRel));
file.write(reinterpret_cast<const char*>(&isMale), sizeof(isMale));
//file.write(reinterpret_cast<const char*>(&day), sizeof(day)); TODO
//file.write(reinterpret_cast<const char*>(&month), sizeof(month));
//file.write(reinterpret_cast<const char*>(&year), sizeof(year));
}
void Person::load(std::ifstream &file)
{
char nameLen;
file.read((char*)&nameLen, sizeof(nameLen)); //Check len
C8 nameBuf[128];
file.read(nameBuf, nameLen * sizeof(*nameBuf));
nameBuf[nameLen] = '\0';
name = nameBuf;
file.read(reinterpret_cast<char*>(&numRel), sizeof(numRel));
file.read(reinterpret_cast<char*>(&isMale), sizeof(isMale));
//file.read(reinterpret_cast<char*>(&day), sizeof(day)); TODO
//file.read(reinterpret_cast<char*>(&month), sizeof(month));
//file.read(reinterpret_cast<char*>(&year), sizeof(year));
}
void Person::rename(const char* newName)
{
name = newName;
}
void Person::print()const
{
std::cout << name << '\n';
birth.print();
std::cout << (isMale ? "Male\n\n" : "Female\n\n");
}