From da3b5b2131d2b4ff5cb127e92090fca568376835 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Wed, 24 May 2023 10:28:49 +1200 Subject: in-progress: Config refactor started, changed all existing keybind command args and added in the new files, still many errors --- commands.cpp | 184 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 commands.cpp (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp new file mode 100644 index 0000000..3e78463 --- /dev/null +++ b/commands.cpp @@ -0,0 +1,184 @@ +#include "commands.h" +#include "error.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +using std::cout, std::endl, std::string, std::vector, std::tolower; + +const void CommandsModule::printHello(const CommandArg* argv) +{ + cout << "Hello" << endl; +} + +const void CommandsModule::echo(const CommandArg* argv) +{ + cout << "Echo: '" << argv[0].str << '\'' << endl; +} + +CommandsModule::CommandsModule() +{ + addCommand("printHello", &CommandsModule::printHello, 0, {}, this); + CommandArgType* args0 = new CommandArgType[1]; + args0[0] = STR_REST; + addCommand("echo", &CommandsModule::echo, 1, args0, this); +} +CommandsModule::~CommandsModule() +{ + for(Command c : commandList) + { + // This is probably needed but its not working + //if(c.argc > 0) + //delete[] c.argTypes; + } +} + +void CommandsModule::addCommand(Command c) +{ + if(lookupCommand(c.name) != nullptr) + { + cout << "Duplicate command: " << c.name << endl; + } + commandList.push_back(c); +} + +struct NameMatches +{ + NameMatches(string s): s_{s} {} + bool operator()(Command c) { return (c.name == s_); } + string s_; +}; + +Command* CommandsModule::lookupCommand(string name) +{ + auto elem = std::find_if(commandList.begin(), commandList.end(), NameMatches(name)); + if (elem != commandList.end()) + { + int i = elem - commandList.begin(); + return &(commandList[i]); + } + else + { + return nullptr; + } +} + +vector CommandsModule::splitCommand(string command) +{ + vector v; + string regexString = "((?:\"[^\"\n]+\")|(?:'[^'\n]+')|(?:[^ \n]+))+"; + std::regex regex(regexString, std::regex_constants::_S_icase); + auto begin = std::sregex_iterator(command.begin(), command.end(), regex); + auto end = std::sregex_iterator(); + for (std::sregex_iterator i = begin; i != end; i++) + { + std::smatch match = *i; + std::string str = match.str(); + if(str[0] == '\'' || str[0] == '"') + str = str.substr(1, str.size() - 2); + v.push_back(str); + } + return v; +} + +template +std::basic_string lowercase(const std::basic_string& s) +{ + std::basic_string s2 = s; + std::transform(s2.begin(), s2.end(), s2.begin(), [](unsigned char c){ return std::tolower(c); }); + return s2; +} + +CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandArgType* argTypes, const int argc) +{ + CommandArg* args = new CommandArg[argc]; + for(int i = 1; i < argc + 1; i++) + { + switch(argTypes[i-1]) + { + case STR: args[i-1].str = (char*)split[i].c_str(); break; + case NUM: args[i-1].num = std::stoi(split[i]); break; + case DIR: args[i-1].dir = LEFT; break; + case STR_REST: + { + string rest = ""; + for(int j = i; j < split.size(); j++) + { + rest += split[j]; + if(j != split.size() - 1) + rest += " "; + } + args[i-1].str = new char[rest.size()]; + strcpy(args[i-1].str, rest.c_str()); + return args; + } + case NUM_ARR_REST: + { + int* rest = new int[split.size() - i]; + for(int j = 0; j < split.size() - i; j++) + { + rest[j] = std::stoi(split[j + i]); + } + args[i-1].numArr = {rest, (int) split.size() - i}; + return args; + } + default: cout << "UH OH SOMETHING IS VERY WRONG" << endl; + } + } + return args; +} + +void CommandsModule::runCommand(string command) +{ + vector split = splitCommand(command); + Command* cmd = lookupCommand(split[0]); + if(cmd == nullptr) + throw Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name"); + if(cmd->argc > split.size() - 1) + throw Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args"); + CommandArg* args = getCommandArgs(split, cmd->argTypes, cmd->argc); + try + { + cmd->func(*cmd->module, args); + } + catch (Err e) + { + for(int i = 0; i < cmd->argc; i++) + { + if(cmd->argTypes[i] == STR_REST) + delete[] args[i].str; + } + delete[] args; + throw e; + } + for(int i = 0; i < cmd->argc; i++) + { + if(cmd->argTypes[i] == STR_REST) + delete[] args[i].str; + } + delete[] args; +} + +Err CommandsModule::checkCommand(string command) +{ + vector split = splitCommand(command); + Command* cmd = lookupCommand(split[0]); + if(cmd == nullptr) + return Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name"); + if(cmd->argc > split.size()) + return Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args"); + CommandArg* args = getCommandArgs(split, cmd->argTypes, cmd->argc); + for(int i = 0; i < cmd->argc; i++) + { + if(cmd->argTypes[i] == STR_REST || cmd->argTypes[i] == NUM_ARR_REST) + delete[] args[i].str; + } + delete[] args; + return Err(NOERR, ""); +} -- cgit v1.2.3 From 27137ec9d29c36df8117869773203b243849896c Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Fri, 26 May 2023 21:46:19 +1200 Subject: feat: Made keybinds work (I hope) Note: the config file reloading keybinds isn't quite working, though, need to ungrab the keys --- commands.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp index 3e78463..322d160 100644 --- a/commands.cpp +++ b/commands.cpp @@ -104,7 +104,7 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA { case STR: args[i-1].str = (char*)split[i].c_str(); break; case NUM: args[i-1].num = std::stoi(split[i]); break; - case DIR: args[i-1].dir = LEFT; break; + case MOVDIR: args[i-1].dir = LEFT; break; case STR_REST: { string rest = ""; -- cgit v1.2.3 From 6b3dff404248bcf03e0bf0463a8c53d8918f7eec Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Wed, 31 May 2023 20:55:49 +1200 Subject: Added support for commands without a module Also made it easier to add commands by making the ~addCommand~ function accept a vector for ~argTypes~ and then convert it. --- commands.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp index 322d160..549775e 100644 --- a/commands.cpp +++ b/commands.cpp @@ -47,6 +47,20 @@ void CommandsModule::addCommand(Command c) } commandList.push_back(c); } +void CommandsModule::addCommand(std::string name, const void (*func)(const CommandArg *), const int argc, CommandArgType *argTypes) +{ + Command c = {name, nullptr, func, argc, argTypes, nullptr}; + addCommand(c); +} +void CommandsModule::addCommand(std::string name, const void(*func)(const CommandArg*), const int argc, std::vector argTypes) +{ + CommandArgType* argTypesArr = new CommandArgType[argc]; + for(int i = 0; i < argc; i++) + { + argTypesArr[i] = argTypes[i]; + } + addCommand(name, func, argc, argTypesArr); +} struct NameMatches { @@ -145,7 +159,10 @@ void CommandsModule::runCommand(string command) CommandArg* args = getCommandArgs(split, cmd->argTypes, cmd->argc); try { - cmd->func(*cmd->module, args); + if(cmd->module == nullptr) + cmd->staticFunc(args); + else + cmd->func(*cmd->module, args); } catch (Err e) { -- cgit v1.2.3 From 01bc6a33eb235cd10851e2c31b99e6840603ca7d Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Thu, 1 Jun 2023 12:53:13 +1200 Subject: IT WORKS!!!!!! The new config commands system works, finally able to run this as a test and it works!!!!!. Still more to be done but at least it works. --- commands.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp index 549775e..09bdf62 100644 --- a/commands.cpp +++ b/commands.cpp @@ -150,6 +150,7 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA void CommandsModule::runCommand(string command) { + cout << command << endl; vector split = splitCommand(command); Command* cmd = lookupCommand(split[0]); if(cmd == nullptr) -- cgit v1.2.3 From e9cc5756dbb0a2d079a7b5e3438d79945f819df5 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sat, 3 Jun 2023 21:30:05 +1200 Subject: feat: Keybind updates Re added the ability to swap super and mod as a config parameter (the ~swapmods~) command). Finally fixed keybinds sometimes not working because of numlock, bitwise & with the modifiers I actually care about. --- commands.cpp | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp index 09bdf62..c41536d 100644 --- a/commands.cpp +++ b/commands.cpp @@ -12,30 +12,15 @@ using std::cout, std::endl, std::string, std::vector, std::tolower; -const void CommandsModule::printHello(const CommandArg* argv) -{ - cout << "Hello" << endl; -} - -const void CommandsModule::echo(const CommandArg* argv) -{ - cout << "Echo: '" << argv[0].str << '\'' << endl; -} - CommandsModule::CommandsModule() { - addCommand("printHello", &CommandsModule::printHello, 0, {}, this); - CommandArgType* args0 = new CommandArgType[1]; - args0[0] = STR_REST; - addCommand("echo", &CommandsModule::echo, 1, args0, this); } CommandsModule::~CommandsModule() { for(Command c : commandList) { - // This is probably needed but its not working - //if(c.argc > 0) - //delete[] c.argTypes; + if(c.argc > 0) + delete[] c.argTypes; } } @@ -150,7 +135,6 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA void CommandsModule::runCommand(string command) { - cout << command << endl; vector split = splitCommand(command); Command* cmd = lookupCommand(split[0]); if(cmd == nullptr) -- cgit v1.2.3 From 0b539b0b0278f2d7c2b7629e6d28d8463cba2688 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Mon, 5 Jun 2023 20:35:32 +1200 Subject: Added a very basic config, and fixed some stuff NOTE: for some reason toggling doesn't work anymore --- commands.cpp | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp index c41536d..141de26 100644 --- a/commands.cpp +++ b/commands.cpp @@ -86,10 +86,9 @@ vector CommandsModule::splitCommand(string command) return v; } -template -std::basic_string lowercase(const std::basic_string& s) +string lowercase(string s) { - std::basic_string s2 = s; + string s2 = s; std::transform(s2.begin(), s2.end(), s2.begin(), [](unsigned char c){ return std::tolower(c); }); return s2; } @@ -103,30 +102,41 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA { case STR: args[i-1].str = (char*)split[i].c_str(); break; case NUM: args[i-1].num = std::stoi(split[i]); break; - case MOVDIR: args[i-1].dir = LEFT; break; + case MOVDIR: + { + if(lowercase(split[i]) == "up") + args[i-1].dir = UP; + else if(lowercase(split[i]) == "down") + args[i-1].dir = DOWN; + else if(lowercase(split[i]) == "left") + args[i-1].dir = LEFT; + else if(lowercase(split[i]) == "right") + args[i-1].dir = RIGHT; + break; + } case STR_REST: - { - string rest = ""; - for(int j = i; j < split.size(); j++) { - rest += split[j]; - if(j != split.size() - 1) - rest += " "; + string rest = ""; + for(int j = i; j < split.size(); j++) + { + rest += split[j]; + if(j != split.size() - 1) + rest += " "; + } + args[i-1].str = new char[rest.size()]; + strcpy(args[i-1].str, rest.c_str()); + return args; } - args[i-1].str = new char[rest.size()]; - strcpy(args[i-1].str, rest.c_str()); - return args; - } case NUM_ARR_REST: - { - int* rest = new int[split.size() - i]; - for(int j = 0; j < split.size() - i; j++) { - rest[j] = std::stoi(split[j + i]); + int* rest = new int[split.size() - i]; + for(int j = 0; j < split.size() - i; j++) + { + rest[j] = std::stoi(split[j + i]); + } + args[i-1].numArr = {rest, (int) split.size() - i}; + return args; } - args[i-1].numArr = {rest, (int) split.size() - i}; - return args; - } default: cout << "UH OH SOMETHING IS VERY WRONG" << endl; } } -- cgit v1.2.3 From d4196b2476909e2c92ece6363eee7f450d74be5f Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Fri, 16 Jun 2023 17:33:41 +1200 Subject: feat: Error checking for command args The command arguements now handle errors. --- commands.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp index 141de26..4e8e01b 100644 --- a/commands.cpp +++ b/commands.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -101,7 +102,19 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA switch(argTypes[i-1]) { case STR: args[i-1].str = (char*)split[i].c_str(); break; - case NUM: args[i-1].num = std::stoi(split[i]); break; + case NUM: + { + try + { + args[i-1].num = std::stoi(split[i]); + break; + } + catch(std::invalid_argument e) + { + delete[] args; + throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a number!"); + } + } case MOVDIR: { if(lowercase(split[i]) == "up") @@ -112,6 +125,11 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA args[i-1].dir = LEFT; else if(lowercase(split[i]) == "right") args[i-1].dir = RIGHT; + else + { + delete[] args; + throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a direction!"); + } break; } case STR_REST: @@ -132,7 +150,16 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA int* rest = new int[split.size() - i]; for(int j = 0; j < split.size() - i; j++) { - rest[j] = std::stoi(split[j + i]); + try + { + rest[j] = std::stoi(split[j + i]); + } + catch(std::invalid_argument e) + { + delete[] rest; + delete[] args; + throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a number!"); + } } args[i-1].numArr = {rest, (int) split.size() - i}; return args; @@ -151,7 +178,15 @@ void CommandsModule::runCommand(string command) throw Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name"); if(cmd->argc > split.size() - 1) throw Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args"); - CommandArg* args = getCommandArgs(split, cmd->argTypes, cmd->argc); + CommandArg* args; + try + { + args = getCommandArgs(split, cmd->argTypes, cmd->argc); + } + catch(Err e) + { + throw e; + } try { if(cmd->module == nullptr) @@ -185,7 +220,15 @@ Err CommandsModule::checkCommand(string command) return Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name"); if(cmd->argc > split.size()) return Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args"); - CommandArg* args = getCommandArgs(split, cmd->argTypes, cmd->argc); + CommandArg* args; + try + { + args = getCommandArgs(split, cmd->argTypes, cmd->argc); + } + catch(Err e) + { + return e; + } for(int i = 0; i < cmd->argc; i++) { if(cmd->argTypes[i] == STR_REST || cmd->argTypes[i] == NUM_ARR_REST) -- cgit v1.2.3