-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsnotes.h
131 lines (105 loc) · 3.46 KB
/
psnotes.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef PSNOTES_H
#define PSNOTES_H
#include <QList>
#include <QXmlStreamReader>
#include <QMap>
#include <QSet>
#include <QFile>
#include <QDebug>
struct ps_label_t {
ps_label_t (int id, QString color, QString comment) {
this->id = id;
this->color = color;
this->comment = comment;
}
ps_label_t () {}
QString id;
QString color;
QString comment;
};
class ps_notes_reader_t {
public:
ps_notes_reader_t(const QString & filename) {
m_filename = filename;
}
void read() {
QFile f(m_filename);
f.open(QFile::ReadOnly);
m_xml.setDevice(&f);
if (m_xml.readNextStartElement()) {
if (m_xml.name() == "notes")
read_notes();
else
m_warnings += "Notes root element not found\n";
}
qDebug() << "Read finished, label count = " << m_labels.count() << " | player count " << m_players.count();
}
QList<ps_label_t> * get_labels() { return &m_labels; }
QMap<QString, QSet<QString>> * get_players () { return &m_players; }
QString get_warnings() { return m_warnings; }
bool has_data() {
if (m_labels.size() != 0 && m_players.size() != 0)
return true;
return false;
}
private:
void read_notes() {
Q_ASSERT (m_xml.isStartElement() && m_xml.name() == "notes");
while (m_xml.readNextStartElement()) {
if (m_xml.name() == "labels")
read_labels();
else if (m_xml.name() == "note")
read_note();
else {
m_warnings += "Unknown element found (read_notes) : " + m_xml.name().toString() + "\n";
m_xml.skipCurrentElement();
}
}
}
void read_labels() {
Q_ASSERT (m_xml.isStartElement() && m_xml.name() == "labels");
while (m_xml.readNextStartElement()) {
if (m_xml.name() == "label")
read_label();
else {
m_warnings += "Unknown element found (read_labels) : " + m_xml.name().toString() + "\n";
m_xml.skipCurrentElement();
}
}
}
void read_label() {
Q_ASSERT (m_xml.isStartElement() && m_xml.name() == "label");
if (!m_xml.attributes().hasAttribute("id") ||
!m_xml.attributes().hasAttribute("color"))
{
m_warnings += "Invalid label found in labels\n";
m_xml.skipCurrentElement();
return;
}
ps_label_t label;
label.id = m_xml.attributes().value("id").toString();
label.color = m_xml.attributes().value("color").toString();
label.comment = m_xml.readElementText();
m_labels.append(label);
}
void read_note() {
Q_ASSERT (m_xml.isStartElement() && m_xml.name() == "note");
if(!m_xml.attributes().hasAttribute("player") ||
!m_xml.attributes().hasAttribute("label"))
{
m_warnings += "Invalid note found\n";
m_xml.skipCurrentElement();
return;
}
QString name = m_xml.attributes().value("player").toString().toLower();
QString label = m_xml.attributes().value("label").toString();
m_players [label].insert(name);
m_xml.skipCurrentElement();
}
QString m_filename;
QXmlStreamReader m_xml;
QList<ps_label_t> m_labels;
QMap<QString, QSet<QString>> m_players;
QString m_warnings;
};
#endif // PSNOTES_H