fix cvs quoting

develop
myk002 2022-10-14 13:11:26 -07:00
parent fc926df59d
commit fea0ce0718
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
3 changed files with 22 additions and 12 deletions

@ -2,5 +2,5 @@
,,{givename name="foo dumper"} ,,"{givename name=""foo dumper""}"
,,{givename name="foo"} ,,"{givename name=""foo""}"

Can't render this file because it contains an unexpected character in line 5 and column 18.

@ -5,8 +5,6 @@
* Written by cdombroski. * Written by cdombroski.
*/ */
#include <algorithm>
#include <regex>
#include <sstream> #include <sstream>
#include <unordered_map> #include <unordered_map>
@ -1011,11 +1009,23 @@ static const char * get_tile_zone(const df::coord &pos,
return add_expansion_syntax(zone, get_zone_keys(zone)); return add_expansion_syntax(zone, get_zone_keys(zone));
} }
static string csv_sanitize(const string &str) { // surrounds the given string in quotes and replaces internal double quotes (")
static const std::regex pattern("\""); // with double double quotes ("") (as per the csv spec)
static const string replacement("\"\""); static string csv_quote(const string &str) {
std::ostringstream outstr;
outstr << "\"";
size_t start = 0;
auto end = str.find('"');
while (end != std::string::npos) {
outstr << str.substr(start, end - start);
outstr << "\"\"";
start = end + 1;
end = str.find('"', start);
}
outstr << str.substr(start, end) << "\"";
return std::regex_replace(str, pattern, replacement); return outstr.str();
} }
static const char * get_tile_query(const df::coord &pos, static const char * get_tile_query(const df::coord &pos,
@ -1042,11 +1052,11 @@ static const char * get_tile_query(const df::coord &pos,
std::ostringstream str; std::ostringstream str;
if (bld_name.size()) if (bld_name.size())
str << "{givename name=\"" << csv_sanitize(bld_name) << "\"}"; str << "{givename name=" + csv_quote(bld_name) + "}";
if (zone_name.size()) if (zone_name.size())
str << "{namezone name=\"" << csv_sanitize(zone_name) << "\"}"; str << "{namezone name=" + csv_quote(zone_name) + "}";
return cache(str); return cache(csv_quote(str.str()));
} }
static const char * get_tile_rooms(const df::coord &, const tile_context &ctx) { static const char * get_tile_rooms(const df::coord &, const tile_context &ctx) {

@ -444,7 +444,7 @@ function extra_fns.gui_quantum(pos)
view:onInput({_STRING=string.byte('f')}) view:onInput({_STRING=string.byte('f')})
view:onInput({_STRING=string.byte('o')}) view:onInput({_STRING=string.byte('o')})
view:onInput({_STRING=string.byte('o')}) view:onInput({_STRING=string.byte('o')})
view:onInput({SELECT=true}) send_keys('SELECT')
-- rotate the dump direction to the south -- rotate the dump direction to the south
send_keys('CUSTOM_D') send_keys('CUSTOM_D')
view:onRender() view:onRender()