-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtitles.cpp
94 lines (84 loc) · 2.5 KB
/
subtitles.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
#include "subtitles.h"
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <iostream>
Subtitles::Subtitles()
{
}
Subtitles::Subtitles(QString filename) {
this->load(filename);
}
void Subtitles::load(QString filename) {
this->subpath = filename;
this->reloadSubtitle();
}
void Subtitles::reloadSubtitle() {
QFile* subfile = new QFile(subpath);
if (subfile->open(QFile::ReadOnly)) {
QTextStream in(subfile);
// NOTE: I expect input to be in CP-1250
// (This program is mainly meant for cz/sk subtitles on Windows)
// TODO: Provide auto detection or picker for encoding.
in.setCodec(QTextCodec::codecForName("CP1250"));
// NOTE: I only support SRT file-format now
// TODO: Implement other format support (possibly through plug-ins)
int ctr = 0;
QString id_str;
QString timestamps_str;
QVector<QString> subtitle_lines;
while (!in.atEnd()) {
QString line = in.readLine();
if (line == "") {
ctr = 0;
SubtitleLine line(id_str, timestamps_str, subtitle_lines);
this->lines.push_back(line);
subtitle_lines.clear();
}
else {
if (ctr == 0) id_str = line;
else if (ctr == 1) timestamps_str = line;
else subtitle_lines.push_back(line);
ctr++;
}
}
if (ctr != 0) {
SubtitleLine line(id_str, timestamps_str, subtitle_lines);
this->lines.push_back(line);
}
subfile->close();
} else {
std::cerr << "File could not be opened!" << std::endl;
}
delete subfile;
}
void Subtitles::saveAs(QString filename) {
QFile* subfile = new QFile(filename);
if (subfile->open(QFile::WriteOnly)) {
QTextStream out(subfile);
// NOTE: Look above if you want to know why cp1250
out.setCodec(QTextCodec::codecForName("CP1250"));
for (SubtitleLine line : lines) {
out << line.getFormatted();
}
subfile->close();
}
delete subfile;
}
void Subtitles::save() {
saveAs(subpath);
}
QString Subtitles::getSubPath() {
return this->subpath;
}
QVector<SubtitleLine>* Subtitles::getLines() {
return &(this->lines);
}
SubtitleLine* Subtitles::getLineByText(QString text) {
for (SubtitleLine &line : lines) {
if (line.getOneLine() == text) {
return &line;
}
}
return NULL;
}