1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-20 07:23:53 +00:00
openmw/components/config/settingsbase.hpp

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

111 lines
3.1 KiB
C++
Raw Normal View History

#ifndef SETTINGSBASE_HPP
#define SETTINGSBASE_HPP
#include <QRegularExpression>
#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
QRegularExpression sectionRe("^\\[([^]]+)\\]$");
QRegularExpression 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;
QRegularExpressionMatch sectionMatch = sectionRe.match(line);
if (sectionMatch.hasMatch())
2013-10-25 16:17:26 +00:00
{
sectionPrefix = sectionMatch.captured(1);
2013-10-25 16:17:26 +00:00
sectionPrefix.append("/");
continue;
}
QRegularExpressionMatch match = keyRe.match(line);
if (match.hasMatch())
2013-10-25 16:17:26 +00:00
{
QString key = match.captured(1).trimmed();
QString value = match.captured(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