forked from IENT/YUView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plistserializer.cpp
executable file
·101 lines (93 loc) · 3.73 KB
/
plistserializer.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
/* 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 "plistserializer.h"
// Qt includes
#include <QDomElement>
#include <QDomDocument>
#include <QDate>
#include <QDateTime>
static QDomElement textElement(QDomDocument& doc, const char *tagName, QString contents) {
QDomElement tag = doc.createElement(QString::fromLatin1(tagName));
tag.appendChild(doc.createTextNode(contents));
return tag;
}
static QDomElement serializePrimitive(QDomDocument &doc, const QVariant &variant) {
QDomElement result;
if (variant.type() == QVariant::Bool) {
result = doc.createElement(variant.toBool() ? QStringLiteral("true") : QStringLiteral("false"));
}
else if (variant.type() == QVariant::Date) {
result = textElement(doc, "date", variant.toDate().toString(Qt::ISODate));
}
else if (variant.type() == QVariant::DateTime) {
result = textElement(doc, "date", variant.toDateTime().toString(Qt::ISODate));
}
else if (variant.type() == QVariant::ByteArray) {
result = textElement(doc, "data", QString::fromLatin1(variant.toByteArray().toBase64()));
}
else if (variant.type() == QVariant::String) {
result = textElement(doc, "string", variant.toString());
}
else if (variant.type() == QVariant::Int) {
result = textElement(doc, "integer", QString::number(variant.toInt()));
}
else if (variant.canConvert(QVariant::Double)) {
QString num;
num.setNum(variant.toDouble());
result = textElement(doc, "real", num);
}
return result;
}
QDomElement PListSerializer::serializeElement(QDomDocument &doc, const QVariant &variant) {
if (variant.type() == QVariant::Map) {
return serializeMap(doc, variant.toMap());
}
else if (variant.type() == QVariant::List) {
return serializeList(doc, variant.toList());
}
else {
return serializePrimitive(doc, variant);
}
}
QDomElement PListSerializer::serializeList(QDomDocument &doc, const QVariantList &list) {
QDomElement element = doc.createElement(QStringLiteral("array"));
foreach(QVariant item, list) {
element.appendChild(serializeElement(doc, item));
}
return element;
}
QDomElement PListSerializer::serializeMap(QDomDocument &doc, const QVariantMap &map) {
QDomElement element = doc.createElement(QStringLiteral("dict"));
QList<QString> keys = map.keys();
foreach(QString key, keys) {
QDomElement keyElement = textElement(doc, "key", key);
element.appendChild(keyElement);
element.appendChild(serializeElement(doc, map[key]));
}
return element;
}
QString PListSerializer::toPList(const QVariant &variant) {
QDomDocument document(QStringLiteral("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\""));
document.appendChild(document.createProcessingInstruction(QStringLiteral("xml"), QStringLiteral("version=\"1.0\" encoding=\"UTF-8\"")));
QDomElement plist = document.createElement(QStringLiteral("plist"));
plist.setAttribute(QStringLiteral("version"), QStringLiteral("1.0"));
document.appendChild(plist);
plist.appendChild(serializeElement(document, variant));
return document.toString();
}