reader_srt.cpp
3.61 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
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
/***************************************************************************
* Universidade Federal da Paraíba *
* Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital *
* *
* Centro de Informática - UFPB - Campus I *
* João Pessoa - PB - Brasil *
* *
* Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) *
* Date: Qui Out 24 22:26:35 BRT 2013 *
* *
**************************************************************************/
#include "reader_srt.h"
namespace sndesc {
ReaderSRT::ReaderSRT(std::string _filepath, FileIO::open_flags_t _mode) {
file = new lavidlib::File(_filepath);
try{
file_io = new lavidlib::FileIO(file->getPath(), _mode);
}catch(lavidlib::IOException){
throw ReaderException("[ERRO: reader_srt.cpp] Arquivo de legenda não encontrado.");
}
seek_pos = 0;
hasNextSub = true;
}
ReaderSRT::~ReaderSRT() {
if (bff_reader != NULL) delete bff_reader;
if (file_io != NULL) delete file_io;
}
Subtitle* ReaderSRT::next() {
if (seek_pos >= file_io->getSize())
throw ReaderException("[ERRO: reader_srt.cpp] Esse arquivo já foi lido.");
file_io->seek(seek_pos);
try{
bff_reader = new BufferedReader(file_io);
}catch(lavidlib::IOException){
throw ReaderException("[ERRO: reader_srt.cpp] O BufferedReader não foi inicializado.");
}
Subtitle* sub = new Subtitle();
std::string line = "";
std::string text_sub = "";
try {
/* ID */
int id = 0;
line = bff_reader->readLine();
seek_pos += (int64_t) line.size() + SIZE_CSCAPE;
id = atoi(line.c_str());
sub->setID(id);
/* TimeIn and TimeOut */
int64_t t_in = 0, t_out = 0;
line = bff_reader->readLine();
seek_pos += (int64_t) line.size() + SIZE_CSCAPE;
int target_pos = line.find(TARGET_TIME);
t_in = str_to_time(line.substr(0, target_pos));
sub->setTimeIn(t_in);
t_out = str_to_time(line.substr(target_pos + strlen(TARGET_TIME)+1, line.size()));
sub->setTimeOut(t_out);
/* Text: read until line be empty */
while ((line = bff_reader->readLine()).size() > 0) {
text_sub += line;
text_sub.append(" ");
}
seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE;
} catch (lavidlib::EOFException &ex) {
sub->setSubtitleText(text_sub);
sub->setStatusOfReady(true);
delete(bff_reader);
seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE;
hasNextSub = false;
return sub;
}
sub->setSubtitleText(text_sub);
sub->setStatusOfReady(true);
delete(bff_reader);
return sub;
}
bool ReaderSRT::hasNextSubtitle() {
return hasNextSub;
}
int64_t ReaderSRT::str_to_time(std::string str_time) {
int64_t ttime = 0;
char* tokens = new char[4]; // hh, mm, ss, ms
strcpy(tokens, (char*)str_time.c_str());
int index = 0;
int values [4]; // hh, mm, ss, ms
char * str = strtok(tokens, ":,");
while (str != NULL) {
values[index] = atoi(str);
str = strtok(NULL, ":,");
index++;
}
delete(tokens);
/* calculate time */
ttime = /*hour to sec*/((((values[0] * 60) * 60) +
/*min to sec*/(values[1] * 60) +/*sec*/values[2])*1000) + values[3];
return ttime;
}
}