diff --git a/.gitignore b/.gitignore index a642095..a4aa8ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ + +/bin + # Prerequisites *.d diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..de72290 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,67 @@ +{ + "files.associations": { + "*.html": "html", + "*.jsste": "plaintext", + "string": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "map": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp", + "compare": "cpp", + "concepts": "cpp", + "ios": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocinfo": "cpp", + "xlocnum": "cpp", + "xmemory": "cpp", + "xstddef": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xtree": "cpp", + "xutility": "cpp" + } +} diff --git a/makefile b/makefile index 87aa1cd..eeeb5c4 100644 --- a/makefile +++ b/makefile @@ -1,3 +1,11 @@ -all: - g++ -std=c++11 ./src/main.cpp -o ./src/autom - ./src/autom test \ No newline at end of file +CC = g++ +CFLAGS = -std=c++11 -Wall +SRCDIR = ./src +BINDIR = ./bin + +$(BINDIR)/main: $(SRCDIR)/main.cpp $(SRCDIR)/command.cpp $(SRCDIR)/inputparser.cpp + $(CC) $(CFLAGS) $^ -o $@ + +.PHONY: clean +clean: + rm -f $(BINDIR)/autom \ No newline at end of file diff --git a/src/autom b/src/autom deleted file mode 100644 index 216952d..0000000 Binary files a/src/autom and /dev/null differ diff --git a/src/command.cpp b/src/command.cpp new file mode 100644 index 0000000..9b69ad5 --- /dev/null +++ b/src/command.cpp @@ -0,0 +1,49 @@ +#include "command.h" + + +Command::Command() {} + +Command::~Command() {} + +void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) +{ + commands[name] = func; +} + +void Command::addDefaultCommand(void (*func)(int argc, char *argv[])) +{ + defaultCommand = func; +} + +void Command::runCommand(char *name, int argc, char *argv[]) +{ + std::cout << "Running command: " << name << std::endl; + if (this->isInCommands(name)) + { + + for (int i = 0; i < argc; i++) + { + argv[i] = argv[i + 1]; + } + + commands[name](argc, argv); + } + else + { + defaultCommand(argc, argv); + } +} + +bool Command::isInCommands(char *name) +{ + for (auto const &command : commands) + { + if (command.first == name) + { + std::cout << "Command found: " << command.first << std::endl; + return true; + } + } + std::cout << "Command not found: " << name << std::endl; + return false; +} \ No newline at end of file diff --git a/src/command.h b/src/command.h new file mode 100644 index 0000000..bc4eda4 --- /dev/null +++ b/src/command.h @@ -0,0 +1,23 @@ +#ifndef COMMAND_H +#define COMMAND_H + +#include +#include +#include + +class Command +{ +public: + Command(void); + ~Command(void); + void addCommand(std::string name, void (*func)(int argc, char *argv[])); + void addDefaultCommand(void (*func)(int argc,char *argv[])); + void runCommand(char *name, int argc, char *argv[]); + bool isInCommands(char *name); + +private: + std::map commands; + void (*defaultCommand)(int argc,char *argv[]); +}; + +#endif // COMMAND_H \ No newline at end of file diff --git a/src/inputparser.cpp b/src/inputparser.cpp new file mode 100644 index 0000000..3fb4681 --- /dev/null +++ b/src/inputparser.cpp @@ -0,0 +1,38 @@ +#include "./inputparser.h" + +InputParser::InputParser(int argc, char *argv[]) +{ + for (int i = 1; i < argc; i++) + { + std::string arg(argv[i]); + if (arg.find("-") == 0) + { + std::string key = arg.substr(1); + if (i + 1 < argc) + { + std::string value(argv[i + 1]); + if (value.find("-") != 0) + { + args[key] = value; + i++; + } + else + { + args[key] = ""; + } + } + else + { + args[key] = ""; + } + } + } +} +std::string InputParser::getValue(std::string key) +{ + return args[key]; +} +bool InputParser::hasKey(std::string key) +{ + return args.count(key) > 0; +} \ No newline at end of file diff --git a/src/inputparser.h b/src/inputparser.h new file mode 100644 index 0000000..46ee070 --- /dev/null +++ b/src/inputparser.h @@ -0,0 +1,20 @@ +#ifndef INPUTPARSER_H +#define INPUTPARSER_H + +#include +#include +#include +#include +#include + +class InputParser +{ + std::map args; + +public: + InputParser(int argc, char *argv[]); + std::string getValue(std::string key); + bool hasKey(std::string key); +}; + +#endif // INPUTPARSER_H \ No newline at end of file diff --git a/src/main b/src/main deleted file mode 100644 index 216952d..0000000 Binary files a/src/main and /dev/null differ diff --git a/src/main.cpp b/src/main.cpp index bc7a786..66fa4f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,25 +1,43 @@ -#include -#include -#include +#include "main.h" int main(int argc, char *argv[]) { - // make dir in /etc/autom - std::string home = getenv("HOME"); - std::string dir = home + "/.autom"; - // std::cout << "dir: " << dir << std::endl; +#ifdef _WIN32 + _mkdir(dir.c_str()); +#else mkdir(dir.c_str(), 0777); +#endif - if (argc > 1 && argv[1][0] != '-') - { - std::string pre_script = "cd " + dir + " && "; - std::string script = pre_script + dir + "/" + argv[1] + ".sh"; - system(script.c_str()); - } - // else{ - // std::cout << "Usage: autom