1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 17:29:55 +00:00
openmw/components/config/settingsbase.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
2.9 KiB
C++
Raw Normal View History

#ifndef SETTINGSBASE_HPP
#define SETTINGSBASE_HPP
#include <QRegExp>
#include <QString>
#include <QStringList>
#include <QTextStream>
namespace Config
{
2013-10-25 16:17:26 +00:00
template <class Map>
class SettingsBase
{
2013-10-25 16:17:26 +00:00
public:
SettingsBase() { mMultiValue = false; }
2020-10-22 22:03:14 +00:00
~SettingsBase() = default;
inline QString value(const QString& key, const QString& defaultValue = QString()) const
2013-10-25 16:17:26 +00:00
{
return mSettings.value(key).isEmpty() ? defaultValue : mSettings.value(key);
}
2013-10-25 16:17:26 +00:00
inline void setValue(const QString& key, const QString& value) { mSettings.replace(key, value); }
2013-10-25 16:17:26 +00:00
inline void setMultiValue(const QString& key, const QString& value)
{
QStringList values = mSettings.values(key);
if (!values.contains(value))
2020-06-24 10:51:26 +00:00
mSettings.insert(key, value);
2013-10-25 16:17:26 +00:00
}
2013-10-25 16:17:26 +00:00
inline void setMultiValueEnabled(bool enable) { mMultiValue = enable; }
2013-10-25 16:17:26 +00:00
inline void remove(const QString& key) { mSettings.remove(key); }
Map getSettings() const { return mSettings; }
2013-10-25 16:17:26 +00:00
bool readFile(QTextStream& stream)
{
Map cache;
2013-10-25 16:17:26 +00:00
QString sectionPrefix;
2013-05-08 01:33:42 +00:00
2013-10-25 16:17:26 +00:00
QRegExp sectionRe("^\\[([^]]+)\\]");
QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$");
2013-10-25 16:17:26 +00:00
while (!stream.atEnd())
{
QString line = stream.readLine();
2013-10-25 16:17:26 +00:00
if (line.isEmpty() || line.startsWith("#"))
continue;
2013-10-25 16:17:26 +00:00
if (sectionRe.exactMatch(line))
{
sectionPrefix = sectionRe.cap(1);
sectionPrefix.append("/");
continue;
}
2013-10-25 16:17:26 +00:00
if (keyRe.indexIn(line) != -1)
{
2013-10-25 16:17:26 +00:00
QString key = keyRe.cap(1).trimmed();
QString value = keyRe.cap(2).trimmed();
2013-10-25 16:17:26 +00:00
if (!sectionPrefix.isEmpty())
key.prepend(sectionPrefix);
2013-10-25 16:17:26 +00:00
mSettings.remove(key);
QStringList values = cache.values(key);
2013-10-25 16:17:26 +00:00
if (!values.contains(value))
{
if (mMultiValue)
{
2020-06-24 10:51:26 +00:00
cache.insert(key, value);
2013-10-25 16:17:26 +00:00
}
else
{
2020-06-24 10:51:26 +00:00
cache.remove(key);
cache.insert(key, value);
2013-10-25 16:17:26 +00:00
}
}
}
}
2013-10-25 16:17:26 +00:00
if (mSettings.isEmpty())
{
mSettings = cache; // This is the first time we read a file
2013-10-25 16:17:26 +00:00
return true;
}
// Merge the changed keys with those which didn't
mSettings.unite(cache);
return true;
}
void clear() { mSettings.clear(); }
2013-10-25 16:17:26 +00:00
private:
Map mSettings;
2013-10-25 16:17:26 +00:00
bool mMultiValue;
};
}
#endif // SETTINGSBASE_HPP