From 179f7b4107bacbd867c96b4d5d05dc76ffba41dc Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 29 Oct 2023 21:19:37 +0100 Subject: [PATCH] some changes && add command class --- lpstd.hpp | 5 ++ src/classes/command.hpp | 175 +++++++++++++++++++++++++++++++++++++ src/patterns/singleton.hpp | 4 + src/testing/testing.hpp | 16 ++-- 4 files changed, 191 insertions(+), 9 deletions(-) create mode 100644 src/classes/command.hpp diff --git a/lpstd.hpp b/lpstd.hpp index 8ee6cfc..4574a26 100644 --- a/lpstd.hpp +++ b/lpstd.hpp @@ -2,10 +2,15 @@ #ifndef __LPSTD_HPP__ #define __LPSTD_HPP__ +#ifdef _WIN32 +#include +#include +#endif #include "src/patterns/singleton.hpp" #include "src/testing/testing.hpp" // #include "src/faker/faker.hpp" #include "src/exceptions/exceptions.hpp" +#include "src/classes/command.hpp" #endif // __LPSTD_HPP__ diff --git a/src/classes/command.hpp b/src/classes/command.hpp new file mode 100644 index 0000000..ae6be8c --- /dev/null +++ b/src/classes/command.hpp @@ -0,0 +1,175 @@ +#ifndef __LPSTD_COMMAND_H__ +#define __LPSTD_COMMAND_H__ + +#include +#include +#include +#include +#include +#include +#include +#include "../patterns/singleton.hpp" + +struct commandInfo +{ + std::string name; + std::string description; + void (*func)(); + std::vector args; +}; + +class Command : public lpstd::Singleton +{ + +private: + std::list commands = {}; + std::string defaultCommandName = "help"; + + commandInfo currentCommand = {name : "", + description : "", + func : nullptr}; + +public: + Command(/* args */) + { + } + + ~Command() + { + } + + commandInfo getCurrentCommand() + { + return this->currentCommand; + } + + template + bool existsArg(Args &&...arg) + { + + for (auto &argToFind : {arg...}) + { + std::vector::iterator it = std::find(this->currentCommand.args.begin(), this->currentCommand.args.end(), argToFind); + if (it != this->currentCommand.args.end()) + { + return true; + } + } + + return false; + } + + template + std::string getArg(Args &&...arg) + { + + if (this->currentCommand.args.size() == 0) + { + return ""; + } + + std::vector args = this->currentCommand.args; + std::vector argsToFind = {arg...}; + std::vector values = {}; + for (auto &argToFind : argsToFind) + { + std::vector::iterator it = std::find(args.begin(), args.end(), argToFind); + if (it != args.end()) + { + values.push_back(*(it + 1)); + } + } + return values.size() > 0 ? values[0] : ""; + } + + void addCommand(std::string name, std::string description, void (*func)(void)) + { + commandInfo command; + command.name = name; + command.description = description; + command.func = func; + this->commands.push_back(command); + } + + // void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) + // { + // commandInfo command; + // command.name = name; + // command.description = description; + // command.func = func; + // this->commands.push_back(command); + // } + + std::string getHelpAsString() + { + std::string help = ""; + std::list sortedCommands = this->commands; + + sortedCommands.sort([](const commandInfo &a, const commandInfo &b) + { return a.name < b.name; }); + + for (auto &commandInfo : sortedCommands) + { + help += commandInfo.name + " - " + commandInfo.description + "\n"; + } + + return help; + } + + void callCommand(std::string command, int argc, char *argv[]) + { + std::vector args; + for (int i = 2; i < argc; i++) + { + args.push_back(argv[i]); + } + + for (auto &commandInfo : this->commands) + { + if (commandInfo.name == command) + { + commandInfo.args = args; + currentCommand = commandInfo; + commandInfo.func(); + return; + } + } + std::cout << "Command not found" << std::endl; + } + + void execute(int argc, char *argv[]) + { + + if (argc < 2) + { + std::cout << "Command not found \n" + << std::endl; + // throw lpstd::exceptions::ParameterException("argc < 2"); + + this->callCommand(defaultCommandName, argc, argv); + + return; + } + + std::string command = argv[1]; + std::vector args; + for (int i = 2; i < argc; i++) + { + args.push_back(argv[i]); + } + + for (auto &commandInfo : this->commands) + { + if (commandInfo.name == command) + { + commandInfo.args = args; + currentCommand = commandInfo; + commandInfo.func(); + return; + } + } + std::cout << "Command not found" << std::endl; + } +}; + +#endif // __LPSTD_COMMAND_H__ \ No newline at end of file diff --git a/src/patterns/singleton.hpp b/src/patterns/singleton.hpp index 3b7e7df..86650da 100644 --- a/src/patterns/singleton.hpp +++ b/src/patterns/singleton.hpp @@ -1,4 +1,6 @@ +#ifndef __LPSTD_SINGLETON_HPP__ +#define __LPSTD_SINGLETON_HPP__ namespace lpstd { template @@ -20,3 +22,5 @@ namespace lpstd Singleton &operator=(Singleton const &); }; } + +#endif // __LPSTD_SINGLETON_HPP__ \ No newline at end of file diff --git a/src/testing/testing.hpp b/src/testing/testing.hpp index 50fe43c..2f42de6 100644 --- a/src/testing/testing.hpp +++ b/src/testing/testing.hpp @@ -3,8 +3,9 @@ #include #ifdef _WIN32 -#include -#include +#define needUTF8() (SetConsoleOutputCP(65001) && setvbuf(stdout, nullptr, _IOFBF, 1000)) +#else +#define needUTF8() #endif namespace lpstd @@ -22,6 +23,9 @@ namespace lpstd void drawResults() { + + needUTF8(); + std::cout << std::endl; if (Results.failed == 0) { @@ -40,13 +44,7 @@ namespace lpstd Expect(T value) { -#ifdef _WIN32 - // Set console code page to UTF-8 so console known how to interpret string data - SetConsoleOutputCP(CP_UTF8); - - // Enable buffering to prevent VS from chopping up UTF-8 byte sequences - setvbuf(stdout, nullptr, _IOFBF, 1000); -#endif + needUTF8(); this->value = value; }