Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<string> splitCommands(string input)
- {
- string currentCmd;
- vector<string> commands;
- bool ignoreSpace = false;
- bool escaped = false;
- //Loop each character in input string
- for (int i = 0; i < input.size(); i++)
- {
- char c = input.at(i);
- if (isprint(c))
- {
- if (i == input.size() - 1)
- {
- //Append last character
- currentCmd += c;
- //Add command and clear current command
- commands.push_back(currentCmd);
- currentCmd = "";
- continue;
- }
- else if (c == '\\')
- {
- //If we're not escaping next character we now are else user entered \\ so we now aren't
- if (!escaped)
- {
- escaped = true;
- currentCmd += c;
- continue;
- }
- else
- {
- escaped = false;
- currentCmd += c;
- continue;
- }
- }
- else if (c == '"' && !escaped)
- {
- //If we're not currently inside a quotation we now are else user closed quote, start accepting spaces as deliminator
- if (!ignoreSpace)
- {
- ignoreSpace = true;
- currentCmd += c;
- continue;
- }
- else
- {
- ignoreSpace = false;
- currentCmd += c;
- continue;
- }
- }
- else if (c == ';' && !ignoreSpace && !escaped)
- {
- //Add command and clear current command
- commands.push_back(currentCmd);
- currentCmd = "";
- continue;
- }
- else
- {
- currentCmd += c;
- continue;
- }
- }
- }
- return commands;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement