Missed update to private test repo version.
[dcpomatic.git] / src / lib / string_text_file.cc
1 /*
2     Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "cross.h"
23 #include "exceptions.h"
24 #include "string_text_file.h"
25 #include "string_text_file_content.h"
26 #include <dcp/file.h>
27 #include <sub/collect.h>
28 #include <sub/ssa_reader.h>
29 #include <sub/stl_binary_reader.h>
30 #include <sub/subrip_reader.h>
31 #include <unicode/ucsdet.h>
32 #include <unicode/ucnv.h>
33 #include <iostream>
34
35 #include "i18n.h"
36
37
38 using std::cout;
39 using std::shared_ptr;
40 using std::string;
41 using std::vector;
42 using boost::scoped_array;
43 using boost::optional;
44 using dcp::ArrayData;
45 using namespace dcpomatic;
46
47
48 StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
49 {
50         string ext = content->path(0).extension().string();
51         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
52
53         std::unique_ptr<sub::Reader> reader;
54
55         if (ext == ".stl") {
56                 dcp::File f(content->path(0), "rb");
57                 if (!f) {
58                         throw OpenFileError (f.path(), errno, OpenFileError::READ);
59                 }
60                 try {
61                         reader.reset(new sub::STLBinaryReader(f.get()));
62                 } catch (...) {
63                         throw;
64                 }
65
66         } else {
67                 /* Text-based file; sort out its character encoding before we try to parse it */
68
69                 ArrayData in (content->path (0));
70
71                 UErrorCode status = U_ZERO_ERROR;
72                 UCharsetDetector* detector = ucsdet_open (&status);
73                 ucsdet_setText (detector, reinterpret_cast<const char *>(in.data()), in.size(), &status);
74
75                 UCharsetMatch const * match = ucsdet_detect (detector, &status);
76                 char const * in_charset = ucsdet_getName (match, &status);
77
78                 UConverter* to_utf16 = ucnv_open (in_charset, &status);
79                 /* This is a guess; I think we should be able to encode any input in 4 times its input size */
80                 scoped_array<uint16_t> utf16 (new uint16_t[in.size() * 2]);
81                 int const utf16_len = ucnv_toUChars (
82                                 to_utf16, reinterpret_cast<UChar*>(utf16.get()), in.size() * 2,
83                                 reinterpret_cast<const char *>(in.data()), in.size(),
84                                 &status
85                                 );
86
87                 UConverter* to_utf8 = ucnv_open ("UTF-8", &status);
88                 /* Another guess */
89                 scoped_array<char> utf8 (new char[utf16_len * 2]);
90                 ucnv_fromUChars (to_utf8, utf8.get(), utf16_len * 2, reinterpret_cast<UChar*>(utf16.get()), utf16_len, &status);
91
92                 /* Fix OS X line endings */
93                 size_t utf8_len = strlen (utf8.get ());
94                 for (size_t i = 0; i < utf8_len; ++i) {
95                         if (utf8[i] == '\r' && ((i == utf8_len - 1) || utf8[i + 1] != '\n')) {
96                                 utf8[i] = '\n';
97                         }
98                 }
99
100                 ucsdet_close (detector);
101                 ucnv_close (to_utf16);
102                 ucnv_close (to_utf8);
103
104                 if (ext == ".srt") {
105                         reader.reset(new sub::SubripReader(utf8.get()));
106                 } else if (ext == ".ssa" || ext == ".ass") {
107                         reader.reset(new sub::SSAReader(utf8.get()));
108                 }
109         }
110
111         if (reader) {
112                 _subtitles = sub::collect<vector<sub::Subtitle>>(reader->subtitles());
113         }
114 }
115
116 /** @return time of first subtitle, if there is one */
117 optional<ContentTime>
118 StringTextFile::first () const
119 {
120         if (_subtitles.empty()) {
121                 return {};
122         }
123
124         return ContentTime::from_seconds(_subtitles[0].from.all_as_seconds());
125 }
126
127 ContentTime
128 StringTextFile::length () const
129 {
130         if (_subtitles.empty ()) {
131                 return {};
132         }
133
134         return ContentTime::from_seconds (_subtitles.back().to.all_as_seconds ());
135 }