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
|
/*
Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
This file is part of DCP-o-matic.
DCP-o-matic 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 2 of the License, or
(at your option) any later version.
DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
*/
#include "string_text_file.h"
#include "cross.h"
#include "exceptions.h"
#include "string_text_file_content.h"
#include <sub/subrip_reader.h>
#include <sub/ssa_reader.h>
#include <sub/stl_binary_reader.h>
#include <sub/collect.h>
#include <unicode/ucsdet.h>
#include <unicode/ucnv.h>
#include <iostream>
#include "i18n.h"
using std::vector;
using std::cout;
using std::string;
using boost::shared_ptr;
using boost::scoped_array;
using boost::optional;
using dcp::Data;
StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
{
string ext = content->path(0).extension().string();
transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
sub::Reader* reader = 0;
if (ext == ".stl") {
FILE* f = fopen_boost (content->path(0), "rb");
if (!f) {
throw OpenFileError (content->path(0), errno, OpenFileError::READ);
}
try {
reader = new sub::STLBinaryReader (f);
} catch (...) {
fclose (f);
throw;
}
fclose (f);
} else {
/* Text-based file; sort out its character encoding before we try to parse it */
Data in (content->path (0));
UErrorCode status = U_ZERO_ERROR;
UCharsetDetector* detector = ucsdet_open (&status);
ucsdet_setText (detector, reinterpret_cast<const char *> (in.data().get()), in.size(), &status);
UCharsetMatch const * match = ucsdet_detect (detector, &status);
char const * in_charset = ucsdet_getName (match, &status);
UConverter* to_utf16 = ucnv_open (in_charset, &status);
/* This is a guess; I think we should be able to encode any input in 4 times its input size */
scoped_array<uint16_t> utf16 (new uint16_t[in.size() * 2]);
int const utf16_len = ucnv_toUChars (
to_utf16, reinterpret_cast<UChar*>(utf16.get()), in.size() * 2,
reinterpret_cast<const char *> (in.data().get()), in.size(),
&status
);
UConverter* to_utf8 = ucnv_open ("UTF-8", &status);
/* Another guess */
scoped_array<char> utf8 (new char[utf16_len * 2]);
ucnv_fromUChars (to_utf8, utf8.get(), utf16_len * 2, reinterpret_cast<UChar*>(utf16.get()), utf16_len, &status);
/* Fix OS X line endings */
size_t utf8_len = strlen (utf8.get ());
for (size_t i = 0; i < utf8_len; ++i) {
if (utf8[i] == '\r' && ((i == utf8_len - 1) || utf8[i + 1] != '\n')) {
utf8[i] = '\n';
}
}
ucsdet_close (detector);
ucnv_close (to_utf16);
ucnv_close (to_utf8);
if (ext == ".srt") {
reader = new sub::SubripReader (utf8.get());
} else if (ext == ".ssa" || ext == ".ass") {
reader = new sub::SSAReader (utf8.get());
}
}
if (reader) {
_subtitles = sub::collect<vector<sub::Subtitle> > (reader->subtitles ());
}
delete reader;
}
/** @return time of first subtitle, if there is one */
optional<ContentTime>
StringTextFile::first () const
{
if (_subtitles.empty()) {
return optional<ContentTime>();
}
return ContentTime::from_seconds(_subtitles[0].from.all_as_seconds());
}
ContentTime
StringTextFile::length () const
{
if (_subtitles.empty ()) {
return ContentTime ();
}
return ContentTime::from_seconds (_subtitles.back().to.all_as_seconds ());
}
|