issue 153: command line parsing dying on imbalanced quotes in comments

This commit is contained in:
jonathan pickett
2014-07-22 15:58:40 -07:00
parent 192b285b14
commit 5e05dad83a
2 changed files with 24 additions and 8 deletions
Binary file not shown.
+24 -8
View File
@@ -447,21 +447,37 @@ std::vector<std::string> split(const std::string &s, char delim) {
vector<string> Tokenize(string line) {
vector<string> tokens;
stringstream token;
for (string::iterator sit = line.begin(); sit != line.end(); sit++) {
// no need to parse empty lines, or comment lines (which may have unbalanced quotes)
if ((line.length() == 0) ||
((line.length() != 0) && (*line.begin()) == '#')) {
return tokens;
}
for (string::const_iterator sit = line.begin(); sit != line.end(); sit++) {
char c = *(sit);
if (isspace(c) && token.str().length() > 0) {
tokens.push_back(token.str());
token.str("");
} else if (c == '\'' || c == '\"') {
char endQuote = c;
while (++sit != line.end()) {
if (*sit == endQuote) break;
token << *sit;
string::const_iterator endQuoteIt = sit;
while (++endQuoteIt != line.end()) {
if (*endQuoteIt == endQuote) break;
}
if (endQuoteIt != line.end()) {
while (++sit != endQuoteIt) {
token << (*sit);
}
string path = token.str();
replace(path.begin(), path.end(), '/', '\\');
tokens.push_back(path);
token.str("");
} else {
// stuff the imbalanced quote character and continue
token << (*sit);
}
string path = token.str();
replace(path.begin(), path.end(), '/', '\\');
tokens.push_back(path);
token.str("");
} else {
token << c;
}