2 Copyright (C) 2012-2020 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.
24 #include <boost/shared_ptr.hpp>
35 using boost::optional;
36 using boost::shared_ptr;
39 /** @param s A string.
40 * @return true if the string contains only space, newline or tab characters, or is empty.
43 sub::empty_or_white_space (string s)
45 for (size_t i = 0; i < s.length(); ++i) {
46 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
55 sub::get_line_string (string* s)
57 if (s->length() == 0) {
58 return optional<string>();
61 size_t pos = s->find ("\n");
62 if (pos == string::npos) {
68 string const c = s->substr (0, pos);
69 s->erase (0, pos + 1);
74 sub::get_line_file (FILE* f)
77 char* r = fgets (buffer, sizeof (buffer), f);
79 return optional<string> ();
82 return string (buffer);
86 sub::remove_unicode_bom (optional<string>& line)
89 line->length() >= 3 &&
90 static_cast<unsigned char> (line.get()[0]) == 0xef &&
91 static_cast<unsigned char> (line.get()[1]) == 0xbb &&
92 static_cast<unsigned char> (line.get()[2]) == 0xbf
95 /* Skip Unicode byte order mark */
96 line = line->substr (3);
101 sub::dump (shared_ptr<const Reader> reader, ostream& os)
103 map<string, string> metadata = reader->metadata ();
104 for (map<string, string>::const_iterator i = metadata.begin(); i != metadata.end(); ++i) {
105 os << i->first << ": " << i->second << "\n";
108 list<sub::Subtitle> subs = collect<list<sub::Subtitle> > (reader->subtitles ());
110 for (list<sub::Subtitle>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
111 os << "Subtitle " << n << " at " << i->from << " -> " << i->to << "\n";
112 for (list<sub::Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
116 if (j->vertical_position.proportional) {
117 os << j->vertical_position.proportional.get() << " of screen";
118 } else if (j->vertical_position.line && j->vertical_position.lines) {
119 os << j->vertical_position.line.get() << " lines of " << j->vertical_position.lines.get();
121 if (j->vertical_position.reference) {
123 switch (j->vertical_position.reference.get()) {
127 case VERTICAL_CENTRE_OF_SCREEN:
130 case BOTTOM_OF_SCREEN:
133 case TOP_OF_SUBTITLE:
134 os << "top of subtitle";
141 bool underline = false;
142 for (list<sub::Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
143 if (k->italic && !italic) {
145 } else if (italic && !k->italic) {
148 if (k->underline && !underline) {
150 } else if (underline && !k->underline) {
155 underline = k->underline;