Fix issues with badly encoded escape sequences in config files.

This commit is contained in:
Petr Mrázek
2014-03-29 21:16:54 +01:00
parent 902dc50c87
commit 5f7a48a35e
5 changed files with 88 additions and 11 deletions

View File

@ -25,17 +25,49 @@ INIFile::INIFile()
QString INIFile::unescape(QString orig)
{
orig.replace("\\n", "\n");
orig.replace("\\t", "\t");
orig.replace("\\\\", "\\");
return orig;
QString out;
QChar prev = 0;
for(auto c: orig)
{
if(prev == '\\')
{
if(c == 'n')
out += '\n';
else if (c == 't')
out += '\t';
else
out += c;
prev = 0;
}
else
{
if(c == '\\')
{
prev = c;
continue;
}
out += c;
prev = 0;
}
}
return out;
}
QString INIFile::escape(QString orig)
{
orig.replace("\\", "\\\\");
orig.replace("\n", "\\n");
orig.replace("\t", "\\t");
return orig;
QString out;
for(auto c: orig)
{
if(c == '\n')
out += "\\n";
else if (c == '\t')
out += "\\t";
else if(c == '\\')
out += "\\\\";
else
out += c;
}
return out;
}
bool INIFile::saveFile(QString fileName)

View File

@ -33,6 +33,6 @@ public:
QVariant get(QString key, QVariant def) const;
void set(QString key, QVariant val);
QString unescape(QString orig);
QString escape(QString orig);
static QString unescape(QString orig);
static QString escape(QString orig);
};