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));
53 boost::optional<SubRipSubtitle> current;
57 fgets (buffer, sizeof (buffer), f);
63 trim_right_if (line, boost::is_any_of ("\n\r"));
69 /* a blank line at the start is ok */
74 current = SubRipSubtitle ();
80 boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
81 if (p.size() != 3 && p.size() != 7) {
82 throw SubRipError (line, _("a time/position line"), content->path (0));
85 current->period = ContentTimePeriod (convert_time (p[0]), convert_time (p[2]));
88 current->x1 = convert_coordinate (p[3]);
89 current->x2 = convert_coordinate (p[4]);
90 current->y1 = convert_coordinate (p[5]);
91 current->y2 = convert_coordinate (p[6]);
99 current->pieces = convert_content (lines);
100 _subtitles.push_back (current.get ());
104 lines.push_back (line);
110 if (state == CONTENT) {
111 current->pieces = convert_content (lines);
112 _subtitles.push_back (current.get ());
119 SubRip::convert_time (string t)
124 boost::algorithm::split (a, t, boost::is_any_of (":"));
125 assert (a.size() == 3);
126 r += ContentTime::from_seconds (lexical_cast<int> (a[0]) * 60 * 60);
127 r += ContentTime::from_seconds (lexical_cast<int> (a[1]) * 60);
130 boost::algorithm::split (b, a[2], boost::is_any_of (","));
131 r += ContentTime::from_seconds (lexical_cast<int> (b[0]));
132 r += ContentTime::from_seconds (lexical_cast<double> (b[1]) / 1000);
138 SubRip::convert_coordinate (string t)
141 boost::algorithm::split (a, t, boost::is_any_of (":"));
142 assert (a.size() == 2);
143 return lexical_cast<int> (a[1]);
147 SubRip::maybe_content (list<SubRipSubtitlePiece>& pieces, SubRipSubtitlePiece& p)
149 if (!p.text.empty ()) {
150 pieces.push_back (p);
155 list<SubRipSubtitlePiece>
156 SubRip::convert_content (list<string> t)
158 list<SubRipSubtitlePiece> pieces;
160 SubRipSubtitlePiece p;
169 /* XXX: missing <font> support */
170 /* XXX: nesting of tags e.g. <b>foo<i>bar<b>baz</b>fred</i>jim</b> might
174 for (list<string>::const_iterator i = t.begin(); i != t.end(); ++i) {
175 for (size_t j = 0; j < i->size(); ++j) {
178 if ((*i)[j] == '<' || (*i)[j] == '{') {
185 if ((*i)[j] == '>' || (*i)[j] == '}') {
187 maybe_content (pieces, p);
189 } else if (tag == "/b") {
190 maybe_content (pieces, p);
192 } else if (tag == "i") {
193 maybe_content (pieces, p);
195 } else if (tag == "/i") {
196 maybe_content (pieces, p);
198 } else if (tag == "u") {
199 maybe_content (pieces, p);
201 } else if (tag == "/u") {
202 maybe_content (pieces, p);
215 maybe_content (pieces, p);
221 SubRip::length () const
223 if (_subtitles.empty ()) {
224 return ContentTime ();
227 return _subtitles.back().period.to;