-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrnsl.cpp
113 lines (113 loc) · 2.48 KB
/
trnsl.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
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
unsigned int index = 0;
bool state = 0;
unsigned int a = 1;
unsigned int i = 1;
unsigned int j = 0;
unsigned int sublength = 0;
string print;
string subtitle;
string translated;
vector<unsigned int> timestamps;
int main(int argc, char* argv[]){
ifstream file;
unsigned int chunksize = stoi(argv[2]);
file.open(argv[1]);
if(file.fail()){
cout << "ERROR: Cannot open file, please see if there's a typo somewhere";
return 1;
}
stringstream buffer;
buffer << file.rdbuf();
subtitle = buffer.str();
cout << "Length of subtitle: " << subtitle.length() << '\n';
while(subtitle[i] != '\n'){
i++;
}
while(true){
cout << "\n\033[31mBlock #" << a << "\033[0m\n";
sublength=i+chunksize;
state = 0;
if(sublength > subtitle.length()){
state = 1;
sublength = subtitle.length();
}
while(i < sublength){
if((subtitle[i] == '\n')&&(subtitle[i+1] == '\n')){
i=i+2;
timestamps.push_back(i);
while(subtitle[i] != '\n'){
i++;
}
cout << '\n';
if((sublength + 24) < subtitle.length()){
sublength = sublength + 24;
}else{
sublength = subtitle.length();
}
}
cout << subtitle[i];
i++;
}
i--;
while((subtitle[i]!=' ')&&(subtitle[i]!='\n')){
cout << "\b \b";
i--;
}
i++;
if(state){
break;
}
cout << '\n';
a++;
}
string line;
cout << "\nWhere's the file with the translated version: \n";
getline(cin, line);
ifstream newFile;
newFile.open(line);
if(newFile.fail()){
cout << "ERROR: Cannot open file, please see if there's a typo somewhere";
return 1;
}
stringstream secndbuffer;
secndbuffer << newFile.rdbuf();
translated = secndbuffer.str();
a=0;
i=0;
j=1;
translated.insert(0,1,'\n');
while(subtitle[j]!='\n'){
translated.insert(i,1,subtitle[j]);
i++;
j++;
}
translated.insert(i,1,'\n');
i++;
while(i < translated.length()){
if((translated[i] == '\n')&&(translated[i+1] == '\n')){
i=i+2;
j=timestamps[a];
while(subtitle[j]!='\n'){
translated.insert(i,1,subtitle[j]);
i++;
j++;
}
translated.insert(i,1,'\n');
a++;
}
i++;
}
cout << "\nType the name of the file which will contain the translated captions and the right timestamps: \n";
getline(cin, line);
ofstream output(line);
output << translated;
output.close();
file.close();
newFile.close();
}