diff options
| author | BossCode45 <human.cyborg42@gmail.com> | 2023-06-21 17:19:05 +1200 |
|---|---|---|
| committer | BossCode45 <human.cyborg42@gmail.com> | 2023-06-21 17:19:05 +1200 |
| commit | d7da80856ed209066b6f70d18bda9f506e05e67b (patch) | |
| tree | 16124789c04fbe52b2b51266e299f7d26259955f /commands.cpp | |
| parent | 8b4b487e685c72a478baf74aea2bf756988fe550 (diff) | |
| parent | 5c799ffa098f6c9b6d88513c2662968a9f5aaa41 (diff) | |
| download | YATwm-d7da80856ed209066b6f70d18bda9f506e05e67b.tar.gz YATwm-d7da80856ed209066b6f70d18bda9f506e05e67b.zip | |
Merge branch 'config-refactor'
Added proper line parser
Diffstat (limited to 'commands.cpp')
| -rw-r--r-- | commands.cpp | 62 |
1 files changed, 52 insertions, 10 deletions
diff --git a/commands.cpp b/commands.cpp index 4e8e01b..de7d81e 100644 --- a/commands.cpp +++ b/commands.cpp @@ -72,18 +72,60 @@ Command* CommandsModule::lookupCommand(string name) vector<string> CommandsModule::splitCommand(string command) { vector<string> 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++) + string arg = ""; + bool inQuotes = false; + bool escapeNext = true; + char quoteType; + for(int i = 0; i < command.size(); 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); + if(escapeNext) + { + arg += command[i]; + escapeNext = false; + } + else if(command[i] == '\\') + { + escapeNext = true; + } + else if(inQuotes) + { + if(command[i] == quoteType) + { + if(arg != "") + { + v.push_back(arg); + arg = ""; + } + inQuotes = false; + } + else + { + arg += command[i]; + } + } + else + { + if(command[i] == ' ') + { + if(arg != "") + { + v.push_back(arg); + arg = ""; + } + } + else if(command[i] == '"' || command[i] == '\'') + { + inQuotes = true; + quoteType = command[i]; + } + else + { + arg += command[i]; + } + } } + if(arg != "") + v.push_back(arg); return v; } |
