-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplistreader.cpp
More file actions
103 lines (87 loc) · 2.17 KB
/
Copy pathplistreader.cpp
File metadata and controls
103 lines (87 loc) · 2.17 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
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
#include "plistreader.h"
#include <QIODevice>
#include <QVariant>
#include <QMap>
PListReader::PListReader()
{
}
bool PListReader::read(QIODevice *device)
{
m_xmlReader.setDevice(device);
if (m_xmlReader.readNextStartElement())
{
if (m_xmlReader.name() == "plist" && m_xmlReader.attributes().value("version") == "1.0")
readPList();
else
m_xmlReader.raiseError("The file is not an PList version 1.0 file.");
}
return m_xmlReader.error();
}
const QList<QVariant> PListReader::data() const
{
return m_data;
}
QString PListReader::errorString() const
{
return QString("%1\nLine %2, column %3")
.arg(m_xmlReader.errorString())
.arg(m_xmlReader.lineNumber())
.arg(m_xmlReader.columnNumber());
}
void PListReader::readPList()
{
Q_ASSERT(m_xmlReader.isStartElement() && m_xmlReader.name() == "plist");
while (m_xmlReader.readNextStartElement())
{
if (m_xmlReader.name() == "array")
readArray(m_data);
else if (m_xmlReader.name() == "dict")
readDict(m_data);
else
m_xmlReader.skipCurrentElement();
}
}
void PListReader::readArray(QList<QVariant> &array)
{
Q_ASSERT(m_xmlReader.isStartElement() && m_xmlReader.name() == "array");
while (m_xmlReader.readNextStartElement())
{
if (m_xmlReader.name() == "array")
{
QList<QVariant> subArray;
readArray(subArray);
array.push_back(subArray);
}
else if (m_xmlReader.name() == "dict")
{
readDict(array);
}
else
{
m_xmlReader.skipCurrentElement();
}
}
}
void PListReader::readDict(QList<QVariant> &array)
{
Q_ASSERT(m_xmlReader.isStartElement() && m_xmlReader.name() == "dict");
QMap<QString, QVariant> dict;
while (m_xmlReader.readNextStartElement())
{
// 这里只处理key,在readKey中,一次默认读取一对键值
if (m_xmlReader.name() == "key")
readKey(dict);
else
m_xmlReader.skipCurrentElement();
}
array.push_back(dict);
}
void PListReader::readKey(QMap<QString, QVariant> &dict)
{
Q_ASSERT(m_xmlReader.isStartElement() && m_xmlReader.name() == "key");
// 这里一次读取一个键值对
QString key = m_xmlReader.readElementText();
Q_ASSERT(m_xmlReader.readNextStartElement());
QString value = m_xmlReader.readElementText();
dict.insertMulti(key, value);
}