forked from mrkite/TerraFirma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteamconfig.cpp
93 lines (85 loc) · 2.34 KB
/
steamconfig.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* @Copyright 2015 seancode
*
* Handles parsing of the steam config files
*/
#include "./steamconfig.h"
#include <QSettings>
#include <QTextStream>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRegularExpressionMatchIterator>
#include <QStandardPaths>
#include <QDir>
SteamConfig::SteamConfig() {
root = NULL;
QSettings settings("HKEY_CURRENT_USER\\Software\\Valve\\Steam",
QSettings::NativeFormat);
QString path = settings.value("SteamPath").toString();
if (path.isEmpty()) {
path = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation)
.first();
path += QDir::toNativeSeparators("/Steam");
}
path += QDir::toNativeSeparators("/config/config.vdf");
QFile file(path);
if (file.exists())
parse(path);
}
QString SteamConfig::operator[](QString path) const {
if (root == NULL)
return QString();
return root->find(path);
}
void SteamConfig::parse(QString filename) {
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
QList<QString> strings;
QTextStream in(&file);
while (!in.atEnd())
strings.append(in.readLine());
file.close();
root = new Element(&strings);
}
}
SteamConfig::Element::Element() {}
SteamConfig::Element::Element(QList<QString> *lines) {
QString line;
QRegularExpression re("\"([^\"]*)\"");
QRegularExpressionMatchIterator i;
while (lines->length() > 0) {
line = lines->front();
lines->pop_front();
i = re.globalMatch(line);
if (i.hasNext())
break;
}
if (!lines->length()) // corrupt
return;
QRegularExpressionMatch match = i.next();
name = match.captured(1).toLower();
if (i.hasNext()) { // value is a string
match = i.next();
value = match.captured(1);
value.replace("\\\\", "\\");
}
line = lines->front();
if (line.contains("{")) {
lines->pop_front();
while (true) {
line = lines->front();
if (line.contains("}")) { // empty
lines->pop_front();
return;
}
Element e(lines);
children[e.name] = e;
}
}
}
QString SteamConfig::Element::find(QString path) {
int ofs = path.indexOf("/");
if (ofs == -1)
return children[path].value;
return children[path.left(ofs)].find(path.mid(ofs + 1));
}