Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. vector<string> splitCommands(string input)
  2. {
  3. string currentCmd;
  4. vector<string> commands;
  5. bool ignoreSpace = false;
  6. bool escaped = false;
  7.  
  8. //Loop each character in input string
  9. for (int i = 0; i < input.size(); i++)
  10. {
  11. char c = input.at(i);
  12. if (isprint(c))
  13. {
  14. if (i == input.size() - 1)
  15. {
  16. //Append last character
  17. currentCmd += c;
  18.  
  19. //Add command and clear current command
  20. commands.push_back(currentCmd);
  21. currentCmd = "";
  22. continue;
  23. }
  24. else if (c == '\\')
  25. {
  26. //If we're not escaping next character we now are else user entered \\ so we now aren't
  27. if (!escaped)
  28. {
  29. escaped = true;
  30. currentCmd += c;
  31. continue;
  32. }
  33. else
  34. {
  35. escaped = false;
  36. currentCmd += c;
  37. continue;
  38. }
  39. }
  40. else if (c == '"' && !escaped)
  41. {
  42. //If we're not currently inside a quotation we now are else user closed quote, start accepting spaces as deliminator
  43. if (!ignoreSpace)
  44. {
  45. ignoreSpace = true;
  46. currentCmd += c;
  47. continue;
  48. }
  49. else
  50. {
  51. ignoreSpace = false;
  52. currentCmd += c;
  53. continue;
  54. }
  55.  
  56. }
  57. else if (c == ';' && !ignoreSpace && !escaped)
  58. {
  59. //Add command and clear current command
  60. commands.push_back(currentCmd);
  61. currentCmd = "";
  62. continue;
  63. }
  64. else
  65. {
  66. currentCmd += c;
  67. continue;
  68. }
  69. }
  70. }
  71. return commands;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement