GH-1813 Escape # in INI (and better reader)

This commit is contained in:
janrupf 2019-06-17 16:38:32 +02:00 committed by Petr Mrázek
parent ce12f1a734
commit f87c890912

View File

@ -28,49 +28,20 @@ INIFile::INIFile()
QString INIFile::unescape(QString orig) QString INIFile::unescape(QString orig)
{ {
QString out; return orig
QChar prev = 0; .replace("\\#", "#")
for(auto c: orig) .replace("\\t", "\t")
{ .replace("\\n", "\n")
if(prev == '\\') .replace("\\\\", "\\");
{
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) QString INIFile::escape(QString orig)
{ {
QString out; return orig
for(auto c: orig) .replace('\\', "\\\\")
{ .replace('\n', "\\n")
if(c == '\n') .replace('\t', "\\t")
out += "\\n"; .replace('#', "\\#");
else if (c == '\t')
out += "\\t";
else if(c == '\\')
out += "\\\\";
else
out += c;
}
return out;
} }
bool INIFile::saveFile(QString fileName) bool INIFile::saveFile(QString fileName)
@ -120,7 +91,15 @@ bool INIFile::loadFile(QByteArray file)
{ {
QString &lineRaw = lines[i]; QString &lineRaw = lines[i];
// Ignore comments. // Ignore comments.
QString line = lineRaw.left(lineRaw.indexOf('#')).trimmed(); int commentIndex = 0;
QString line = lineRaw;
// Search for comments until no more escaped # are available
while((commentIndex = line.indexOf('#', commentIndex + 1)) != -1) {
if(commentIndex > 0 && line.at(commentIndex - 1) == '\\') {
continue;
}
line = line.left(lineRaw.indexOf('#')).trimmed();
}
int eqPos = line.indexOf('='); int eqPos = line.indexOf('=');
if (eqPos == -1) if (eqPos == -1)