Add writeDB() & loadDB() functions

This commit is contained in:
realaltffour 2020-04-01 13:01:55 +03:00
parent 5fe5104298
commit 8d56bc895e
No known key found for this signature in database
GPG Key ID: 05B35E2E8F56C5A6
2 changed files with 112 additions and 12 deletions

View File

@ -1,14 +1,15 @@
#ifndef DATA_H
#define DATA_H
#include <string>
#include <vector>
#include <ctime>
#include <map>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <fstream>
#include "json.hpp"
using nlohmann::json;
#define LOC_DB = ".db"
#define LOC_EVENTLOG = ".log"
@ -16,18 +17,25 @@
/////////////// DATA CONTAINING STRUCTURES ///////////////////
struct Date {
int day = 0;
int month = 0;
int year = 0;
};
struct Project {
std::string name = "";
std::string desc = "";
boost::uuids::uuid uuid;
int preferredPrior = 0;
time_t creationDate;
std::string uuid;
int pri = 0;
Date creationDate;
Date doneDate;
bool isDone = false;
};
struct Skid {
std::vector<Project*> projects = {};
time_t creationDate;
Date creationDate;
};
/////////////// END OF DATA CONTAINING STRUCTURES ////////////
@ -55,12 +63,92 @@ struct EventLog {
/////////////// EVENT END OF HISTORY SYSTEM STRUCTURES /////////////
// Main Database that represents the skidjular directory.
struct DB {
std::map<std::string, Project> projects;
EventLog log;
time_t creationDate;
time_t lastAccessTime;
Date creationDate;
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;
}
}
////////////// End Utilities for the Database structure ////////////
#endif

View File

@ -33,7 +33,19 @@ int main(int argc, const char* argv[]) {
.positional(general_positional)
.run(), vm);
notify(vm);
std::cout << "Hello world";
DB db, db2;
Project p1,p2;
p1.name = "hello";
p2.name = "world";
db.projects["hello"] = p1;
db.projects["world"] = p2;
writeDB(db, "hello.db");
loadDB(db2, "hello.db");
std::cout << "\n Loading projects: ";
std::cout << db2.projects["hello"].name;
if (vm.count("help")) {
std::cout << general << "\n";
exit(0);
@ -65,7 +77,7 @@ int main(int argc, const char* argv[]) {
exit(0);
}
std::cout << "Hello world";
}
catch (const error &ex) {
cerr << ex.what() << '\n';