From 213b61e7ec8ce2bbd66dbcac4414cb845c4698b0 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 11 Jan 2025 23:47:25 -0600 Subject: [PATCH] hack tinyxml to save/load string prefs win/linux --- src/tools/prefs.win.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/tools/prefs.win.cpp b/src/tools/prefs.win.cpp index e0f10905..cb676522 100644 --- a/src/tools/prefs.win.cpp +++ b/src/tools/prefs.win.cpp @@ -92,7 +92,11 @@ static bool save_prefs(fs::path fpath) { fout << kv.first << " = " << boost::any_cast(kv.second) << std::endl; else if(kv.second.type() == typeid(double)) fout << kv.first << " = " << std::fixed << boost::any_cast(kv.second) << std::endl; - else printf("Warning: Unknown preference value type, skipping...\n"); + else if(kv.second.type() == typeid(std::string)){ + std::string encoded; + TiXmlBase::EncodeString(boost::any_cast(kv.second), &encoded); + fout << kv.first << " = \"" << encoded << "\"" << std::endl; + }else printf("Warning: Unknown preference value type, skipping...\n"); if(!fout) { perror("Error writing preferences"); return false; @@ -134,7 +138,15 @@ static bool load_prefs(std::istream& in) { std::string key = line.substr(0, key_end + 1), val = line.substr(val_beg); if(val == "true") temp_prefs[key] = true; else if(val == "false") temp_prefs[key] = false; - else if(val[0] == '[') { + else if(val[0] == '"') { + // Dastardly trick to make TinyXML encode and decode string preferences + std::ostringstream dummy_xml; + dummy_xml << "" << val.substr(1, val.size() - 2) << ""; + TiXmlElement text_element(""); + text_element.Parse(dummy_xml.str().c_str(), nullptr, TIXML_ENCODING_UTF8); + std::string decoded = text_element.GetText(); + temp_prefs[key] = decoded; + } else if(val[0] == '[') { int arr_end = val.find_first_of(']'); std::istringstream arr_vals(val.substr(1, arr_end - 1)); int i = 0;