forked from Subtivals/subtivals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.cpp
91 lines (81 loc) · 2.79 KB
/
script.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
#include <QSettings>
#include <QStringList>
#include <QFile>
#include "script.h"
enum SectionType { SECTION_NONE, SECTION_INFOS, SECTION_STYLES, SECTION_EVENTS };
Script::Script(const QString &p_fileName, QObject *p_parent) :
QObject(p_parent), m_fileName (p_fileName)
{
SectionType section = SECTION_NONE;
// Read and process each line of the input file
QFile file(p_fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd()) {
QString line = QString::fromUtf8(file.readLine());
if (line.contains("[Script Info]")) {
section = SECTION_INFOS;
} else if (line.contains("[V4+ Styles]")) {
section = SECTION_STYLES;
} else if (line.contains("[Events]")) {
section = SECTION_EVENTS;
} else {
line = line.trimmed().replace("\n", "");
if (!line.startsWith(";") && !line.isEmpty()) {
if (section == SECTION_INFOS) {
QList<QString> parts = line.split(':');
if(parts.size() == 2) {
QString key = parts[0].trimmed().toLower();
QString value = parts[1].trimmed();
if (key == "title") {
m_title = value;
}
}
} else if (section == SECTION_STYLES) {
QList<QString> parts = line.split(':');
if(parts.size() == 2) {
QString key = parts[0].trimmed().toLower();
QString value = parts[1].trimmed();
if (key == "style") {
Style *style = new Style(value, this);
m_styles[style->name()] = style;
}
}
} else if (section == SECTION_EVENTS) {
QList<QString> parts = line.split(':');
if(parts.size() > 1) {
QString key = parts[0].trimmed().toLower();
QString value = line.mid(key.length() + 1).trimmed();
if (key == "dialogue") {
m_events.append(new Event(value, this));
}
}
}
}
}
}
}
const QString &Script::fileName() const
{
return m_fileName;
}
const QString &Script::title() const
{
return m_title;
}
const Style *Script::style(const QString &p_name) const
{
return m_styles[p_name];
}
int Script::eventsCount() const
{
return m_events.size();
}
const QListIterator<Event *> Script::events() const
{
return QListIterator<Event *>(m_events);
}
const Event *Script::eventAt(int i) const
{
return m_events[i];
}