2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <boost/algorithm/string.hpp>
21 #include <boost/lexical_cast.hpp>
23 #include "subrip_content.h"
24 #include "subrip_subtitle.h"
26 #include "exceptions.h"
34 using boost::shared_ptr;
35 using boost::lexical_cast;
36 using boost::algorithm::trim;
38 SubRip::SubRip (shared_ptr<const SubRipContent> content)
40 FILE* f = fopen_boost (content->path (0), "r");
42 throw OpenFileError (content->path (0));
54 boost::optional<SubRipSubtitle> current;
58 fgets (buffer, sizeof (buffer), f);
64 trim_right_if (line, boost::is_any_of ("\n\r"));
70 /* a blank line at the start is ok */
76 x = lexical_cast<int> (line);
81 if (x == next_count) {
84 current = SubRipSubtitle ();
86 throw SubRipError (line, _("a subtitle count"), content->path (0));
93 boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
94 if (p.size() != 3 && p.size() != 7) {
95 throw SubRipError (line, _("a time/position line"), content->path (0));
98 current->period = ContentTimePeriod (convert_time (p[0]), convert_time (p[2]));
101 current->x1 = convert_coordinate (p[3]);
102 current->x2 = convert_coordinate (p[4]);
103 current->y1 = convert_coordinate (p[5]);
104 current->y2 = convert_coordinate (p[6]);
112 current->pieces = convert_content (lines);
113 _subtitles.push_back (current.get ());
117 lines.push_back (line);
123 if (state == CONTENT) {
124 current->pieces = convert_content (lines);
125 _subtitles.push_back (current.get ());
132 SubRip::convert_time (string t)
137 boost::algorithm::split (a, t, boost::is_any_of (":"));
138 assert (a.size() == 3);
139 r += ContentTime::from_seconds (lexical_cast<int> (a[0]) * 60 * 60);
140 r += ContentTime::from_seconds (lexical_cast<int> (a[1]) * 60);
143 boost::algorithm::split (b, a[2], boost::is_any_of (","));
144 r += ContentTime::from_seconds (lexical_cast<int> (b[0]));
145 r += ContentTime::from_seconds (lexical_cast<double> (b[1]) / 1000);
151 SubRip::convert_coordinate (string t)
154 boost::algorithm::split (a, t, boost::is_any_of (":"));
155 assert (a.size() == 2);
156 return lexical_cast<int> (a[1]);
160 SubRip::maybe_content (list<SubRipSubtitlePiece>& pieces, SubRipSubtitlePiece& p)
162 if (!p.text.empty ()) {
163 pieces.push_back (p);
168 list<SubRipSubtitlePiece>
169 SubRip::convert_content (list<string> t)
171 list<SubRipSubtitlePiece> pieces;
173 SubRipSubtitlePiece p;
182 /* XXX: missing <font> support */
183 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
187 for (list<string>::const_iterator i = t.begin(); i != t.end(); ++i) {
188 for (size_t j = 0; j < i->size(); ++j) {
191 if ((*i)[j] == '<' || (*i)[j] == '{') {
198 if ((*i)[j] == '>' || (*i)[j] == '}') {
200 maybe_content (pieces, p);
202 } else if (tag == "/b") {
203 maybe_content (pieces, p);
205 } else if (tag == "i") {
206 maybe_content (pieces, p);
208 } else if (tag == "/i") {
209 maybe_content (pieces, p);
211 } else if (tag == "u") {
212 maybe_content (pieces, p);
214 } else if (tag == "/u") {
215 maybe_content (pieces, p);
228 maybe_content (pieces, p);
234 SubRip::length () const
236 if (_subtitles.empty ()) {
237 return ContentTime ();
240 return _subtitles.back().period.to;