From 80aa0341a1ca964769a13c7c5826c6719c6a6a9d Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 14 Jan 2025 16:21:26 -0600 Subject: [PATCH] properly force string prefs to be quoted on win/linux --- src/fileio/fileio.cpp | 4 ++-- src/fileio/fileio.hpp | 2 +- src/tools/prefs.win.cpp | 5 +---- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/fileio/fileio.cpp b/src/fileio/fileio.cpp index 7edf6e78..c7ea40ea 100644 --- a/src/fileio/fileio.cpp +++ b/src/fileio/fileio.cpp @@ -163,9 +163,9 @@ std::string read_maybe_quoted_string(std::istream& from) { return result; } -std::string maybe_quote_string(std::string which) { +std::string maybe_quote_string(std::string which, bool force) { if(which.empty()) return "''"; - if(which.find_first_of(" \t\n\f") != std::string::npos || which[0] == '"' || which[0] == '\'') { + if(force || which.find_first_of(" \t\n\f") != std::string::npos || which[0] == '"' || which[0] == '\'') { // The string contains spaces or starts with a quote, so quote it. // We may have to escape quotes or backslashes. int apos = 0, quot = 0, bslash = 0, newline = 0, formfeed = 0; diff --git a/src/fileio/fileio.hpp b/src/fileio/fileio.hpp index c6ce2d21..3eee5bfa 100644 --- a/src/fileio/fileio.hpp +++ b/src/fileio/fileio.hpp @@ -33,7 +33,7 @@ bool save_party(fs::path dest_file, const cUniverse& univ); void init_directories(const char* exec_path); std::string read_maybe_quoted_string(std::istream& from); -std::string maybe_quote_string(std::string which); +std::string maybe_quote_string(std::string which, bool force = false); template struct array_value_type{ diff --git a/src/tools/prefs.win.cpp b/src/tools/prefs.win.cpp index af5442af..aed07bcd 100644 --- a/src/tools/prefs.win.cpp +++ b/src/tools/prefs.win.cpp @@ -106,10 +106,7 @@ static bool save_prefs(fs::path fpath) { fout << kv.first << " = " << std::fixed << boost::any_cast(kv.second) << std::endl; else if(kv.second.type() == typeid(std::string)){ std::string value = boost::any_cast(kv.second); - // Surround with quotes so maybe_quote_string() will always quote it - value.insert(0, 1, '"'); - value.push_back('"'); - fout << kv.first << " = " << maybe_quote_string(value) << std::endl; + fout << kv.first << " = " << maybe_quote_string(value, true) << std::endl; }else printf("Warning: Unknown preference value type, skipping...\n"); if(!fout) { perror("Error writing preferences");