forked from IENT/YUView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplistparser.cpp
executable file
·116 lines (110 loc) · 3.48 KB
/
plistparser.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* YUView - YUV player with advanced analytics toolset
* Copyright (C) 2015 Institut für Nachrichtentechnik
* RWTH Aachen University, GERMANY
*
* YUView is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* YUView is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YUView. If not, see <http://www.gnu.org/licenses/>.
*/
// Own includes
#include "plistparser.h"
// Qt includes
#include <QDomNode>
#include <QDomDocument>
#include <QDateTime>
#include <QDebug>
QVariant PListParser::parsePList(QIODevice *device) {
QVariantMap result;
QDomDocument doc;
QString errorMessage;
int errorLine;
int errorColumn;
bool success = doc.setContent(device, false, &errorMessage, &errorLine, &errorColumn);
if (!success) {
qDebug() << "PListParser Warning: Could not parse PList file!";
qDebug() << "Error message: " << errorMessage;
qDebug() << "Error line: " << errorLine;
qDebug() << "Error column: " << errorColumn;
return result;
}
QDomElement root = doc.documentElement();
if (root.attribute(QStringLiteral("version"), QStringLiteral("1.0")) != QLatin1String("1.0")) {
qDebug() << "PListParser Warning: plist is using an unknown format version, parsing might fail unexpectedly";
}
return parseElement(root.firstChild().toElement());
}
QVariant PListParser::parseElement(const QDomElement &e) {
QString tagName = e.tagName();
QVariant result;
if (tagName == QLatin1String("dict")) {
result = parseDictElement(e);
}
else if (tagName == QLatin1String("array")) {
result = parseArrayElement(e);
}
else if (tagName == QLatin1String("string")) {
result = e.text();
}
else if (tagName == QLatin1String("data")) {
result = QByteArray::fromBase64(e.text().toUtf8());
}
else if (tagName == QLatin1String("integer")) {
result = e.text().toInt();
}
else if (tagName == QLatin1String("real")) {
result = e.text().toFloat();
}
else if (tagName == QLatin1String("true")) {
result = true;
}
else if (tagName == QLatin1String("false")) {
result = false;
}
else if (tagName == QLatin1String("date")) {
result = QDateTime::fromString(e.text(), Qt::ISODate);
}
else {
qDebug() << "PListParser Warning: Invalid tag found: " << e.tagName() << e.text();
}
return result;
}
QVariantList PListParser::parseArrayElement(const QDomElement& element) {
QVariantList result;
QDomNodeList children = element.childNodes();
for (int i = 0; i < children.count(); i++) {
QDomNode child = children.at(i);
QDomElement e = child.toElement();
if (!e.isNull()) {
result.append(parseElement(e));
}
}
return result;
}
QVariantMap PListParser::parseDictElement(const QDomElement& element) {
QVariantMap result;
QDomNodeList children = element.childNodes();
QString currentKey;
for (int i = 0; i < children.count(); i++) {
QDomNode child = children.at(i);
QDomElement e = child.toElement();
if (!e.isNull()) {
QString tagName = e.tagName();
if (tagName == QLatin1String("key")) {
currentKey = e.text();
}
else if (!currentKey.isEmpty()) {
result[currentKey] = parseElement(e);
}
}
}
return result;
}