Created
November 15, 2015 09:13
-
-
Save wareya/3449314bbabf24464d1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Septalicensed under the CC0, ISC, MIT, New-BSD, WTFPL, GNU All-Permissive, and Unlicense licenses. | |
// Code for splitting up a commandline-like string into a series of arguments, where \ escapes certain characters, including space, and "" escapes full arguments | |
std::vector<std::string> arglist; | |
std::string scraparg(""); | |
bool escape = false; | |
bool encapsulate = false; | |
for(auto character : command) | |
{ | |
if ( character == '\\' and escape == false ) // character is '\' | |
escape = true; | |
else | |
{ | |
if ( escape ) | |
{ | |
switch ( character ) | |
{ | |
case 'n': | |
scraparg += '\n'; | |
break; | |
case 't': | |
scraparg += '\t'; | |
break; | |
case '\\': | |
scraparg += '\\'; | |
break; | |
case '"': | |
scraparg += '\"'; | |
break; | |
case ' ': | |
scraparg += ' '; | |
break; | |
default: | |
scraparg += '\\'; | |
scraparg += character; | |
} | |
escape = false; | |
} | |
else | |
{ | |
switch ( character ) | |
{ | |
case '"': | |
encapsulate = !encapsulate; | |
if (!encapsulate) | |
{ | |
arglist.push_back(scraparg); | |
scraparg = ""; | |
} | |
break; | |
case ' ': | |
if(encapsulate) | |
scraparg += ' '; | |
else | |
{ | |
arglist.push_back(scraparg); | |
scraparg = ""; | |
} | |
break; | |
default: | |
scraparg += character; | |
} | |
} | |
escape = false; | |
} | |
} | |
if(strcmp(scraparg.data(), "") != 0) | |
arglist.push_back(scraparg); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment