Separate data.h into data.{h,cpp}

This commit is contained in:
realaltffour 2020-04-01 13:26:46 +03:00
parent 8d56bc895e
commit 9bf368ee4e
No known key found for this signature in database
GPG Key ID: 05B35E2E8F56C5A6
2 changed files with 75 additions and 75 deletions

71
src/data.cpp Normal file
View File

@ -0,0 +1,71 @@
#include "data.h"
void to_json(json& j, const Date& date) {
j = json{
{"day", date.day},
{"month", date.day},
{"year", date.year}
};
}
void from_json(const json& j, Date& date) {
j.at("day").get_to(date.day);
j.at("month").get_to(date.month);
j.at("year").get_to(date.year);
}
void to_json(json& j, const Project& proj) {
j = json{
{"name", proj.name},
{"desc", proj.desc},
{"uuid", proj.uuid},
{"pri", proj.pri},
{"creationDate", proj.creationDate},
{"doneDate", proj.doneDate},
{"isDone", proj.isDone}
};
}
void from_json(const json& j, Project& proj) {
j.at("name").get_to(proj.name);
j.at("desc").get_to(proj.desc);
j.at("uuid").get_to(proj.uuid);
j.at("pri").get_to(proj.pri);
j.at("creationDate").get_to(proj.creationDate);
j.at("doneDate").get_to(proj.doneDate);
j.at("isDone").get_to(proj.isDone);
}
void to_json(json& j, const DB& db) {
j = json{
{"projects", db.projects},
{"creationDate", db.creationDate},
{"lastAccessTime", db.lastAccessTime}
};
}
void from_json(const json& j, DB& db) {
j.at("projects").get_to(db.projects);
j.at("creationDate").get_to(db.creationDate);
j.at("lastAccessTime").get_to(db.lastAccessTime);
}
void writeDB(DB db, const std::string &dest) {
try {
std::ofstream f(dest);
json j = db;
f << j;
} catch (std::exception &ex) {
std::cout << "Failed writing database to: " << dest << std::endl;
std::cout << "Error: " << ex.what() << std::endl;
}
}
void loadDB(DB &db, const std::string &src) {
try {
std::ifstream f(src);
json j;
f >> j;
db = j;
} catch (const std::exception& ex) {
std::cout << "Failed loading database from: " << src << std::endl;
std::cout << "Error: " << ex.what() << std::endl;
}
}

View File

@ -1,7 +1,8 @@
#ifndef DATA_H
#define DATA_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
@ -72,82 +73,10 @@ struct DB {
Date lastAccessTime;
};
////////////// Helping functions for JSON /////////////////////////
void to_json(json& j, const Date& date) {
j = json{
{"day", date.day},
{"month", date.day},
{"year", date.year}
};
}
void from_json(const json& j, Date& date) {
j.at("day").get_to(date.day);
j.at("month").get_to(date.month);
j.at("year").get_to(date.year);
}
void to_json(json& j, const Project& proj) {
j = json{
{"name", proj.name},
{"desc", proj.desc},
{"uuid", proj.uuid},
{"pri", proj.pri},
{"creationDate", proj.creationDate},
{"doneDate", proj.doneDate},
{"isDone", proj.isDone}
};
}
void from_json(const json& j, Project& proj) {
j.at("name").get_to(proj.name);
j.at("desc").get_to(proj.desc);
j.at("uuid").get_to(proj.uuid);
j.at("pri").get_to(proj.pri);
j.at("creationDate").get_to(proj.creationDate);
j.at("doneDate").get_to(proj.doneDate);
j.at("isDone").get_to(proj.isDone);
}
void to_json(json& j, const DB& db) {
j = json{
{"projects", db.projects},
{"creationDate", db.creationDate},
{"lastAccessTime", db.lastAccessTime}
};
}
void from_json(const json& j, DB& db) {
j.at("projects").get_to(db.projects);
j.at("creationDate").get_to(db.creationDate);
j.at("lastAccessTime").get_to(db.lastAccessTime);
}
////////////// End Helping functions for JSON /////////////////////
////////////// Utilities for the Database structure ////////////////
void writeDB(DB db, const std::string &dest) {
try {
std::ofstream f(dest);
json j = db;
f << j;
} catch (...) {
std::cout << "Failed writing database to: " << dest << std::endl;
}
}
void loadDB(DB &db, const std::string &src) {
try {
std::ifstream f(src);
json j;
f >> j;
db = j;
} catch (const std::exception& ex) {
std::cout << "Failed loading database from: " << src << std::endl;
std::cout << "Error: " << ex.what() << std::endl;
}
}
void writeDB(DB db, const std::string &dest);
void loadDB(DB &db, const std::string &src);
////////////// End Utilities for the Database structure ////////////