-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
199 lines (171 loc) · 4.44 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <iostream>
//#include <iomanip>>
#include <fstream>
#include <sstream>
#include <thread>
#include <exception>
#include <future>
#include <chrono>
#include <vector>
#include "exception_classes.h"
#include "helper_functions.h"
using namespace std::chrono_literals;
/**
* Prints the initial dialog to the user on the console to get file path.
*
* @return file path string.
*/
std::string GetFilePath(){
std::string Path;
std::cout << "Procced by entering the path of the srt file you want to shift."
<< std::endl << std::endl;
std::getline (std::cin,Path);
return Path;
}
double StrToFloat(const std::string & num_str){
std::string::size_type sz;
double num;
num = std::stof(num_str, &sz);
return num;
}
/**
* Gets time shift from user.
*
* @return time shift double.
*/
double GetTimeShift(){
double TimeShift;
std::string TimeShift_str;
std::cout << "Enter the time shift you want to apply on the subtitles." << std::endl;
std::cout << "Please, take caution that timeshift should be entered as '+' or '-' followed by shift seconds." << std::endl;
std::cout << "For example, for delaying subtitles by 1.5 seconds, I'd enter '+1.5'"
<< std::endl<<std::endl;
std::getline (std::cin,TimeShift_str);
if(TimeShift_str[0] != '+' && TimeShift_str[0] != '-'){
throw NoSign();
}
try{
TimeShift = StrToFloat(TimeShift_str);
}
catch(...){
std::cerr << "You didn't enter a float"<<std::endl <<"Terminating"<<std::endl;
std::terminate();
}
return TimeShift;
}
/**
* Prints new lines with special characters in between
* to distinguish between different sections of the program.
*/
void EndSection(){
std::cout<< std::endl
<< "---------------------------------------------------------------------" <<
std::endl << std::endl;
}
/**
* Manages the processing animation until
* processing is done.
*/
void PrintProcessing(std::future<void> && fut){
while(fut.wait_for(0ms) != std::future_status::ready){
std::cout<<"\r "<<std::flush;
std::cout<<"\rProcessing"<<std::flush;
std::this_thread::sleep_for(300ms);
std::cout<<"\rProcessing."<<std::flush;
std::this_thread::sleep_for(300ms);
std::cout<<"\rProcessing.."<<std::flush;
std::this_thread::sleep_for(300ms);
std::cout<<"\rProcessing..."<<std::flush;
std::this_thread::sleep_for(300ms);
}
std::cout<<std::endl;
EndSection();
std::cout<<std::endl<<"Finished processing"<<std::endl;
}
/**
* Allocates another file and writes a
* shifted version of the old file.
*
* @param Fpath string which points to the file.
* @param TS double timeshift.
*/
void Process(std::string Fpath, double TS){
std::this_thread::sleep_for(2s);
std::string NewFilePath;
try{
NewFilePath = helper::GetNewFilePath(Fpath);
}
catch(const FileAlreadyExisits& ex){
std::cerr << "File already exists."<<std::endl;
std::terminate();
}
std::vector<int> Shifts = helper::ProcessShift(TS);
std::ifstream IFile{Fpath};
std::ofstream OFile{NewFilePath};
std::string Line;
std::vector<int> FailedLines;
int i = 0;
while (std::getline(IFile, Line)){
i++;
if(Line.find("-->") != std::string::npos){
try{
Line = helper::ApplyShiftToLine(Line, Shifts);
}
catch(...){
FailedLines.push_back(i);
}
}
// Write Line to the newly created file.
OFile << Line << '\n';
}
IFile.close();
OFile.close();
for(int ID: FailedLines){
std::cout<<"\nLine Number "<<ID<<" failed\n";
}
}
int main()
{
std::string FilePath = GetFilePath(); // Getting file path.
double TimeShift;
// Try to open file.
{
std::ifstream i_file(FilePath,std::ios::in);
std::string Extension = FilePath.substr(FilePath.length() - 4,4);
try{
if (!(i_file)){
throw FileNotFound();
}
else if(Extension != ".srt") {
throw NotSrtFile();
}
}
catch(const FileNotFound& ex){
std::cerr << "File Not found." <<std::endl<<
"Terminating."<<std::endl;
std::terminate();
}
catch(const NotSrtFile& ex){
std::cerr << "That's not an srt file." <<std::endl<<
"Terminating."<<std::endl;
std::terminate();
}
i_file.close();
}
EndSection();
// Getting time shift from user.
try{
TimeShift = GetTimeShift();
}
catch(const NoSign & ex){
std::cerr << "You didn't enter a sign"<<std::endl <<"Program terminated."<<std::endl;
std::terminate();
}
EndSection();
// Start processing.
std::future<void> fut = std::async(std::launch::async,Process,
FilePath,TimeShift);
PrintProcessing(std::move(fut));
return 0;
}