X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Finifile.cpp;h=838ff723630f057cb4a7e750a2f229877df1c83b;hb=602faa7b96d4467f127ccd581228ca7245283c32;hp=28e700f33c5b1c199cefbd44f94b459bcf77cd05;hpb=6230ebd0a05134031e52001c15473c3c3e6c7c1b;p=evaf
diff --git a/src/libs/Common/inifile.cpp b/src/libs/Common/inifile.cpp
index 28e700f..838ff72 100644
--- a/src/libs/Common/inifile.cpp
+++ b/src/libs/Common/inifile.cpp
@@ -56,12 +56,12 @@ QString IniFile::errorString() const
return d->errorString();
}
-QVariant IniFile::getValue(QString const & paramName, QVariant const & defaultValue)
+QVariant IniFile::getValue(QByteArray const & paramName, QVariant const & defaultValue)
{
return d->getValue(paramName, defaultValue);
}
-bool IniFile::setValue(QString const & paramName, QVariant const & value)
+bool IniFile::setValue(QByteArray const & paramName, QVariant const & value)
{
return d->setValue(paramName, value);
}
@@ -99,7 +99,7 @@ IniFileImpl::~IniFileImpl()
void IniFileImpl::updateCache(quint64 pos, qint64 diff)
{
// Walk through all the sections in the cache
- QHash >::const_iterator it;
+ QHash >::const_iterator it;
for (it = mCache.constBegin(); it != mCache.constEnd(); ++it) {
QExplicitlySharedDataPointer sectionObject = *it;
@@ -108,7 +108,7 @@ void IniFileImpl::updateCache(quint64 pos, qint64 diff)
sectionObject->filePos += diff;
// Update individual values in the section that come after the current file offset
- QHash >::const_iterator it1;
+ QHash >::const_iterator it1;
for (it1 = sectionObject->values.constBegin(); it1 != sectionObject->values.constEnd(); ++it1) {
QExplicitlySharedDataPointer valueObject = *it1;
if (valueObject->filePos > pos)
@@ -117,7 +117,7 @@ void IniFileImpl::updateCache(quint64 pos, qint64 diff)
}
}
-QExplicitlySharedDataPointer IniFileImpl::getSection(QFile & file, QString const & sectionName)
+QExplicitlySharedDataPointer IniFileImpl::getSection(QFile & file, QByteArray const & sectionName)
{
// Check for external modifications
QFileInfo fi(file);
@@ -128,7 +128,7 @@ QExplicitlySharedDataPointer IniFileImpl::getSection(QFile & fil
}
// Look for the section in the cache first
- QHash >::const_iterator it = mCache.constFind(sectionName.toLower());
+ QHash >::const_iterator it = mCache.constFind(sectionName.toLower());
if (it != mCache.constEnd()) {
// Found in the cache
if (mValid)
@@ -138,7 +138,7 @@ QExplicitlySharedDataPointer IniFileImpl::getSection(QFile & fil
// Read the INI file and look for the section
while (mValid && !file.atEnd()) {
- QString line = file.readLine().trimmed();
+ QByteArray line = file.readLine().trimmed();
// Ignore the line if it is empty, a comment or not a section name
if (line.isEmpty() || line.startsWith(';') || line.startsWith('#') || !line.startsWith('['))
@@ -150,7 +150,7 @@ QExplicitlySharedDataPointer IniFileImpl::getSection(QFile & fil
continue;
// Is this the section that we are looking for?
- if (line.mid(1, idx - 1).compare(sectionName, Qt::CaseInsensitive) == 0) {
+ if (qstricmp(line.mid(1, idx - 1).constData(), sectionName.constData()) == 0) {
// Create the section object and add to the cache
QExplicitlySharedDataPointer sectionObject(new IniFileSection(file.pos()));
sectionObject->name = sectionName.toLower();
@@ -166,10 +166,10 @@ QExplicitlySharedDataPointer IniFileImpl::getSection(QFile & fil
return QExplicitlySharedDataPointer();
}
-QExplicitlySharedDataPointer IniFileImpl::getParameter(QFile & file, IniFileSection & section, QString const & paramName)
+QExplicitlySharedDataPointer IniFileImpl::getParameter(QFile & file, IniFileSection & section, QByteArray const & paramName)
{
// Look for the parameter in the cache first
- QHash >::const_iterator it = section.values.constFind(paramName.toLower());
+ QHash >::const_iterator it = section.values.constFind(paramName.toLower());
if (it != section.values.constEnd()) {
// Found it in the cache
if (mValid)
@@ -183,7 +183,7 @@ QExplicitlySharedDataPointer IniFileImpl::getParameter(QFile & fil
// Current file position
quint64 currentPos = file.pos();
- QString line = file.readLine().trimmed();
+ QByteArray line = file.readLine().trimmed();
// Ignore the line if it is empty or a comment
if (line.isEmpty() || line.startsWith(';') || line.startsWith('#'))
@@ -202,8 +202,8 @@ QExplicitlySharedDataPointer IniFileImpl::getParameter(QFile & fil
if (idx == -1)
continue;
- QString name = line.mid(0, idx).trimmed().toLower();
- QString value = line.mid(idx + 1).trimmed();
+ QByteArray name = line.mid(0, idx).trimmed().toLower();
+ QByteArray value = line.mid(idx + 1).trimmed();
// Check for the 'windows:' or 'linux:' prefix in the parameter name
bool thisOsOnly = false;
@@ -226,11 +226,12 @@ QExplicitlySharedDataPointer IniFileImpl::getParameter(QFile & fil
// If the parameter value is not in the cache, add it to the cache
QExplicitlySharedDataPointer valueObject;
- QHash >::const_iterator it = section.values.constFind(name);
+ QHash >::const_iterator it = section.values.constFind(name);
if (it == section.values.constEnd()) {
valueObject = new IniFileValue(currentPos);
valueObject->name = name;
valueObject->paramValue = value;
+ valueObject->thisOsOnly = thisOsOnly;
section.values.insert(name, valueObject);
}
else {
@@ -238,10 +239,11 @@ QExplicitlySharedDataPointer IniFileImpl::getParameter(QFile & fil
valueObject = *it;
valueObject->name = name;
valueObject->paramValue = value;
+ valueObject->thisOsOnly = thisOsOnly;
}
// Is this the parameter vwe are looking for?
- if (name.compare(paramName, Qt::CaseInsensitive) == 0) {
+ if (qstricmp(name.constData(), paramName.constData()) == 0) {
// Rewind to the beginning of the line
file.seek(currentPos);
@@ -254,16 +256,16 @@ QExplicitlySharedDataPointer IniFileImpl::getParameter(QFile & fil
return QExplicitlySharedDataPointer();
}
-QVariant IniFileImpl::getValue(QString const & paramName, QVariant const & defaultValue)
+QVariant IniFileImpl::getValue(QByteArray const & paramName, QVariant const & defaultValue)
{
// Locate the '/' character that separates section names from key names
- int idx = paramName.indexOf('/');
+ int idx = paramName.lastIndexOf('/');
if (idx < 0)
return defaultValue;
// Separate section and key names
- QString section = paramName.left(idx);
- QString key = paramName.mid(idx + 1);
+ QByteArray section = paramName.left(idx);
+ QByteArray key = paramName.mid(idx + 1);
// Open the file
QFile f(mFileName);
@@ -287,40 +289,88 @@ QVariant IniFileImpl::getValue(QString const & paramName, QVariant const & defau
if (f.isOpen())
f.close();
- return toVariant(valueObject->paramValue, defaultValue);
+ // Return the cached value if it is already set and the type is the same than the default value type
+ if (valueObject->value.isValid() && (!defaultValue.isValid() || valueObject->value.type() == defaultValue.type()))
+ return valueObject->value;
+
+ // Convert to the proper type
+ if (defaultValue.type() == QVariant::ByteArray || defaultValue.type() == QVariant::String) {
+ // Remove single and double quotes
+ QByteArray v = valueObject->paramValue;
+ if (v.startsWith('\"')) {
+ v.remove(0, 1);
+ if (v.endsWith('\"'))
+ v.remove(v.size() - 1, 1);
+ }
+ else if (v.startsWith('\'')) {
+ v.remove(0, 1);
+ if (v.endsWith('\''))
+ v.remove(v.size() - 1, 1);
+ }
+
+ // Convert from the escaped character array
+ if (defaultValue.type() == QVariant::String)
+ valueObject->value = QVariant(strFromEscapedCharArray(v));
+ else
+ valueObject->value = QVariant(binFromEscapedCharArray(v));
+ }
+ else
+ valueObject->value = toVariant(valueObject->paramValue, defaultValue);
+
+ return valueObject->value;
}
-bool IniFileImpl::setValue(QString const & paramName, QVariant const & value)
+bool IniFileImpl::setValue(QByteArray const & paramName, QVariant const & value)
{
// Locate the '/' character that separates section names from key names
- int idx = paramName.indexOf('/');
+ int idx = paramName.lastIndexOf('/', -1);
if (idx < 0)
return false;
// Separate section and key names
- QString section = paramName.left(idx).toLower();
- QString key = paramName.mid(idx + 1).toLower();
+ QByteArray section = paramName.left(idx).toLower();
+ QByteArray key = paramName.mid(idx + 1).toLower();
// Format the value depending on the type
- QString valueString;
+ QByteArray valueString;
switch (value.type()) {
case QVariant::UInt:
- valueString = "0x" + QString::number(value.toUInt(), 16);
+ valueString = QByteArray("0x").append(QByteArray::number(value.toUInt(), 16));
break;
case QVariant::Int:
- valueString = QString::number(value.toInt());
+ valueString = QByteArray::number(value.toInt());
break;
case QVariant::Double:
- valueString = QString::number(value.toDouble(), 'f');
+ valueString = QByteArray::number(value.toDouble(), 'f');
break;
case QVariant::Bool:
valueString = value.toBool() ? "true" : "false";
break;
- case QVariant::Char:
- valueString = value.toChar();
+ case QVariant::Char: {
+ QChar c = value.toChar();
+ printf("c.unicode() = %u\n", c.unicode());
+ if (c.unicode() < 32 || c.unicode() >= 127)
+ valueString = QByteArray("\\0x").append(QByteArray::number(c.unicode(), 16));
+ else
+ valueString = QByteArray(1, (char const)c.unicode());
+ break;
+ }
+ case QVariant::ByteArray:
+ valueString = binToEscapedCharArray(value.toByteArray());
+ if (valueString.startsWith(' ') || valueString.endsWith(' ')) {
+ valueString.insert(0, '\"');
+ valueString.append('\"');
+ }
+ break;
+ case QVariant::String:
+ valueString = strToEscapedCharArray(value.toString());
+ if (valueString.startsWith(' ') || valueString.endsWith(' ')) {
+ valueString.insert(0, '\"');
+ valueString.append('\"');
+ }
break;
default:
- valueString = value.toString();
+ valueString = value.toString().toLatin1();
}
// Open the file
@@ -341,7 +391,7 @@ bool IniFileImpl::setValue(QString const & paramName, QVariant const & value)
if (!sectionObject) {
// Write the new section to the INI file (the file is already positioned at the end)
- if (f.write(QString("[%1]" EOL).arg(section).toLocal8Bit()) == -1) {
+ if (f.write(QString("[%1]" EOL).arg(section.constData()).toLatin1()) == -1) {
mErrorString = f.errorString();
mValid = false;
EVAF_ERROR("Failed to write to the INI file %s : %s", qPrintable(mFileName), qPrintable(mErrorString));
@@ -356,7 +406,7 @@ bool IniFileImpl::setValue(QString const & paramName, QVariant const & value)
mCache.insert(section.toLower(), sectionObject);
// Write the parameter value to the INI file
- if (f.write(QString("%1 = %2" EOL).arg(key).arg(valueString).toLocal8Bit()) == -1) {
+ if (f.write(QString("%1 = %2" EOL).arg(key.constData()).arg(valueString.constData()).toLatin1()) == -1) {
mErrorString = f.errorString();
mValid = false;
EVAF_ERROR("Failed to write to the INI file %s : %s", qPrintable(mFileName), qPrintable(mErrorString));
@@ -367,6 +417,7 @@ bool IniFileImpl::setValue(QString const & paramName, QVariant const & value)
QExplicitlySharedDataPointer valueObject(new IniFileValue(currentPos));
valueObject->name = key;
valueObject->paramValue = valueString;
+ valueObject->value = value;
sectionObject->values.insert(key, valueObject);
}
@@ -392,7 +443,7 @@ bool IniFileImpl::setValue(QString const & paramName, QVariant const & value)
#ifdef Q_OS_LINUX
prefix = "linux:";
#endif
-#if Q_OS_WIN32
+#ifdef Q_OS_WIN32
prefix = "windows:";
#endif
}
@@ -421,7 +472,7 @@ bool IniFileImpl::setValue(QString const & paramName, QVariant const & value)
// Rewind to the original position and write the new parameter value
f.seek(currentPos);
- if (f.write(QString("%1%2 = %3" EOL).arg(prefix).arg(key).arg(valueString).toLocal8Bit()) == -1) {
+ if (f.write(QString("%1%2 = %3" EOL).arg(prefix).arg(key.constData()).arg(valueString.constData()).toLatin1()) == -1) {
mErrorString = f.errorString();
mValid = false;
EVAF_ERROR("Failed to write to the INI file %s : %s", qPrintable(mFileName), qPrintable(mErrorString));
@@ -455,6 +506,10 @@ bool IniFileImpl::setValue(QString const & paramName, QVariant const & value)
if (diff)
updateCache(currentPos, diff);
+ // Update the parameter value in the internal cache
+ valueObject->paramValue = valueString;
+ valueObject->value = value;
+
}
f.close();