This repository was archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOpenMWConfigInterface.cpp
More file actions
74 lines (63 loc) · 1.45 KB
/
OpenMWConfigInterface.cpp
File metadata and controls
74 lines (63 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "OpenMWConfigInterface.h"
OpenMWConfigInterface::OpenMWConfigInterface(const QString& configFilePath)
{
setConfigPath(configFilePath);
load();
}
OpenMWConfigInterface::~OpenMWConfigInterface()
{
save();
}
void OpenMWConfigInterface::insert(const QString& key, const QVariant& value)
{
getByKey(key).push_back(value);
}
QVector<QVariant>& OpenMWConfigInterface::getByKey(const QString &key)
{
if (!data.contains(key))
{
data[key] = QVector<QVariant>();
}
return data[key];
}
void OpenMWConfigInterface::save()
{
QFile cfgFile(cfgPath);
if (!cfgFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
qWarning("Couldn't open OpenMW config file for writing.");
return;
}
foreach(const QString& key, data.keys())
{
foreach(const QVariant& value, data[key])
{
QString line = key + "=" + value.toString() + '\n';
cfgFile.write(line.toUtf8());
}
}
}
void OpenMWConfigInterface::load()
{
// Clear current map.
data.clear();
// Load json from settings file.
QFile cfgFile(cfgPath);
if (!cfgFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning("Couldn't open OpenMW config file for reading.");
return;
}
QString line;
line = cfgFile.readLine();
while (!line.isEmpty())
{
int splitIndex = line.indexOf('=');
insert(line.left(splitIndex).trimmed(), line.right(line.length()-splitIndex-1).trimmed());
line = cfgFile.readLine();
}
}
void OpenMWConfigInterface::setConfigPath(const QString& path)
{
cfgPath = path;
}