throw SubripError (*line, "a time/position line", _context);
}
- rs.from = convert_time (p[0]);
- rs.to = convert_time (p[2]);
+ string expected;
+ auto from = convert_time(p[0], &expected);
+ if (!from) {
+ throw SubripError(p[0], expected, _context);
+ }
+ rs.from = *from;
+
+ auto to = convert_time(p[2], &expected);
+ if (!to) {
+ throw SubripError(p[2], expected, _context);
+ }
+ rs.to = *to;
/* XXX: should not ignore coordinate specifications */
}
}
-Time
-SubripReader::convert_time (string t)
+optional<Time>
+SubripReader::convert_time(string t, string* expected)
{
+ auto report_expected = [expected](string const& s) {
+ if (expected) {
+ *expected = s;
+ }
+ };
+
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", _context);
+ report_expected("time in the format h:m:s,ms");
+ return {};
}
vector<string> b;
boost::algorithm::split (b, a[2], boost::is_any_of (","));
if (b.size() != 2) {
- throw SubripError (t, "time in the format h:m:s,ms", _context);
+ report_expected("time in the format h:m:s,ms");
+ return {};
}
int h, m, s, ms;
try {
h = lexical_cast<int>(a[0]);
} catch (boost::bad_lexical_cast &) {
- throw SubripError (t, "integer hour value", _context);
+ report_expected("integer hour value");
+ return {};
}
try {
m = lexical_cast<int>(a[1]);
} catch (boost::bad_lexical_cast &) {
- throw SubripError (t, "integer minute value", _context);
+ report_expected("integer minute value");
+ return {};
}
try {
s = lexical_cast<int>(b[0]);
} catch (boost::bad_lexical_cast &) {
- throw SubripError (t, "integer second value", _context);
+ report_expected("integer second value");
+ return {};
}
try {
ms = lexical_cast<int>(b[1]);
} catch (boost::bad_lexical_cast &) {
- throw SubripError (t, "integer millisecond value", _context);
+ report_expected("integer millisecond value");
+ return {};
}
return Time::from_hms (h, m, s, ms);
SubripReader (FILE* f);
SubripReader (std::string subs);
+ static boost::optional<Time> convert_time(std::string t, std::string* expected = nullptr);
+
private:
/* For tests */
friend struct ::subrip_reader_convert_line_test;
friend struct ::subrip_reader_test6;
SubripReader () {}
- Time convert_time (std::string t);
void convert_line (std::string t, RawSubtitle& p);
void maybe_content (RawSubtitle& p);
void read (boost::function<boost::optional<std::string> ()> get_line);
BOOST_AUTO_TEST_CASE (subrip_reader_convert_time_test)
{
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));
+ auto t = reader.convert_time("00:03:10,500");
+ BOOST_REQUIRE(t);
+ BOOST_CHECK_EQUAL(*t, sub::Time::from_hms(0, 3, 10, 500));
+ t = reader.convert_time("04:19:51,782");
+ BOOST_REQUIRE(t);
+ BOOST_CHECK_EQUAL(*t, sub::Time::from_hms(4, 19, 51, 782));
}
static void