OSDN Git Service

IniOptionsMgr: Fix the problem that it cannot be read the value from the INI file...
authorTakashi Sawanaka <sdottaka@users.sourceforge.net>
Thu, 6 May 2021 13:45:28 +0000 (22:45 +0900)
committerTakashi Sawanaka <sdottaka@users.sourceforge.net>
Thu, 6 May 2021 13:45:28 +0000 (22:45 +0900)
Src/Common/IniOptionsMgr.cpp
Src/Common/OptionsMgr.cpp
Src/Common/OptionsMgr.h

index 257e7dd..a213533 100644 (file)
@@ -155,7 +155,7 @@ int CIniOptionsMgr::SaveOption(const String& name)
        {
                if (valType == varprop::VT_STRING)
                {
-                       String strVal = value.GetString();
+                       String strVal = EscapeValue(value.GetString());
                        LPCWSTR text = strVal.c_str();
                        WritePrivateProfileString(lpAppName, name.c_str(), text, GetFilePath());
                }
@@ -282,7 +282,8 @@ String CIniOptionsMgr::ReadValueFromFile(const String& name)
                                        break;
                                ++v;
                                size_t vlen = _tcslen(v);
-                               m_iniFileKeyValues.insert_or_assign(String{ p, v - 1 }, String{v, v + vlen});
+                               String value{ v, v + vlen };
+                               m_iniFileKeyValues.insert_or_assign(String{ p, v - 1 }, UnescapeValue(value));
                                p = v + vlen + 1;
                        }
                }
index e521ebf..fee124a 100644 (file)
@@ -670,7 +670,7 @@ int COptionsMgr::ExportOptions(const String& filename, const bool bHexColor /*=
                }
                else if (value.GetType() == varprop::VT_STRING)
                {
-                       strVal = value.GetString();
+                       strVal = EscapeValue(value.GetString());
                }
 
                bool bRet = !!WritePrivateProfileString(_T("WinMerge"), name.c_str(),
@@ -728,8 +728,9 @@ int COptionsMgr::ImportOptions(const String& filename)
                {
                        TCHAR strVal[MAX_PATH_FULL] = {0};
                        GetPrivateProfileString(_T("WinMerge"), pKey, _T(""), strVal, MAX_PATH_FULL, filename.c_str());
-                       value.SetString(strVal);
-                       SaveOption(pKey, strVal);
+                       String sVal = UnescapeValue(strVal);
+                       value.SetString(sVal);
+                       SaveOption(pKey, sVal);
                }
                Set(pKey, value);
 
@@ -742,3 +743,39 @@ int COptionsMgr::ImportOptions(const String& filename)
        }
        return retVal;
 }
+
+String COptionsMgr::EscapeValue(const String& text)
+{
+       String text2;
+       for (int i = 0; i < text.length(); ++i)
+       {
+               TCHAR ch = text[i];
+               if (ch == '\0' || ch == '\x1b' || ch == '\r' || ch == '\n')
+               {
+                       text2 += '\x1b';
+                       text2 += ch + '@';
+               }
+               else
+                       text2 += ch;
+       }
+       return text2;
+}
+
+String COptionsMgr::UnescapeValue(const String& text)
+{
+       if (text.find('\x1b') == String::npos)
+               return text;
+       String text2;
+       for (int i = 0; i < text.length(); ++i)
+       {
+               if (text[i] == '\x1b' && i < text.length() - 1)
+               {
+                       ++i;
+                       text2 += text[i] - '@';
+               }
+               else
+                       text2 += text[i];
+       }
+       return text2;
+}
+
index 99c44ee..0b63cea 100644 (file)
@@ -152,6 +152,9 @@ public:
        virtual void SetSerializing(bool serializing=true) = 0;
 
 protected:
+       static String EscapeValue(const String& text);
+       static String UnescapeValue(const String& text);
+
        OptionsMap m_optionsMap; /**< Map where options are stored. */
 
 private: