diff --git a/Makefile b/Makefile index ddb999a..b7a2589 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ cpp_src += $(wildcard *.cpp) +cpp_src += $(wildcard ./utils/*.cpp) #cpp_src += $(wildcard ./communication/i2c/*.cpp) #c_src += $(wildcard ./algorithms/*.c) diff --git a/main.cpp b/main.cpp index 7087a1e..44cfaa1 100644 --- a/main.cpp +++ b/main.cpp @@ -12,9 +12,19 @@ #include "main.hpp" +CommandManager commander; + +void dummy() +{ + std::cout << "Dummy" << std::endl; +} + int main(int argc, char *argv[]) { std::cout << "Main Begin" << std::endl; + commander.addNewCommand("dummy", "The test command for testing the test", dummy); + commander(argv[1]); std::cout << "Main End" << std::endl; return 1; } + diff --git a/main.hpp b/main.hpp index b5423ed..5c928bb 100644 --- a/main.hpp +++ b/main.hpp @@ -4,6 +4,6 @@ #include #include #include - +#include "./utils/commandManager.h" #endif // __MAIN_HPP__ diff --git a/runtest b/runtest index 031a5b8..b6a1d39 100755 Binary files a/runtest and b/runtest differ diff --git a/utils/commandManager.cpp b/utils/commandManager.cpp new file mode 100644 index 0000000..17973a4 --- /dev/null +++ b/utils/commandManager.cpp @@ -0,0 +1,100 @@ +/* + * Authors : Kerem Yollu & Edwin Koch + * Date : 07.03.2021 + * Version : 0.1 + * License : MIT-0 + * + * Description : An easy way to intereact with argv for passing commands. + * + * TODO : Inplement singleton pattern + * TODO : Write description + * TODO : Comment the code wiht odxygen + * TODO : write a standart string Output function and implment it (will come with the BSL) 08.08.21 + */ + +#include "commandManager.h" + + +CommandManager::CommandManager():emptyIndex(0) +{ +} + +void CommandManager::addNewCommand( const std::string& commmand, + const std::string& description, + commanCallback_t callBack) +{ + if(emptyIndex >= MAX_MUNBER_OF_COMMANDS) // Check if the command list is full + { + std::cout << "Error | Intern | Command list is full!" << std::endl; + exit(1); + } + if(getLookUpIndex(commmand) >= 0) // Chek for command duplicats + { + std::cout << "Error | Intern | Command already exitst!" << std::endl; + exit(1); + } + + + commandLookup[emptyIndex].commmand = commmand; + commandLookup[emptyIndex].description = description; + commandLookup[emptyIndex].callBack = callBack; + emptyIndex++; +} + +void CommandManager::operator()(const std::string cmdName) +{ + int index = 0; + + if(cmdName == "help" || cmdName == "Help") + { + printHelp(); + exit(1); + } + + if(cmdName == "-h" || cmdName == "-H") + { + printCommads(); + exit(1); + } + + index = getLookUpIndex(cmdName); + + if(index < 0) + { + std::cout << "Error | Intern | Invalid Command!" << std::endl; + exit(1); + } + + commandLookup[index].callBack(); + exit(1); +} + + +// +// Privat memeber functions +// + + +int CommandManager::getLookUpIndex(const std::string& cmd) +{ + int index = 0; + for (index = 0; index < emptyIndex; index++) + { + if(commandLookup[index].commmand == cmd) + { + return index; + } + } + return -1; +} + +void CommandManager::printHelp() +{ + std::cout << "Function : printHelp is under construction" << std::endl; +} + +void CommandManager::printCommads() +{ + std::cout << "Function : printCommads is under construction" << std::endl; +} + diff --git a/utils/commandManager.h b/utils/commandManager.h new file mode 100644 index 0000000..0ad164d --- /dev/null +++ b/utils/commandManager.h @@ -0,0 +1,50 @@ +/* + * Authors : Kerem Yollu & Edwin Koch + * Date : 07.03.2021 + * + * Description : + * TODO : Inplement singleton pattern + * TODO : Write description + * TODO : Comment the code wiht odxygen + * + */ + + +#ifndef _COMMMANDMANAGER_H_ +#define _COMMMANDMANAGER_H_ + +#include +#include +#include +#include + +#define MAX_MUNBER_OF_COMMANDS 10 + +class CommandManager +{ + public: + typedef std::function commanCallback_t; + CommandManager(); + void addNewCommand( const std::string& commmand, + const std::string& description, + commanCallback_t callBack); + + void operator()(const std::string cmdName); + + + private: + unsigned int emptyIndex; + + struct commant_t{ + std::string commmand; + std::string description; + commanCallback_t callBack; // The Callback function could only be a void returning a void. + }; + + std::array commandLookup; + int getLookUpIndex(const std::string& cmd); // If command exists retunrs the index otherwise -1 + void printHelp(); // Prints all awailbale commands and their description. + void printCommads(); // Prints all awailable commnads without description. +}; + +#endif // _COMMMANDMANAGER_H_