#include "subrip_reader.h"
#include "exceptions.h"
#include "util.h"
+#include <locked_sstream.h>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
using std::list;
using std::cout;
using std::hex;
-using std::stringstream;
using boost::lexical_cast;
using boost::to_upper;
using boost::optional;
/** @param s Subtitle string encoded in UTF-8 */
SubripReader::SubripReader (string const & s)
{
- stringstream str (s);
+ locked_stringstream str (s);
this->read (boost::bind (&get_line_stringstream, &str));
}
} state = COUNTER;
RawSubtitle rs;
- rs.font = "Arial";
- rs.font_size.set_points (48);
+
+ /* This reader extracts no information about where the subtitle
+ should be on screen, so its reference is TOP_OF_SUBTITLE.
+ */
rs.vertical_position.line = 0;
- /* XXX: arbitrary */
- rs.vertical_position.lines = 32;
rs.vertical_position.reference = TOP_OF_SUBTITLE;
while (true) {
}
trim_right_if (*line, boost::is_any_of ("\n\r"));
+ remove_unicode_bom (line);
- if (
- line->length() >= 3 &&
- static_cast<unsigned char> (line.get()[0]) == 0xef &&
- static_cast<unsigned char> (line.get()[1]) == 0xbb &&
- static_cast<unsigned char> (line.get()[2]) == 0xbf
- ) {
-
- /* Skip Unicode byte order mark */
- line = line->substr (3);
+ /* Keep some history in case there is an error to report */
+ _context.push_back (*line);
+ if (_context.size() > 5) {
+ _context.pop_front ();
}
switch (state) {
case METADATA:
{
vector<string> p;
- boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "));
+
+ /* Further trim this line, removing spaces from the end */
+ trim_right_if (*line, boost::is_any_of (" "));
+
+ boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" "), boost::token_compress_on);
if (p.size() != 3 && p.size() != 7) {
- throw SubripError (*line, "a time/position line");
+ for (int i = 0; i < 2; ++i) {
+ optional<string> ex = get_line ();
+ if (ex) {
+ _context.push_back (*ex);
+ }
+ }
+ throw SubripError (*line, "a time/position line", _context);
}
rs.from = convert_time (p[0]);
vector<string> a;
boost::algorithm::split (a, t, boost::is_any_of (":"));
if (a.size() != 3) {
- throw SubripError (t, "time in the format h:m:s,ms");
+ throw SubripError (t, "time in the format h:m:s,ms", _context);
}
vector<string> b;