-
Notifications
You must be signed in to change notification settings - Fork 3
/
FileManager.cpp
executable file
·627 lines (537 loc) · 16.4 KB
/
FileManager.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//
// Copyright 2015 CutePoisonX ([email protected])
//
// This file is part of PoisonConvert.
//
// PoisonConvert is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// PoisonConvert is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with PoisonConvert. If not, see <http://www.gnu.org/licenses/>.
//
#include "FileManager.h"
#include "FileWriteException.h"
#include "VectorSourceManager.h"
#include "AnalyzeMedia.h"
#include "SettingsMode.h"
#include "Settings.h"
#include <sstream>
#include <vector>
#include <cstdlib>
#include <math.h>
using namespace std;
FileManager::FileManager(SettingsMode& settings, VectorSourceManager& vec_man,
AnalyzeMedia& analyze)
: setting_(settings), vec_man_(vec_man), analyze_(analyze)
{
}
FileManager::FileManager(const FileManager& orig)
: setting_(orig.setting_), vec_man_(orig.vec_man_), analyze_(orig.analyze_)
{
}
FileManager::~FileManager()
{
}
bool FileManager::readSettings() throw(FileReadException,
OpenFileException)
{
unsigned int num_settings;
bool all_settings_succeeded = true;
ifstream readfile;
string tmp_string;
num_settings = setting_.getVectorLen();
//Head
readfile.open("/usr/syno/etc/poisonconvert/PoisonConvert_Settings");
if (readfile.is_open() == false)
{
throw OpenFileException();
}
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (tmp_string != "-----Settings-----")
{
readfile.close();
throw FileReadException();
}
for(unsigned int i=0; i<=num_settings; i++)
{
std::string setting_name;
std::string setting_value;
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (tmp_string == "-----Settings-----")
{
getline(readfile, tmp_string, '\n');
if (readfile.eof() != true)
{
readfile.close();
throw FileReadException();
}
return all_settings_succeeded;
}
setting_name = tmp_string.substr(0, tmp_string.find(" "));
setting_value = tmp_string.substr(tmp_string.find(" ") + 1);
if (setting_.checkIfSettingExists(setting_name) == false)
{
readfile.close();
throw FileReadException();
}
if (setting_.writeParam(setting_value, setting_name, false, true) == Settings::PARAM_CHANGE_ERROR)
{
all_settings_succeeded = false;
}
}
//should never reach this line
readfile.close();
throw FileReadException();
return all_settings_succeeded;
}
void FileManager::saveSettingsToFile() throw(FileWriteException)
{
ofstream writefile;
unsigned int num_settings;
num_settings = setting_.getVectorLen();
writefile.open("/usr/syno/etc/poisonconvert/PoisonConvert_Settings", ostream::out);
if (writefile.is_open() == false)
{
writefile.close();
throw FileWriteException();
}
//Head
writefile << "-----Settings-----" << endl;
//First setting (filename)
for(unsigned int i=0; i<num_settings; i++)
{
writefile << setting_.getSettingsName(i); writefile << " ";
writefile << setting_.getSettingsParam(i) << endl;
}
//End of file
writefile << "-----Settings-----" << endl;
writefile.close();
}
void FileManager::ReadFileError(ifstream& readfile) throw(FileReadException)
{
if (readfile.good() != true)
{
readfile.close();
throw FileReadException();
}
}
int FileManager::savePreferencesToFile() throw(FileWriteException)
{
ofstream writefile;
string filename = setting_.getSettingsParam(SettingsMode::CONFIGNAME);
string config_file;
unsigned int identifier = 0;
config_file = checkPathToConfig();
if(config_file == "")
{
return -1; //no path to config-file specified
}
config_file.append(filename);
//filename.append(".txt");
//Try open stream
writefile.open(config_file.c_str(), ostream::out);
if (writefile.is_open() == false)
{
writefile.close();
throw FileWriteException();
}
//Head
writefile << "-----Preferences-----" << endl;
//Targets
for (identifier = TARVIDEO; identifier <= TARSUB; identifier++)
{
writefile << "-----START-TARGETS-----" << endl;
writefile << vec_man_.getVectorLen(identifier) << endl;
writePrefs(writefile, identifier);
writefile << "------END-TARGETS------" << endl;
}
//Rules
for (identifier = SRCVIDEO; identifier <= SRCSUB; identifier++)
{
writefile << "------START-RULES------" << endl;
writefile << vec_man_.getVectorLen(identifier) << endl;
writePrefs(writefile, identifier);
writefile << "-------END-RULES-------" << endl;
}
//End of file
writefile << "-----Preferences-----" << endl;
writefile.close();
return 0;
}
string FileManager::checkPathToConfig()
{
string config_file = setting_.getSettingsParam(SettingsMode::CONFIGLOC);
return config_file;
}
void FileManager::writePrefs(ofstream& writefile, unsigned int identifier)
{
for(unsigned int priority = 1; priority <= vec_man_.getVectorLen(identifier); priority++)
{
for(unsigned int param_nr = 1; param_nr <= 5; param_nr++)
{
writefile << vec_man_.sendSinglePreference(priority, identifier, param_nr).c_str();
writefile << "HEXT";
}
writefile << endl;
if(identifier <= SRCSUB)
{
writefile << vec_man_.getTargetVectorLenFromSrc(identifier, priority) << endl;
for(unsigned int tar_item = 1; tar_item <= vec_man_.getTargetVectorLenFromSrc(identifier, priority);
tar_item++)
{
writefile << vec_man_.getTargetIDfromSource(priority, identifier, tar_item) << endl;
}
}
}
}
void FileManager::readPreferences() throw (FileReadException, OpenFileException)
{
ifstream readfile;
string tmp_string;
string filename = setting_.getSettingsParam(SettingsMode::CONFIGNAME);
string config_file = setting_.getSettingsParam(SettingsMode::CONFIGLOC);
config_file.append(filename);
unsigned int tmp_count = 0;
unsigned int identifier;
//filename.append(".txt");
if(readfile.is_open() == true)
readfile.close();
readfile.open(config_file.c_str());
if (readfile.is_open() == false)
{
readfile.close();
throw OpenFileException();
}
//Head
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (tmp_string != "-----Preferences-----")
{
readfile.close();
throw FileReadException();
}
//Targets
for (identifier = TARVIDEO; identifier <= TARSUB; identifier++)
{
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (tmp_string != "-----START-TARGETS-----")
{
readfile.close();
throw FileReadException();
}
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if(stringToInt(tmp_string, tmp_count) == false)
{
readfile.close();
throw FileReadException();
}
if(tmp_count != 0)
{
readPrefs(readfile, identifier, tmp_count);
//getline(readfile, tmp_string, '\n');
}
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (tmp_string != "------END-TARGETS------")
{
readfile.close();
throw FileReadException();
}
}
//Rules
for (identifier = SRCVIDEO; identifier <= SRCSUB; identifier++)
{
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (tmp_string != "------START-RULES------")
{
readfile.close();
throw FileReadException();
}
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (stringToInt(tmp_string, tmp_count) == false)
{
readfile.close();
throw FileReadException();
}
if (tmp_count != 0)
{
readPrefs(readfile, identifier, tmp_count);
}
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (tmp_string != "-------END-RULES-------")
{
readfile.close();
throw FileReadException();
}
}
readfile.close();
}
void FileManager::readPrefs(ifstream& readfile, unsigned int identifier,
unsigned int tmp_count) throw(FileReadException)
{
string tmp_string;
string params[5] = {"HI"};
unsigned int count_targets = 0;
unsigned int target_id = 0;
for (unsigned int priority = 1; priority <= tmp_count; priority++)
{
for (unsigned int param_nr = 1; param_nr <= 5; param_nr++)
{
getline(readfile, params[param_nr - 1], 'H');
ReadFileError(readfile);
if(param_nr == 5)
{
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (tmp_string != "EXT")
{
readfile.close();
throw FileReadException();
}
}
else
{
getline(readfile, tmp_string, 'T');
ReadFileError(readfile);
if (tmp_string != "EX")
{
readfile.close();
throw FileReadException();
}
}
}
//in order to be compatible to older config files (versions <1.4.1), where
//the parameter "audio channels" can have a label like stereo or 5.1, we
//convert the label to a number ...
if (identifier == SRCAUDIO)
{
if (params[0] == "dts")
{
//check if param is dts and change it to dca:
params[0] = "dca";
}
if (params[1] == "stereo")
{
params[1] = "2";
} else if (params[1] == "5.1")
{
params[1] = "6";
}
}
vec_man_.saveToVector(params[0], params[1], params[2], params[3], params[4],
priority, identifier);
if (identifier <= SRCSUB)
{
//getline(readfile, tmp_string, '\n');
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (stringToInt(tmp_string, count_targets) == false)
{
throw FileReadException();
}
for (unsigned int tar_item = 1; tar_item <= count_targets; tar_item++)
{
getline(readfile, tmp_string, '\n');
ReadFileError(readfile);
if (stringToInt(tmp_string, target_id) == false)
{
throw FileReadException();
}
vec_man_.linkNewTarget(target_id, identifier, priority);
}
}
}
}
bool FileManager::stringToInt(std::string& string_number,
unsigned int& value)
{
std::istringstream stream(string_number);
stream >> value;
if(stream.fail() || ! stream.eof())
{
return false;
}
return true;
}
void FileManager::readImportantFiles(vector<string>& filenames) throw (FileReadException, OpenFileException)
{
ifstream readfile;
string tmp_filename = "start";
readfile.open("/tmp/poisonXfileslist");
if (readfile.is_open() == false)
{
readfile.close();
throw OpenFileException();
}
while (tmp_filename != "")
{
getline(readfile, tmp_filename, '\n');
filenames.push_back(tmp_filename);
}
filenames.pop_back();
readfile.close();
}
void FileManager::readProperties(string filename, string& duration) throw (FileReadException, OpenFileException)
{
ifstream readfile;
string line;
string outter_bitrate;
string params[5] = {"poison"};
readfile.open("/tmp/poisonXprobelist");
if (readfile.is_open() == false)
{
readfile.close();
throw OpenFileException();
}
do
{
getline(readfile, line, '\n');
if (line.find("Duration", 0) != string::npos)
{
unsigned int duration_pos_beg = line.find_first_of(":") + 2;
unsigned int duration_pos_end = line.find_first_of(",");
string tmp_duration;
tmp_duration = line.substr(duration_pos_beg, duration_pos_end - duration_pos_beg);
if (line.find(".") != string::npos) //we don't need to compare fractions of seconds ...
{
tmp_duration = line.substr(0, line.find("."));
}
else
{
tmp_duration = line;
}
if (tmp_duration.empty() == false)
{
duration = tmp_duration;
}
//getting bitrate -> unfortunately not within the method readPropsVideo(...)
outter_bitrate = line.substr(line.find("bitrate: ") + 9); //Note: this happens before we analyze any stream
}
else if (line.substr(0, 6) == "stream") //note: 6 is the length of "stream" -> if true, the line begins with "stream"
{
unsigned int position = line.find("codec_type=", 0);
position = position + 11; //note: 11 is the length of "codec_type="
unsigned int position_two = line.find("|", position);
string type_of_stream = line.substr(position, position_two - position);
if (type_of_stream == "video") //Important: we can not get the bitrate out of the "video" section -> it is analyzed above in the duration section!
{
position = filename.find_last_of(".");
params[0] = filename.substr(position + 1, filename.length() - position);
params[2] = outter_bitrate;
readPropsVideo(line, params);
analyze_.saveToVector(params[0], params[1], params[2], params[3],
params[4], 0, SRCVIDEO);
} else if (type_of_stream == "audio")
{
readPropsAudio(line, params);
analyze_.saveToVector(params[0], params[1], params[2], params[3],
params[4], 0, SRCAUDIO);
} else if (type_of_stream == "subtitle")
{
readPropsSub(line, params[0], params[1]);
params[2] = "OFF";
params[3] = "OFF";
params[4] = "OFF";
analyze_.saveToVector(params[0], params[1], params[2], params[3],
params[4], 0, SRCSUB);
}
}
} while (readfile.eof() == false);
readfile.close();
}
void FileManager::readPropsVideo(string line, string (¶ms)[5])
{
stringstream bitrate_ss;
unsigned int bitrate_nr = 0;
string width;
string height;
string fps_param;
//codec:
params[1] = processOrdinaryParameter(line, "codec_name");
//Bitrate
//Note: due to certain limitations, the bitrate is set in the method: readProperties()
if(params[2].length() >= 4 && params[2].substr(params[2].length() - 4) == "kb/s")
{
stringToInt(params[2], bitrate_nr);
bitrate_nr = bitrate_nr*1000;
bitrate_ss << bitrate_nr;
params[2] = bitrate_ss.str();
}
//resolution
width = processOrdinaryParameter(line, "width");
height = processOrdinaryParameter(line, "height");
params[3] = width + "x" + height;
//Frames per second
fps_param = processOrdinaryParameter(line, "avg_frame_rate");
if (fps_param.find("/") != string::npos)
{
stringstream fps_ss;
string fps_numerator = fps_param.substr(0, fps_param.find("/"));
string fps_denominator = fps_param.substr(fps_param.find("/") + 1);
float fps_numerator_d = atof(fps_numerator.c_str());
float fps_denominator_d = atof(fps_denominator.c_str());
float fps_d = fps_numerator_d / fps_denominator_d;
float factor = pow(10.0, 4 - ceil(log10(fabs(fps_d))));
fps_d = round(fps_d * factor) / factor;
fps_ss << fps_d;
fps_param = fps_ss.str();
}
params[4] = fps_param;
}
void FileManager::readPropsAudio(string line, string (¶ms)[5])
{
//codec:
params[0] = processOrdinaryParameter(line, "codec_name");
//channels:
params[1] = processOrdinaryParameter(line, "channels");
//language:
params[2] = processOrdinaryParameter(line, "tag:language");
//bitrate:
params[3] = processOrdinaryParameter(line, "bit_rate");
//sample rate:
params[4] = processOrdinaryParameter(line, "sample_rate");
}
void FileManager::readPropsSub(string line, string& param0,
string& param1)
{
//codec:
param0 = processOrdinaryParameter(line, "codec_name");
//language:
param1 = processOrdinaryParameter(line, "tag:language");
}
string FileManager::processOrdinaryParameter(string const& line, string const& c_param_name)
{
unsigned int param_begin;
unsigned int param_end;
string param_name = c_param_name + "=";
string param;
param_begin = line.find(param_name, 0);
if (param_begin != string::npos)
{
param_begin = param_begin + param_name.length();
param_end = line.find("|", param_begin);
param = line.substr(param_begin, param_end - param_begin);
if (param == "N/A")
{
param = "none";
}
} else
{
return "none";
}
return param;
}