From 79e49c267f3e51126ad7d6c936de2da5455a9894 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 20 Sep 2016 21:07:57 +0100 Subject: [PATCH] Give a more informative exception on SubRip parse failures. --- src/exceptions.cc | 11 ++++++++++- src/exceptions.h | 14 ++++++++++---- src/subrip_reader.cc | 16 ++++++++++++++-- src/subrip_reader.h | 4 +++- src/wscript | 1 + test/subrip_reader_test.cc | 5 +++-- 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/exceptions.cc b/src/exceptions.cc index 4d9d29f..d59d32a 100644 --- a/src/exceptions.cc +++ b/src/exceptions.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2014-2015 Carl Hetherington + 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 @@ -19,8 +19,10 @@ #include "compose.hpp" #include "exceptions.h" +#include using std::string; +using std::list; using namespace sub; ProgrammingError::ProgrammingError (string file, int line) @@ -28,3 +30,10 @@ ProgrammingError::ProgrammingError (string file, int line) { } + +SubripError::SubripError (string saw, string expecting, list context) + : runtime_error ("Error in SubRip file: saw " + (saw.empty() ? "an empty string" : saw) + " when expecting " + expecting) + , _context (context) +{ + +} diff --git a/src/exceptions.h b/src/exceptions.h index c6f7f1a..c7a3c71 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2014 Carl Hetherington + 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 @@ -22,6 +22,7 @@ #include #include +#include namespace sub { @@ -53,9 +54,14 @@ public: class SubripError : public std::runtime_error { public: - SubripError (std::string saw, std::string expecting) - : std::runtime_error ("Error in SubRip file: saw " + saw + " while expecting " + expecting) - {} + SubripError (std::string saw, std::string expecting, std::list context); + + std::list context () const { + return _context; + } + +private: + std::list _context; }; class MXFError : public std::runtime_error diff --git a/src/subrip_reader.cc b/src/subrip_reader.cc index 94a48c0..6fab0ff 100644 --- a/src/subrip_reader.cc +++ b/src/subrip_reader.cc @@ -83,6 +83,12 @@ SubripReader::read (function ()> get_line) trim_right_if (*line, boost::is_any_of ("\n\r")); remove_unicode_bom (line); + /* 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 COUNTER: { @@ -109,7 +115,13 @@ SubripReader::read (function ()> get_line) boost::algorithm::split (p, *line, boost::algorithm::is_any_of (" ")); if (p.size() != 3 && p.size() != 7) { - throw SubripError (*line, "a time/position line"); + for (int i = 0; i < 2; ++i) { + optional ex = get_line (); + if (ex) { + _context.push_back (*ex); + } + } + throw SubripError (*line, "a time/position line", _context); } rs.from = convert_time (p[0]); @@ -138,7 +150,7 @@ SubripReader::convert_time (string t) vector 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 b; diff --git a/src/subrip_reader.h b/src/subrip_reader.h index 5f5f343..138d703 100644 --- a/src/subrip_reader.h +++ b/src/subrip_reader.h @@ -44,10 +44,12 @@ private: friend struct ::subrip_reader_convert_time_test; SubripReader () {} - static Time convert_time (std::string t); + Time convert_time (std::string t); void convert_line (std::string t, RawSubtitle& p); void maybe_content (RawSubtitle& p); void read (boost::function ()> get_line); + + std::list _context; }; } diff --git a/src/wscript b/src/wscript index 9136ea1..a22c712 100644 --- a/src/wscript +++ b/src/wscript @@ -49,6 +49,7 @@ def build(bld): colour.h dcp_reader.h effect.h + exceptions.h font_size.h interop_dcp_reader.h rational.h diff --git a/test/subrip_reader_test.cc b/test/subrip_reader_test.cc index c06cf25..58cf46b 100644 --- a/test/subrip_reader_test.cc +++ b/test/subrip_reader_test.cc @@ -353,8 +353,9 @@ BOOST_AUTO_TEST_CASE (subrip_reader_convert_line_test) /** Test SubripReader::convert_time */ BOOST_AUTO_TEST_CASE (subrip_reader_convert_time_test) { - BOOST_CHECK_EQUAL (sub::SubripReader::convert_time ("00:03:10,500"), sub::Time::from_hms (0, 3, 10, 500)); - BOOST_CHECK_EQUAL (sub::SubripReader::convert_time ("04:19:51,782"), sub::Time::from_hms (4, 19, 51, 782)); + sub::SubripReader reader; + BOOST_CHECK_EQUAL (reader.convert_time ("00:03:10,500"), sub::Time::from_hms (0, 3, 10, 500)); + BOOST_CHECK_EQUAL (reader.convert_time ("04:19:51,782"), sub::Time::from_hms (4, 19, 51, 782)); } static void -- 2.30.2