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"
32 using boost::shared_ptr;
33 using boost::lexical_cast;
34 using boost::algorithm::trim;
36 SubRip::SubRip (shared_ptr<SubRipContent> content)
38 FILE* f = fopen_boost (content->path (0), "r");
40 throw OpenFileError (content->path (0));
52 boost::optional<SubRipSubtitle> current;
56 fgets (buffer, sizeof (buffer), f);
65 x = lexical_cast<int> (line);
70 if (x == next_count) {
73 current = SubRipSubtitle ();
75 throw SubRipError (line, _("a subtitle count"), content->path (0));
82 boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
83 if (p.size() != 3 && p.size() != 7) {
84 throw SubRipError (line, _("a time/position line"), content->path (0));
87 current->from = convert_time (p[0]);
88 current->to = convert_time (p[2]);
91 current->x1 = convert_coordinate (p[3]);
92 current->x2 = convert_coordinate (p[4]);
93 current->y1 = convert_coordinate (p[5]);
94 current->y2 = convert_coordinate (p[6]);
101 current->pieces = convert_content (lines);
102 _subtitles.push_back (current.get ());
106 lines.push_back (line);
116 SubRip::convert_time (string t)
121 boost::algorithm::split (a, t, boost::is_any_of (":"));
122 assert (a.size() == 3);
123 r += lexical_cast<int> (a[0]) * 60 * 60 * TIME_HZ;
124 r += lexical_cast<int> (a[1]) * 60 * TIME_HZ;
127 boost::algorithm::split (b, a[2], boost::is_any_of (","));
128 r += lexical_cast<int> (b[0]) * TIME_HZ;
129 r += lexical_cast<int> (b[1]) * TIME_HZ / 1000;
135 SubRip::convert_coordinate (string t)
138 boost::algorithm::split (a, t, boost::is_any_of (":"));
139 assert (a.size() == 2);
140 return lexical_cast<int> (a[1]);
144 SubRip::maybe_content (list<SubRipSubtitlePiece>& pieces, SubRipSubtitlePiece& p)
146 if (!p.text.empty ()) {
147 pieces.push_back (p);
152 list<SubRipSubtitlePiece>
153 SubRip::convert_content (list<string> t)
155 list<SubRipSubtitlePiece> pieces;
157 SubRipSubtitlePiece p;
166 /* XXX: missing <font> support */
167 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
171 for (list<string>::const_iterator i = t.begin(); i != t.end(); ++i) {
172 for (size_t j = 0; j < i->size(); ++j) {
175 if ((*i)[j] == '<' || (*i)[j] == '{') {
182 if ((*i)[j] == '>' || (*i)[j] == '}') {
184 maybe_content (pieces, p);
186 } else if (tag == "/b") {
187 maybe_content (pieces, p);
189 } else if (tag == "i") {
190 maybe_content (pieces, p);
192 } else if (tag == "/i") {
193 maybe_content (pieces, p);
195 } else if (tag == "u") {
196 maybe_content (pieces, p);
198 } else if (tag == "/u") {
199 maybe_content (pieces, p);
212 maybe_content (pieces, p);
218 SubRip::length () const
220 boost::mutex::scoped_lock lm (_mutex);
221 if (_subtitles.empty ()) {
225 return _subtitles.back().to;