/* Copyright (C) 2014-2016 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "stl_text_reader.h" #include "compose.hpp" #include #include #include #include using std::list; using std::ostream; using std::istream; using std::string; using std::vector; using std::cout; using boost::algorithm::trim; using boost::algorithm::starts_with; using boost::is_any_of; using boost::optional; using boost::lexical_cast; using namespace sub; STLTextReader::STLTextReader (istream& in) { /* This reader extracts no information about where the subtitle should be on screen, so its reference is TOP_OF_SUBTITLE. */ _subtitle.vertical_position.line = 0; _subtitle.vertical_position.reference = TOP_OF_SUBTITLE; while (in.good ()) { string line; getline (in, line); if (!in.good ()) { return; } trim (line); if (starts_with (line, "//")) { continue; } if (line.size() > 0 && line[0] == '$') { /* $ variables */ vector bits; split (bits, line, is_any_of ("=")); if (bits.size() == 2) { string name = bits[0]; trim (name); string value = bits[1]; trim (value); set (name, value); } else { warn (String::compose ("Unrecognised line %1", line)); } } else { /* "Normal" lines */ size_t divider[2]; divider[0] = line.find_first_of (","); if (divider[0] != string::npos) { divider[1] = line.find_first_of (",", divider[0] + 1); } if (divider[0] == string::npos || divider[1] == string::npos || divider[0] <= 1 || divider[1] >= line.length() - 1) { warn (String::compose ("Unrecognised line %1", line)); continue; } string from_string = line.substr (0, divider[0] - 1); trim (from_string); string to_string = line.substr (divider[0] + 1, divider[1] - divider[0] - 1); trim (to_string); optional