diff --git a/CMakeLists.txt b/CMakeLists.txt index 8854238..1e79fd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,4 +10,6 @@ add_executable(${PROJECT_NAME} src/main.cpp src/main.h src/data/data.tpp src/data/data.h src/data/accountData.cpp src/data/accountData.h src/money/earning.cpp src/money/earning.h - src/data/dateMoney.cpp src/data/dateMoney.h) \ No newline at end of file + src/data/dateMoney.cpp src/data/dateMoney.h + src/optHandlers/optHandler.cpp src/optHandlers/optHandler.h + src/optHandlers/accountOptHandler.cpp src/optHandlers/accountOptHandler.h) \ No newline at end of file diff --git a/src/data/dateMoney.cpp b/src/data/dateMoney.cpp index 28dbe10..c67432c 100644 --- a/src/data/dateMoney.cpp +++ b/src/data/dateMoney.cpp @@ -5,7 +5,6 @@ #include "dateMoney.h" - bool DateMoney::operator<(const DateMoney &rhs) const { return mktime(date) < mktime(rhs.date); } diff --git a/src/data/dateMoney.h b/src/data/dateMoney.h index 638dcbc..1f79dda 100644 --- a/src/data/dateMoney.h +++ b/src/data/dateMoney.h @@ -21,7 +21,7 @@ public: DateMoney(const int *value, tm *date); private: - const int* value; + const int *value; tm *date; }; diff --git a/src/main.cpp b/src/main.cpp index 0408170..bd490e8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,13 +4,16 @@ #include "main.h" #include "data/accountData.h" +#include "optHandlers/accountOptHandler.h" #include #include #include #include #include -#include +#include + +using namespace Budget; const std::string homeDirectory = getpwuid(getuid())->pw_dir; @@ -21,8 +24,14 @@ void createRequiredFolders() { std::filesystem::create_directories(homeDirectory + "/.local/share/budget/receipts"); } -int main() { +int main(int argc, char *argv[]) { + std::vector args(argv, argv + argc); + + OptHandlers::AccountOptHandler accountOptHandler(args); + accountOptHandler.parse(); + createRequiredFolders(); + // Read all accounts saved and store them in accounts std::list accounts; for (const auto &file : std::filesystem::directory_iterator( homeDirectory + "/.local/share/budget/accounts")) { diff --git a/src/money/account.h b/src/money/account.h index 907e5c7..34bb0cc 100644 --- a/src/money/account.h +++ b/src/money/account.h @@ -21,8 +21,9 @@ public: Account(); int getValue(); - std::vector getTimeline(); + std::vector getTimeline(); + private: std::list transactions; diff --git a/src/money/earning.h b/src/money/earning.h index f79f079..6def678 100644 --- a/src/money/earning.h +++ b/src/money/earning.h @@ -12,8 +12,9 @@ public: const int value; explicit Earning(int value, std::tm date); - tm *getDate(); + tm *getDate(); + private: std::tm date; }; diff --git a/src/money/transaction.cpp b/src/money/transaction.cpp index 5f8b6a0..a81f467 100644 --- a/src/money/transaction.cpp +++ b/src/money/transaction.cpp @@ -15,6 +15,6 @@ Receipt &Transaction::getReceipt() { return receipt; } -tm * Transaction::getDate() { +tm *Transaction::getDate() { return &date; } diff --git a/src/money/transaction.h b/src/money/transaction.h index 8777f71..2737f37 100644 --- a/src/money/transaction.h +++ b/src/money/transaction.h @@ -17,7 +17,7 @@ public: Receipt &getReceipt(); - tm * getDate(); + tm *getDate(); private: diff --git a/src/optHandlers/accountOptHandler.cpp b/src/optHandlers/accountOptHandler.cpp new file mode 100644 index 0000000..d834b48 --- /dev/null +++ b/src/optHandlers/accountOptHandler.cpp @@ -0,0 +1,72 @@ +// +// Created by quentin on 8/13/22. +// + +#include +#include "accountOptHandler.h" + +using namespace Budget::OptHandlers; + + +void AccountOptHandler::parse() { + std::string test; + struct option longOpts[] = { + {"help", no_argument, nullptr, 'h'}, + {"list", no_argument, nullptr, 'l'}, + {"delete", required_argument, nullptr, 'd'}, + {"create", required_argument, nullptr, 'c'}, + {"value", required_argument, nullptr, 'v'}, + {nullptr} + }; + + while (true) { + int opt = getopt_long(getArgc(), getArgv(), "hl::d::c::v", longOpts, nullptr); + + if (opt == -1) { + break; + } + + switch (opt) { + case 'h': + setOpts.help = true; + break; + case 'l': + setOpts.list = true; + break; + case 'd': + setOpts.del = true; + setOpts.delAccount = optarg; + break; + case 'c': + setOpts.create = true; + setOpts.createAccount = optarg; + break; + case 'v': + setOpts.value = true; + setOpts.valueAccount = optarg; + break; + case '?': + setOpts.help = true; + setOpts.helpOut = stderr; + break; + default: + break; + } + } +} + +void AccountOptHandler::help() { + fprintf(setOpts.helpOut, "Help budget -a {-cvd account|-hl}\n"); + fprintf(setOpts.helpOut, " -h --help Output this message.\n"); + fprintf(setOpts.helpOut, " -l --list List available accounts.\n"); + fprintf(setOpts.helpOut, " -d --delete account Delete the specified account.\n"); + fprintf(setOpts.helpOut, " -c --create account Create a new account.\n"); + fprintf(setOpts.helpOut, " -v --value account Print the current value of account.\n"); +} + +const SetOpts *AccountOptHandler::getSetOpts() const { + return &setOpts; +} + +AccountOptHandler::AccountOptHandler(const std::vector &argv) : OptHandler(argv) {} + diff --git a/src/optHandlers/accountOptHandler.h b/src/optHandlers/accountOptHandler.h new file mode 100644 index 0000000..5eb631d --- /dev/null +++ b/src/optHandlers/accountOptHandler.h @@ -0,0 +1,41 @@ +// +// Created by quentin on 8/13/22. +// + +#ifndef BUDGET_ACCOUNTOPTHANDLER_H +#define BUDGET_ACCOUNTOPTHANDLER_H + + +#include "optHandler.h" + +namespace Budget::OptHandlers { + class AccountOptHandler : public OptHandler { + struct SetOpts { + FILE *helpOut = stdout; + bool help = false; + bool list = false; + bool del = false; + char *delAccount{}; + bool create = false; + char *createAccount{}; + bool value = false; + char *valueAccount{}; + }; + + public: + explicit AccountOptHandler(const std::vector &argv); + + void parse() override; + + void help() override; + + [[nodiscard]] const SetOpts *getSetOpts() const; + + private: + SetOpts setOpts; + }; + +} + + +#endif //BUDGET_ACCOUNTOPTHANDLER_H diff --git a/src/optHandlers/optHandler.cpp b/src/optHandlers/optHandler.cpp new file mode 100644 index 0000000..75752a4 --- /dev/null +++ b/src/optHandlers/optHandler.cpp @@ -0,0 +1,20 @@ +// +// Created by quentin on 8/13/22. +// + +#include "optHandler.h" + +#include + +using namespace Budget::OptHandlers; + + +OptHandler::OptHandler(std::vector argv) : argv(std::move(argv)) {} + +int OptHandler::getArgc() { + return argv.size(); +} + +char **OptHandler::getArgv() { + return argv.data(); +} diff --git a/src/optHandlers/optHandler.h b/src/optHandlers/optHandler.h new file mode 100644 index 0000000..c9cd392 --- /dev/null +++ b/src/optHandlers/optHandler.h @@ -0,0 +1,33 @@ +// +// Created by quentin on 8/13/22. +// + +#ifndef BUDGET_OPTHANDLER_H +#define BUDGET_OPTHANDLER_H + +#include +#include +#include +#include + +namespace Budget::OptHandlers { + class OptHandler { + public: + explicit OptHandler(std::vector argv); + + virtual void parse() = 0; + + int getArgc(); + + char **getArgv(); + + private: + + std::vector argv; + + virtual void help() = 0; + }; +} + + +#endif //BUDGET_OPTHANDLER_H