Помогите понять что не так. Проблем 2. 1) не работает десериализация, а если точнее теряется значение перечислений. 2) выводит левый год. Вторая проблема под линуксом не проявляется. Код писался под виндой. Спасибо.#include <iostream>
#include <ctime>
#include <cstdlib>
#include <Windows.h>
#include <string>
#include <io.h>
#include <vector>
using namespace std;
class Event {
public:
enum Priority {
Low,
Normal,
High
};
enum Condition {
Completed,
Expired,
Future
};
Priority p;
Condition c;
tm date;
string name;
string type;
void serialization(char * &buff, int &i) {
buff = new char[name.size() + type.size() + 12];
i = 0;
for(int j = 0; j < name.size(); buff[i] = name[j], ++i, ++j) ;
buff[i] = '\0';
++i;
for(int j = 0; j < type.size(); buff[i] = type[j], ++i, ++j) ;
buff[i] = '\0';
++i;
auto time = mktime(&date);
int j = 0;
char * temp = (char *)&time;
for(; j < 8; buff[i] = *temp, ++temp, ++i, ++j) ;
buff[i] = (char)p;
++i;
buff[i] = c;
++i;
}
void deserialization(__int8 * buff) {
name = string(buff);
for(; *(buff - 1) != 0; ++buff);
type = string(buff);
for(; *(buff - 1) != 0; ++buff);
int i = 0;
time_t time;
auto temp = (__int8 *)&time;
for(; i < 8; *temp = *buff, ++temp, ++i, ++buff) ;
cout << endl << "**"<<*buff<< endl;
p = (Priority)*buff;
++buff;
cout << endl << "**"<<*buff<< endl;
c = (Condition)*buff;
}
void print() {
string a[] = { "Низкий ", "Нормальный", "Высокий " };
string b[] = { "Готово ", "Просрочено", "Грядущее " };
auto t1 = time(0);
auto t2 = mktime(&date);
bool future = t1 < t2;
if(c != Completed) c = future ? Future : Expired;
cout << a[p] << " " << b[c] << " " << date.tm_hour << ":" << date.tm_min << " "
<< date.tm_mday << "." << date.tm_mon + 1 << "." << (date.tm_year +1900);
}
};
vector<Event> events;
void print() {
for(auto e : events) e.print();
}
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
#if(0)
FILE * f;
Event e;
e.p = Event::Normal;
e.c = Event::Future;
e.name = "1";
e.type = "1111111111";
char * data = nullptr;
int size;
e.serialization(data, size);
if(!fopen_s(&f, "data", "w")) {
fwrite(data, size, 1, f);
fclose(f);
}
if(!fopen_s(&f, "data", "r")) {
size = _filelength(_fileno(f));
data = new char[size];
fread(data, size, 1, f);
fclose(f);
}
char * temp1 = data;
for(int i = 0; i < size; ++i, temp1++) {
printf("%x ", *temp1);
}
cout << endl;
e.deserialization(data);
cout << e.type << " " << e.name << endl << " " << (bool)(e.p == Event::Normal) << (bool)(e.c == Event::Future);
cout << endl << e.p << " " << e.c;
#endif
//print();
//auto str = "\x01\x02 1";
/*char str[] = {1, 2, 3};
auto ptr = (char *)&str;
Event::Priority p = (Event::Priority)*ptr;
++ptr;
Event::Condition c = (Event::Condition)*ptr;
cout << p << " " << c << endl;*/
events.push_back(Event());
events[0].c = Event::Future;
events[0].p = Event::Low;
events[0].date.tm_mday = 30;
events[0].date.tm_mon = 11;
events[0].date.tm_year = (2016 - 1900);
events[0].date.tm_hour = 4;
events[0].date.tm_min = 30;
print();/**/
system("pause");
return 0;
}