diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml deleted file mode 100644 index 7d31d7b..0000000 --- a/.pre-commit-config.yaml +++ /dev/null @@ -1,14 +0,0 @@ -# See https://pre-commit.com for more information -# See https://pre-commit.com/hooks.html for more hooks -repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.4.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files -- repo: https://github.com/doublify/pre-commit-clang-format.git - rev: master - hooks: - - id: clang-format diff --git a/.vscode/launch.json b/.vscode/launch.json index 4ba9490..ebd5fc6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/bin/skidjular", - "args": ["history"], + "args": [ "init" ], "stopAtEntry": false, "cwd": "${workspaceFolder}/build/bin/", "environment": [], diff --git a/build.sh b/build.sh index 02c6c1f..c857730 100755 --- a/build.sh +++ b/build.sh @@ -2,7 +2,7 @@ mkdir build cd build -conan install .. --build missing -cmake -DCMAKE_CXX_COMPILER=/usr/bin/g++ -DCMAKE_BUILD_TYPE=Debug .. +conan install .. --build=missing +cmake --config=Debug .. make -j9 cd .. diff --git a/conanfile.txt b/conanfile.txt index d29a454..9969b77 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,7 +1,6 @@ [requires] -zlib/1.2.11@conan/stable +zlib/1.2.11 boost/1.70.0 -OpenSSL/1.1.1@conan/stable [generators] cmake diff --git a/src/data.cpp b/src/data.cpp index ee1e645..eb8e0af 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -132,17 +132,17 @@ Date get_today() { res.month = currentTime->tm_mon; res.year = currentTime->tm_year + 1900; - delete currentTime; return res; } int get_day_num(Date initial) { auto m = (initial.month + 9) % 12; auto y = initial.year - m / 10; - return 365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + (d - 1); + return 365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + + (initial.day - 1); } -Date get_day_date(int intial) { +Date get_day_date(int g) { auto y = (10000 * g + 14780) / 3652425; auto ddd = g - (365 * y + y / 4 - y / 100 + y / 400); if (ddd < 0) @@ -150,7 +150,7 @@ Date get_day_date(int intial) { ddd = g - (365 * y + y / 4 - y / 100 + y / 400); auto mi = (100 * ddd + 52) / 3060; auto mm = (mi + 2) % 12 + 1; - auto y = y + (mi + 2) / 12; + y = y + (mi + 2) / 12; auto dd = ddd - (mi * 306 + 5) / 10 + 1; Date res; @@ -160,8 +160,6 @@ Date get_day_date(int intial) { return res; } -Date addDays_date(Date initial, int number) {} - bool is_empty(std::ifstream& pFile) { return pFile.peek() == std::ifstream::traits_type::eof(); } @@ -170,3 +168,104 @@ bool is_path_exist(const std::string& s) { struct stat buffer; return (stat(s.c_str(), &buffer) == 0); } + +///////////////// COPIED CODE +using namespace std; + +// Return if year is leap year or not. +bool isLeap(int y) { + if (y % 100 != 0 && y % 4 == 0 || y % 400 == 0) + return true; + + return false; +} + +// Given a date, returns number of days elapsed +// from the beginning of the current year (1st +// jan). +int offsetDays(int d, int m, int y) { + int offset = d; + + switch (m - 1) { + case 11: offset += 30; + case 10: offset += 31; + case 9: offset += 30; + case 8: offset += 31; + case 7: offset += 31; + case 6: offset += 30; + case 5: offset += 31; + case 4: offset += 30; + case 3: offset += 31; + case 2: offset += 28; + case 1: offset += 31; + } + + if (isLeap(y) && m > 2) + offset += 1; + + return offset; +} + +// Given a year and days elapsed in it, finds +// date by storing results in d and m. +void revoffsetDays(int offset, int y, int* d, int* m) { + int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + if (isLeap(y)) + month[2] = 29; + + int i; + for (i = 1; i <= 12; i++) { + if (offset <= month[i]) + break; + offset = offset - month[i]; + } + + *d = offset; + *m = i; +} + +// Add x days to the given date. +Date addDays_date(Date date, int x) { + auto d1 = date.day; + auto m1 = date.month; + auto y1 = date.year; + int offset1 = offsetDays(d1, m1, y1); + int remDays = isLeap(y1) ? (366 - offset1) : (365 - offset1); + + // y2 is going to store result year and + // offset2 is going to store offset days + // in result year. + int y2, offset2; + if (x <= remDays) { + y2 = y1; + offset2 = offset1 + x; + } + + else { + // x may store thousands of days. + // We find correct year and offset + // in the year. + x -= remDays; + y2 = y1 + 1; + int y2days = isLeap(y2) ? 366 : 365; + while (x >= y2days) { + x -= y2days; + y2++; + y2days = isLeap(y2) ? 366 : 365; + } + offset2 = x; + } + + // Find values of day and month from + // offset of result year. + int m2, d2; + revoffsetDays(offset2, y2, &d2, &m2); + + Date res; + res.day = d2; + res.month = m2; + res.year = y2; + return res; +} +///////////////////////////// diff --git a/src/data.h b/src/data.h index 1a645ab..9fd5e1e 100644 --- a/src/data.h +++ b/src/data.h @@ -25,6 +25,17 @@ struct Date { int hour = 0; int minute = 0; int second = 0; + + bool operator==(const Date& other) { + if (day == other.day) + if (month == other.month) + if (year == other.year) + if (hour == other.hour) + if (minute == other.minute) + if (second == other.minute) + return true; + return false; + } }; struct Project { @@ -79,10 +90,7 @@ void writeSkid(Skid skid, int skidNum); void loadSkid(Skid& skid, int skidNum); Date get_today(); -int get_day_num(Date initial); -Date get_day_date(int num); -Date addDays_date(Date initial, int number); -Date subDays_date(Date initial, int number); +Date addDays_date(Date initial, int); bool is_empty(std::ifstream& pFile); bool is_path_exist(const std::string& s); diff --git a/src/log.cpp b/src/log.cpp index 7a72650..7c2c0da 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -18,10 +18,10 @@ void logEntry(const std::string& msg, EntryType type) { ev.mesg = msg; ev.timerTime = timer_get(); - time_t t = time(0); + time_t t = time(0); struct tm* currentTime = localtime(&t); - ev.creationDate.day = currentTime->tm_mday; + ev.creationDate.day = currentTime->tm_mday; ev.creationDate.month = currentTime->tm_mon; ev.creationDate.year = currentTime->tm_year + 1900; ev.creationDate.hour = currentTime->tm_hour; @@ -53,7 +53,7 @@ void writeLog(const std::string& loc, bool append) { void printLog(const std::string& loc, int entries) { std::fstream f(LOC_LOG); - std::string line = ""; + std::string line = ""; if (entries) while (std::getline(f, line) && entries--) { std::cout << line << std::endl; diff --git a/src/main.cpp b/src/main.cpp index 7d02a0b..9664c6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "modules/info.h" #include "modules/init.h" #include "modules/proj.h" +#include "modules/skids.h" #include "timer.h" Module* get_Mod(std::string name) { @@ -21,6 +22,8 @@ Module* get_Mod(std::string name) { return info_new(); } else if (name == "history") { return history_new(); + } else if (name == "skids") { + return skids_new(); } return nullptr; }; @@ -33,6 +36,8 @@ void dispatch_Mod(std::string mod, std::vector args) { info_dispatch(args); } else if (mod == "history") { history_dispatch(args); + } else if (mod == "skids") { + skids_dispatch(args); } }; @@ -67,7 +72,7 @@ int main(int argc, const char* argv[]) { timer_stop(); exit(0); } else if (vm.count("help-module")) { - auto name = vm["help-module"].as(); + auto name = vm["help-module"].as(); auto module = get_Mod(name); if (!module) { std::cout << "Module not found.\n"; @@ -88,7 +93,7 @@ int main(int argc, const char* argv[]) { auto args = vm["module-args"].as>(); dispatch_Mod(name, args); } else - dispatch_Mod(name, {}); + dispatch_Mod(name, {""}); } else { std::cout << general << "\n"; writeLog(LOC_LOG); diff --git a/src/modules/info.cpp b/src/modules/info.cpp index 96319c3..7791fec 100644 --- a/src/modules/info.cpp +++ b/src/modules/info.cpp @@ -14,8 +14,8 @@ void info_print(info_args args) { info_mod* info_new() { info_mod* mod = new info_mod{}; - mod->name = "Info"; - mod->desc = "Shows information about the skids databse"; + mod->name = "Info"; + mod->desc = "Shows information about the skids databse"; return mod; } diff --git a/src/modules/init.cpp b/src/modules/init.cpp index 65d5858..3355386 100644 --- a/src/modules/init.cpp +++ b/src/modules/init.cpp @@ -6,12 +6,12 @@ void init_create(init_args arg) { std::cout << "Creating database in current directory." << std::endl; DB db; - db.creationDate = get_today(); + db.creationDate = get_today(); db.lastAccessTime = get_today(); // Mandatory stuff to stop segmantion fault due to // pointers unintialized. - db.projects = new std::map(); + db.projects = new std::map(); db.log.entries = new std::list(); logEntry("Creating database.", EntryType::Creation); @@ -25,7 +25,7 @@ void init_dispatch(const std::vector& arguments) { init_mod* init_new() { init_mod* mod = new init_mod{}; - mod->name = "Init"; - mod->desc = "Creates skidjular directory in $(pwd)"; + mod->name = "Init"; + mod->desc = "Creates skidjular directory in $(pwd)"; return mod; } diff --git a/src/modules/proj.cpp b/src/modules/proj.cpp index aa19e99..e8039b2 100644 --- a/src/modules/proj.cpp +++ b/src/modules/proj.cpp @@ -76,7 +76,7 @@ void proj_list(proj_args args) { proj_mod* proj_new() { proj_mod* mod = new proj_mod{}; - mod->name = "Project Manager"; + mod->name = "Project Manager"; mod->desc = std::string("Manages projects list with these commands:\n") + std::string("- skidjular proj add (Adds new project).\n") + diff --git a/src/modules/skids.cpp b/src/modules/skids.cpp new file mode 100644 index 0000000..93d93d3 --- /dev/null +++ b/src/modules/skids.cpp @@ -0,0 +1,185 @@ +#include "skids.h" + +void skids_init(skids_args args) { + logEntry("Creating new skids, skids_init", EntryType::Creation); + + /* Check first day of process */ + Date firstDate; + if (is_path_exist(LOC_SKIDID + std::to_string(args.db->lastSkidID))) { + Skid skid; + loadSkid(skid, args.db->lastSkidID); + firstDate = skid.date; + } else { + /* Today is the first day */ + firstDate = get_today(); + } + + /* Create skids */ + for (size_t i = 0; i < 7; i++) { + Skid skid; + skid.creationDate = get_today(); + skid.date = firstDate; + firstDate = addDays_date(firstDate, 1); + writeSkid(skid, args.db->lastSkidID++); + } + + /* Update database with right lastSkidID */ + writeDB(*args.db, LOC_DB); +} + +void skids_init_now(skids_args args) { + logEntry("Creating new skids, skids_init_now", EntryType::Creation); + + int skid_id = 0; + /* Load all skids */ + if (is_path_exist(LOC_SKIDID + std::to_string(args.db->lastSkidID))) { + /* Load all skids */ + for (size_t i = 0; i < args.db->lastSkidID; i++) { + Skid skid; + loadSkid(skid, i); + args.db->skids.push_back(skid); + } + + /* Get today's skidID */ + for (size_t i = 0; i < args.db->skids.size(); i++) { + if (args.db->skids[i].date == get_today()) { + skid_id = i; + break; + } + } + + /* Set skid_id to last id */ + skid_id = args.db->lastSkidID; + } else { + /* ID == 0 */ + skid_id = 0; + } + + /* Create skids */ + auto firstDate = get_today(); + for (size_t i = 0; i < 7; i++) { + Skid skid; + skid.creationDate = get_today(); + skid.date = firstDate; + firstDate = addDays_date(firstDate, 1); + writeSkid(skid, skid_id++); + } + + args.db->lastSkidID = skid_id; + writeDB(*args.db, LOC_DB); +} +void skids_now(skids_args args) {} +void skids_this(skids_args args) {} +void skids_set_this(skids_args args) {} +void skids_set_this_lazy(skids_args args) {} +void skids_set_lazy(skids_args args) {} +void skids_set_day(skids_args args) {} +void skids_set_interactive(skids_args args) {} + +skids_mod* skids_new() { + skids_mod* mod = new skids_mod(); + mod->name = "skids Manager"; + mod->desc = + std::string("Manages the skids data with these commands:\n") + + std::string( + "- skidjular skids init (Creates the next uncreated week)\n") + + std::string("- skidjular skids init now (Force creates this week)\n") + + std::string("- skidjular skids this (Outputs this week's projects)\n") + + std::string("- skidjular skids now (Outputs today's project)\n") + + std::string("- skidjular skids set this (Set this " + "week's to )\n") + + std::string("- skidjular skids set (Sets next unset " + " to \n") + + std::string("- skidjular skids set this lazy (Force sets this week's " + "projects automatically)\n") + + std::string("- skidjular skids set lazy (Sets next unset week's " + "projects automatically)\n") + + std::string("- skidjular skids i/interactive (Sets next unset week's " + "projects interactively)\n"); + return mod; +} + +void skids_dispatch(const std::vector& arguments) { + skids_args args; + void (*func)(skids_args) = nullptr; + + /* Get the action type and action param. */ + size_t param_offset = 0; + auto type_str = arguments[0]; + if (type_str == "init") { + if (arguments.size() > 1) { + if (arguments[1] == "now") { + args.type = skidsActType::init_now; + param_offset = 2; + func = skids_init_now; + goto afterActionGet; + } + } + args.type = skidsActType::init; + func = skids_init; + goto afterActionGet; + } else if (type_str == "now") { + args.type = skidsActType::now; + param_offset = 1; + func = skids_now; + goto afterActionGet; + } else if (type_str == "this") { + args.type = skidsActType::this_; + param_offset = 1; + func = skids_this; + goto afterActionGet; + } else if (type_str == "set") { + if (arguments.size() > 1) { + auto type_str2 = arguments[1]; + if (type_str2 == "this") { + if (arguments.size() > 2) { + auto type_str3 = arguments[2]; + if (type_str3 == "lazy") { + args.type = skidsActType::set_this_lazy; + param_offset = 3; + func = skids_set_this_lazy; + goto afterActionGet; + } + args.type = skidsActType::set_this; + func = skids_set_this; + goto afterActionGet; + } + } else if (type_str2 == "lazy") { + args.type = skidsActType::set_lazy; + param_offset = 2; + func = skids_set_lazy; + goto afterActionGet; + } else if (type_str2 == "interactive" || type_str2 == "i") { + args.type = skidsActType::set_interactive; + param_offset = 2; + func = skids_set_lazy; + goto afterActionGet; + } else if (arguments.size() >= 3) { + args.type = skidsActType::set_day; + param_offset = 4; + func = skids_set_day; + goto afterActionGet; + } + } + } + std::cout << "Inavlid number of arguments passed to skids!" << std::endl; + exit(1); + +afterActionGet: + /* Retrieve action parameter. */ + if (param_offset >= arguments.size()) { + args.action_param = {}; + } else { + for (size_t i = param_offset; i < arguments.size(); i++) { + args.action_param.push_back(arguments[i]); + } + } + + /* Load database */ + args.db = new DB(); + loadDB(*args.db, LOC_DB); + + /* Finally, dispatch function. Bye bye! */ + assert(func); + func(args); +} diff --git a/src/modules/skids.h b/src/modules/skids.h new file mode 100644 index 0000000..3986d90 --- /dev/null +++ b/src/modules/skids.h @@ -0,0 +1,45 @@ +#ifndef MODULE_SKIDS_H +#define MODULE_SKIDS_H + +#include +#include +#include +#include +#include + +#include "data.h" +#include "log.h" +#include "module.h" + +enum skidsActType { + init, + init_now, + now, + this_, + set_this, + set_this_lazy, + set_day, + set_lazy, + set_interactive +}; +struct skids_args { + skidsActType type; + std::vector action_param; + + DB* db; +}; +struct skids_mod : skids_args, Module {}; + +skids_mod* skids_new(); +void skids_init(skids_args args); +void skids_init_now(skids_args args); +void skids_now(skids_args args); +void skids_this(skids_args args); +void skids_set_this(skids_args args); +void skids_set_this_lazy(skids_args args); +void skids_set_lazy(skids_args args); +void skids_set_day(skids_args args); +void skids_set_interactive(skids_args args); +void skids_dispatch(const std::vector& arguments); + +#endif diff --git a/src/timer.cpp b/src/timer.cpp index a6ed9bc..23c2aed 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -7,7 +7,7 @@ void timer_start() { void timer_stop() { s_timer.isRunning = false; - s_timer.endTime = std::chrono::steady_clock::now(); + s_timer.endTime = std::chrono::steady_clock::now(); } float timer_get() { @@ -17,4 +17,3 @@ float timer_get() { std::chrono::steady_clock::now() - s_timer.startTime; return duration.count(); } -