Working on a Morrowind.ini reader
parent
30710bad70
commit
8ea31e5050
@ -0,0 +1,68 @@
|
||||
#include "inisettings.hpp"
|
||||
|
||||
|
||||
#include <QTextStream>
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
#include <QRegExp>
|
||||
#include <QDebug>
|
||||
|
||||
Wizard::IniSettings::IniSettings()
|
||||
{
|
||||
}
|
||||
|
||||
Wizard::IniSettings::~IniSettings()
|
||||
{
|
||||
}
|
||||
|
||||
bool Wizard::IniSettings::readFile(QTextStream &stream)
|
||||
{
|
||||
// Look for a square bracket, "'\\["
|
||||
// that has one or more "not nothing" in it, "([^]]+)"
|
||||
// and is closed with a square bracket, "\\]"
|
||||
QRegExp sectionRe("^\\[([^]]+)\\]");
|
||||
|
||||
// Find any character(s) that is/are not equal sign(s), "[^=]+"
|
||||
// followed by an optional whitespace, an equal sign, and another optional whitespace, "\\s*=\\s*"
|
||||
// and one or more periods, "(.+)"
|
||||
QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$");
|
||||
|
||||
QString currentSection;
|
||||
|
||||
while (!stream.atEnd())
|
||||
{
|
||||
const QString &line = stream.readLine();
|
||||
|
||||
if (line.isEmpty() || line.startsWith(";"))
|
||||
continue;
|
||||
|
||||
if (sectionRe.exactMatch(line))
|
||||
{
|
||||
currentSection = sectionRe.cap(1);
|
||||
}
|
||||
else if (keyRe.indexIn(line) != -1)
|
||||
{
|
||||
QString key = keyRe.cap(1).trimmed();
|
||||
QString value = keyRe.cap(2).trimmed();
|
||||
|
||||
// Append the section, but only if there is one
|
||||
if (!currentSection.isEmpty())
|
||||
key = currentSection + QLatin1Char('/') + key;
|
||||
|
||||
mSettings[key] = QVariant(value);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Wizard::IniSettings::writeFile(QTextStream &stream)
|
||||
{
|
||||
qDebug() << "test! " << stream.readAll();
|
||||
|
||||
while (!stream.atEnd()) {
|
||||
qDebug() << "test! " << stream.readLine();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
#ifndef INISETTINGS_HPP
|
||||
#define INISETTINGS_HPP
|
||||
|
||||
#include <QHash>
|
||||
#include <QVariant>
|
||||
|
||||
class QTextStream;
|
||||
|
||||
namespace Wizard
|
||||
{
|
||||
|
||||
typedef QHash<QString, QVariant> SettingsMap;
|
||||
|
||||
class IniSettings
|
||||
{
|
||||
public:
|
||||
explicit IniSettings();
|
||||
~IniSettings();
|
||||
|
||||
inline QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const
|
||||
{
|
||||
return mSettings.value(key, defaultValue);
|
||||
}
|
||||
|
||||
inline void setValue(const QString &key, const QVariant &value)
|
||||
{
|
||||
mSettings.insert(key, value);
|
||||
}
|
||||
|
||||
inline void remove(const QString &key)
|
||||
{
|
||||
mSettings.remove(key);
|
||||
}
|
||||
|
||||
bool readFile(QTextStream &stream);
|
||||
bool writeFile(QTextStream &stream);
|
||||
|
||||
private:
|
||||
|
||||
SettingsMap mSettings;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // INISETTINGS_HPP
|
Loading…
Reference in New Issue