-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubtitle.cpp
106 lines (89 loc) · 2.72 KB
/
Subtitle.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
102
103
104
//
// Created by shahrooz on 10/29/20.
//
#include "Subtitle.h"
#include <algorithm>
#include <iostream>
using namespace std;
Subtitle::Subtitle(const string& file_path) : current_subtitle_index(0){
ifstream in_file(file_path, ios::in);
if(!in_file.is_open()){
cerr << "Cannot open the file \"" << file_path << "\"" << endl;
exit(2);
}
string one_sub_text;
while(!in_file.eof()){
string temp;
getline(in_file, temp);
// Standardization
temp.erase(remove_if(temp.begin(), temp.end(), [](char c) {return !isprint(c);} ), temp.end());
if(temp.empty()){ // subtitle ends
if(one_sub_text.empty()){
continue;
}
this->subtitles.emplace_back(one_sub_text, this);
one_sub_text.clear();
continue;
}
one_sub_text += temp;
one_sub_text += "\n";
}
in_file.close();
}
void Subtitle::reset(){
this->current_subtitle_index = 0;
}
int Subtitle::increase_subtitle_index(){
if(is_finished()){
return EngFlix_Status::END_OF_SUBTITLES;
}
this->current_subtitle_index++;
return EngFlix_Status::OK;
}
int Subtitle::decrease_subtitle_index(){
if(this->current_subtitle_index == 0){
return EngFlix_Status::OK;
}
this->current_subtitle_index--;
return EngFlix_Status::OK;
}
int Subtitle::set_subtitle_index(const uint& indx){
if(indx > subtitles.size()){
return EngFlix_Status::ID_TOO_BIG;
}
this->current_subtitle_index = indx;
return EngFlix_Status::OK;
}
int Subtitle::set_subtitle_index(const time_point<steady_clock, milliseconds>& tp){
int ret = EngFlix_Status::BAD_TIME_POINT;
for(uint i = 0; i < subtitles.size(); i++){
// DPRINTF(DEBUG_Subtitile, "%llu <= %llu\n", tp.time_since_epoch().count(), subtitles[i].get_end_time().time_since_epoch().count());
if(tp < subtitles[i].get_end_time()){
assert(set_subtitle_index(i) == EngFlix_Status::OK);
// DPRINTF(DEBUG_Subtitile, "%llu\n", tp.time_since_epoch().count());
ret = EngFlix_Status::OK;
break;
}
}
return ret;
}
const uint& Subtitle::get_subtitle_index() const{
return this->current_subtitle_index;
}
bool Subtitle::is_finished() const{
if(get_subtitle_index() >= subtitles.size()){
return true;
}
return false;
}
const One_subtitle& Subtitle::get_current_subtitle(){
return subtitles[this->get_subtitle_index()];
}
const One_subtitle& Subtitle::get_subtitle(){
uint t_indx = this->get_subtitle_index();
this->increase_subtitle_index();
return subtitles[t_indx];
}
const vector<One_subtitle> &Subtitle::get_subtitles() const {
return subtitles;
}