123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #include "server/commands/CommandUtils.h"
- static bool splitStringIntern(const std::string& rawCommand, std::string& command, std::vector<std::string>& args) {
- size_t old = 0;
- size_t index = 0;
-
- while(index < rawCommand.size()) {
- if(rawCommand[index] == ' ') {
- command = rawCommand.substr(old, index - old);
- old = index + 1;
- index++;
- break;
- }
- index++;
- }
-
- if(index == rawCommand.size()) {
- command = rawCommand.substr(old, index - old);
- return false;
- }
-
- while(index < rawCommand.size()) {
- if(rawCommand[index] == '"') {
-
- if(index != old) {
- return true;
- }
- old = index + 1;
- while(true) {
- index++;
-
- if(index >= rawCommand.size()) {
- return true;
- } else if(rawCommand[index] == '"') {
- break;
- }
- }
-
- if(index + 1 < rawCommand.size() && rawCommand[index + 1] != ' ') {
- return true;
- }
- args.push_back(rawCommand.substr(old, index - old));
- old = index + 2;
- } else if(rawCommand[index] == ' ') {
- if(index > old && index - old > 0) {
- args.push_back(rawCommand.substr(old, index - old));
- }
- old = index + 1;
- }
- index++;
- }
- if(index > old && index - old > 0) {
- args.push_back(rawCommand.substr(old, index - old));
- }
- return false;
- }
- bool CommandUtils::splitString(const std::string& rawCommand, std::string& command, std::vector<std::string>& args) {
- bool b = splitStringIntern(rawCommand, command, args);
- if(b) {
- args.clear();
- command = "";
- }
- return b;
- }
|