2 Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "string_text_file.h"
23 #include "exceptions.h"
24 #include "string_text_file_content.h"
25 #include <sub/subrip_reader.h>
26 #include <sub/ssa_reader.h>
27 #include <sub/stl_binary_reader.h>
28 #include <sub/collect.h>
29 #include <unicode/ucsdet.h>
30 #include <unicode/ucnv.h>
38 using std::shared_ptr;
39 using boost::scoped_array;
40 using boost::optional;
42 using namespace dcpomatic;
44 StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
46 string ext = content->path(0).extension().string();
47 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
49 std::unique_ptr<sub::Reader> reader;
52 auto f = fopen_boost (content->path(0), "rb");
54 throw OpenFileError (content->path(0), errno, OpenFileError::READ);
57 reader.reset(new sub::STLBinaryReader(f));
65 /* Text-based file; sort out its character encoding before we try to parse it */
67 ArrayData in (content->path (0));
69 UErrorCode status = U_ZERO_ERROR;
70 UCharsetDetector* detector = ucsdet_open (&status);
71 ucsdet_setText (detector, reinterpret_cast<const char *>(in.data()), in.size(), &status);
73 UCharsetMatch const * match = ucsdet_detect (detector, &status);
74 char const * in_charset = ucsdet_getName (match, &status);
76 UConverter* to_utf16 = ucnv_open (in_charset, &status);
77 /* This is a guess; I think we should be able to encode any input in 4 times its input size */
78 scoped_array<uint16_t> utf16 (new uint16_t[in.size() * 2]);
79 int const utf16_len = ucnv_toUChars (
80 to_utf16, reinterpret_cast<UChar*>(utf16.get()), in.size() * 2,
81 reinterpret_cast<const char *>(in.data()), in.size(),
85 UConverter* to_utf8 = ucnv_open ("UTF-8", &status);
87 scoped_array<char> utf8 (new char[utf16_len * 2]);
88 ucnv_fromUChars (to_utf8, utf8.get(), utf16_len * 2, reinterpret_cast<UChar*>(utf16.get()), utf16_len, &status);
90 /* Fix OS X line endings */
91 size_t utf8_len = strlen (utf8.get ());
92 for (size_t i = 0; i < utf8_len; ++i) {
93 if (utf8[i] == '\r' && ((i == utf8_len - 1) || utf8[i + 1] != '\n')) {
98 ucsdet_close (detector);
99 ucnv_close (to_utf16);
100 ucnv_close (to_utf8);
103 reader.reset(new sub::SubripReader(utf8.get()));
104 } else if (ext == ".ssa" || ext == ".ass") {
105 reader.reset(new sub::SSAReader(utf8.get()));
110 _subtitles = sub::collect<vector<sub::Subtitle>>(reader->subtitles());
114 /** @return time of first subtitle, if there is one */
115 optional<ContentTime>
116 StringTextFile::first () const
118 if (_subtitles.empty()) {
119 return optional<ContentTime>();
122 return ContentTime::from_seconds(_subtitles[0].from.all_as_seconds());
126 StringTextFile::length () const
128 if (_subtitles.empty ()) {
129 return ContentTime ();
132 return ContentTime::from_seconds (_subtitles.back().to.all_as_seconds ());