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>
22 #include "subrip_content.h"
23 #include "subrip_subtitle.h"
25 #include "exceptions.h"
33 using boost::shared_ptr;
34 using boost::lexical_cast;
35 using boost::algorithm::trim;
37 SubRip::SubRip (shared_ptr<SubRipContent> content)
39 FILE* f = fopen_boost (content->path (0), "r");
41 throw OpenFileError (content->path (0));
53 boost::optional<SubRipSubtitle> current;
57 fgets (buffer, sizeof (buffer), f);
63 trim_right_if (line, boost::is_any_of ("\n\r"));
70 x = lexical_cast<int> (line);
75 if (x == next_count) {
78 current = SubRipSubtitle ();
80 throw SubRipError (line, _("a subtitle count"), content->path (0));
87 boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
88 if (p.size() != 3 && p.size() != 7) {
89 throw SubRipError (line, _("a time/position line"), content->path (0));
92 current->from = convert_time (p[0]);
93 current->to = convert_time (p[2]);
96 current->x1 = convert_coordinate (p[3]);
97 current->x2 = convert_coordinate (p[4]);
98 current->y1 = convert_coordinate (p[5]);
99 current->y2 = convert_coordinate (p[6]);
107 current->pieces = convert_content (lines);
108 _subtitles.push_back (current.get ());
112 lines.push_back (line);
118 if (state == CONTENT) {
119 current->pieces = convert_content (lines);
120 _subtitles.push_back (current.get ());
127 SubRip::convert_time (string t)
132 boost::algorithm::split (a, t, boost::is_any_of (":"));
133 assert (a.size() == 3);
134 r += lexical_cast<int> (a[0]) * 60 * 60 * TIME_HZ;
135 r += lexical_cast<int> (a[1]) * 60 * TIME_HZ;
138 boost::algorithm::split (b, a[2], boost::is_any_of (","));
139 r += lexical_cast<int> (b[0]) * TIME_HZ;
140 r += lexical_cast<int> (b[1]) * TIME_HZ / 1000;
146 SubRip::convert_coordinate (string t)
149 boost::algorithm::split (a, t, boost::is_any_of (":"));
150 assert (a.size() == 2);
151 return lexical_cast<int> (a[1]);
155 SubRip::maybe_content (list<SubRipSubtitlePiece>& pieces, SubRipSubtitlePiece& p)
157 if (!p.text.empty ()) {
158 pieces.push_back (p);
163 list<SubRipSubtitlePiece>
164 SubRip::convert_content (list<string> t)
166 list<SubRipSubtitlePiece> pieces;
168 SubRipSubtitlePiece p;
177 /* XXX: missing <font> support */
178 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
182 for (list<string>::const_iterator i = t.begin(); i != t.end(); ++i) {
183 for (size_t j = 0; j < i->size(); ++j) {
186 if ((*i)[j] == '<' || (*i)[j] == '{') {
193 if ((*i)[j] == '>' || (*i)[j] == '}') {
195 maybe_content (pieces, p);
197 } else if (tag == "/b") {
198 maybe_content (pieces, p);
200 } else if (tag == "i") {
201 maybe_content (pieces, p);
203 } else if (tag == "/i") {
204 maybe_content (pieces, p);
206 } else if (tag == "u") {
207 maybe_content (pieces, p);
209 } else if (tag == "/u") {
210 maybe_content (pieces, p);
223 maybe_content (pieces, p);
229 SubRip::length () const
231 if (_subtitles.empty ()) {
235 return _subtitles.back().to;