From 9ddeb526bf3fcfffb0ee3ca783ee87d78f6d3614 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Mon, 16 Feb 2015 17:23:33 -0500 Subject: [PATCH] Some fixes to the routine that parses quoted strings - Now (probably) properly deals with escaped characters (either backslash or the quote delimiter) --- src/tools/fileio.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/tools/fileio.cpp b/src/tools/fileio.cpp index b75bf1b2..7a19b994 100644 --- a/src/tools/fileio.cpp +++ b/src/tools/fileio.cpp @@ -86,14 +86,23 @@ void check_for_intel() { std::string read_maybe_quoted_string(std::istream& from) { std::string result; from >> std::ws; - if(from.peek() == '"' || from.peek() == '\'') { + if(from.peek() == '"' || from.peek() == '\'') { char delim = from.get(); getline(from, result, delim); if(result.empty()) return result; while(result[result.length() - 1] == '\\') { - result += delim; + result[result.length() - 1] = delim; std::string nextPart; getline(from, nextPart, delim); + // Collapse any double backslashes; remove any single backslashes + for(std::string::iterator iter = nextPart.begin(); iter != nextPart.end(); iter++) { + if(iter[0] == '\\' && iter + 1 != nextPart.end() && iter[1] != '\\') { + iter = nextPart.erase(iter); + // After this, iter points to the second of the two backslashes, so + // when incremented by the loop, it'll point to the character after the backslashes. + } + } + // Note that this does not support escaping the single quotes in strings delimited by double quotes, and vice versa. result += nextPart; } } else from >> result;