Add get_today()

- Returns Date struct as the value of today's date.

- Use get_today() in init and proj modules.
This commit is contained in:
realaltffour 2020-04-15 22:14:00 +03:00
parent 63279e1e45
commit 11a1b58f6f
No known key found for this signature in database
GPG Key ID: 7115CD2AC9A76A56
4 changed files with 80 additions and 27 deletions

View File

@ -35,12 +35,14 @@ void to_json(json& j, const Skid& skid) {
j = json{
{"projects", projects},
{"creationDate", skid.creationDate},
{"date", skid.date},
};
}
void from_json(const json& j, Skid& skid) {
skid.projects = new Project();
j.at("projects").get_to(*skid.projects);
j.at("creationDate").get_to(skid.creationDate);
j.at("date").get_to(skid.date);
}
void to_json(json& j, const DB& db) {
std::map<std::string, Project> projects;
@ -49,13 +51,15 @@ void to_json(json& j, const DB& db) {
};
j = json{{"projects", projects},
{"creationDate", db.creationDate},
{"lastAccessTime", db.lastAccessTime}};
{"lastAccessTime", db.lastAccessTime},
{"lastSkidID", db.lastSkidID}};
}
void from_json(const json& j, DB& db) {
db.projects = new std::map<std::string, Project>();
j.at("projects").get_to(*db.projects);
j.at("creationDate").get_to(db.creationDate);
j.at("lastAccessTime").get_to(db.lastAccessTime);
j.at("lastSkidID").get_to(db.lastSkidID);
}
void writeDB(DB db, const std::string& dest) {
@ -69,9 +73,6 @@ void writeDB(DB db, const std::string& dest) {
std::cout << "Error: " << ex.what() << std::endl;
}
}
bool is_empty(std::ifstream& pFile) {
return pFile.peek() == std::ifstream::traits_type::eof();
}
void loadDB(DB& db, const std::string& src) {
try {
@ -90,3 +91,58 @@ void loadDB(DB& db, const std::string& src) {
std::cout << "Error: " << ex.what() << std::endl;
}
}
void writeSkid(Skid skid, int skidNum) {
std::string dest = LOC_SKIDID + std::to_string(skidNum);
try {
logEntry("Writing Skid...", EntryType::Modification);
std::ofstream f(dest);
json j = skid;
f << j;
} catch (std::exception& ex) {
std::cout << "Failed writing skid to: " << dest << std::endl;
std::cout << "Error: " << ex.what() << std::endl;
}
}
void loadSkid(Skid& skid, int skidNum) {
std::string dest = LOC_SKIDID + std::to_string(skidNum);
try {
logEntry("Loading Skid...", EntryType::Access);
std::ifstream f(dest);
json j;
if (is_empty(f)) {
std::cout << "Skid is not present" << std::endl;
exit(1);
}
f >> j;
skid = j;
} catch (const std::exception& ex) {
std::cout << "Failed loading skid from: " << dest << std::endl;
std::cout << "Error: " << ex.what() << std::endl;
}
}
Date get_today() {
Date res;
time_t t = time(0);
struct tm* currentTime = localtime(&t);
res.day = currentTime->tm_mday;
res.month = currentTime->tm_mon;
res.year = currentTime->tm_year + 1900;
delete currentTime;
return res;
}
// Date addDate(Date initial, int number, int* slot) {}
bool is_empty(std::ifstream& pFile) {
return pFile.peek() == std::ifstream::traits_type::eof();
}
bool is_path_exist(const std::string& s) {
struct stat buffer;
return (stat(s.c_str(), &buffer) == 0);
}

View File

@ -1,5 +1,7 @@
#ifndef DATA_H
#define DATA_H
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include <list>
@ -14,7 +16,7 @@ using nlohmann::json;
#define LOC_LOG ".log"
#define LOC_SKIDID "skid" // used as LOC_SKIDID+SKIDNUM
/////////////// DATA CONTAINING STRUCTURES ///////////////////
////// DATA STRUCTURES
struct Date {
int day = 0;
@ -38,11 +40,10 @@ struct Project {
struct Skid {
Project* projects = nullptr;
Date creationDate;
Date date;
};
/////////////// END OF DATA CONTAINING STRUCTURES ////////////
/////////////// EVENT HISTORY SYSTEM STRUCTURES ////////////////////
//////// EVENT HISTORY
enum EntryType { Access = 0, Creation = 1, Modification = 2, None = -1 };
@ -57,23 +58,30 @@ struct Log {
std::list<Entry>* entries = nullptr;
};
/////////////// EVENT END OF HISTORY SYSTEM STRUCTURES /////////////
// Main Database that represents the skidjular directory.
struct DB {
std::map<std::string, Project>* projects;
std::vector<Skid> skids;
Log log;
int lastSkidID = 0;
Date creationDate;
Date lastAccessTime;
;
};
////////////// Utilities for the Database structure ////////////////
//////// UTILITIES
void writeDB(DB db, const std::string& dest);
void loadDB(DB& db, const std::string& src);
////////////// End Utilities for the Database structure ////////////
void writeSkid(Skid skid, int skidNum);
void loadSkid(Skid& skid, int skidNum);
Date get_today();
// Date addDate(Date initial, int number, int* slot = nullptr);
bool is_empty(std::ifstream& pFile);
bool is_path_exist(const std::string& s);
#endif

View File

@ -4,16 +4,10 @@
void init_create(init_args arg) {
std::cout << "Creating database in current directory." << std::endl;
DB db;
time_t t = time(0);
struct tm* currentTime = localtime(&t);
DB db;
db.creationDate.day = currentTime->tm_mday;
db.creationDate.month = currentTime->tm_mon;
db.creationDate.year = currentTime->tm_year + 1900;
db.lastAccessTime.day = currentTime->tm_mday;
db.lastAccessTime.month = currentTime->tm_mon;
db.lastAccessTime.year = currentTime->tm_year + 1900;
db.creationDate = get_today();
db.lastAccessTime = get_today();
// Mandatory stuff to stop segmantion fault due to
// pointers unintialized.

View File

@ -26,12 +26,7 @@ void proj_add(proj_args args) {
proj.pri = stoi(temp);
/* Creation date */
time_t t = time(0);
struct tm* currentTime = localtime(&t);
proj.creationDate.day = currentTime->tm_mday;
proj.creationDate.month = currentTime->tm_mon;
proj.creationDate.year = currentTime->tm_year + 1900;
proj.creationDate = get_today();
(*args.db->projects)[proj.uuid] = proj;
writeDB(*args.db, LOC_DB);