properly force string prefs to be quoted on win/linux

This commit is contained in:
2025-01-14 16:21:26 -06:00
committed by Celtic Minstrel
parent aa58b3cb41
commit 80aa0341a1
3 changed files with 4 additions and 7 deletions

View File

@@ -163,9 +163,9 @@ std::string read_maybe_quoted_string(std::istream& from) {
return result; 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.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. // The string contains spaces or starts with a quote, so quote it.
// We may have to escape quotes or backslashes. // We may have to escape quotes or backslashes.
int apos = 0, quot = 0, bslash = 0, newline = 0, formfeed = 0; int apos = 0, quot = 0, bslash = 0, newline = 0, formfeed = 0;

View File

@@ -33,7 +33,7 @@ bool save_party(fs::path dest_file, const cUniverse& univ);
void init_directories(const char* exec_path); void init_directories(const char* exec_path);
std::string read_maybe_quoted_string(std::istream& from); 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<typename T> template<typename T>
struct array_value_type{ struct array_value_type{

View File

@@ -106,10 +106,7 @@ static bool save_prefs(fs::path fpath) {
fout << kv.first << " = " << std::fixed << boost::any_cast<double>(kv.second) << std::endl; fout << kv.first << " = " << std::fixed << boost::any_cast<double>(kv.second) << std::endl;
else if(kv.second.type() == typeid(std::string)){ else if(kv.second.type() == typeid(std::string)){
std::string value = boost::any_cast<std::string>(kv.second); std::string value = boost::any_cast<std::string>(kv.second);
// Surround with quotes so maybe_quote_string() will always quote it fout << kv.first << " = " << maybe_quote_string(value, true) << std::endl;
value.insert(0, 1, '"');
value.push_back('"');
fout << kv.first << " = " << maybe_quote_string(value) << std::endl;
}else printf("Warning: Unknown preference value type, skipping...\n"); }else printf("Warning: Unknown preference value type, skipping...\n");
if(!fout) { if(!fout) {
perror("Error writing preferences"); perror("Error writing preferences");