Make SubripReader::convert_time usable by other classes.
authorCarl Hetherington <cth@carlh.net>
Sun, 13 Nov 2022 18:46:18 +0000 (19:46 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 13 Nov 2022 18:46:18 +0000 (19:46 +0100)
src/subrip_reader.cc
src/subrip_reader.h
test/subrip_reader_test.cc

index f0fe07f0eb997c1e5968dafab243f7b1985f8033..926511a3db16c6361037adcf4030f963e73a1b8e 100644 (file)
@@ -124,8 +124,18 @@ SubripReader::read (function<optional<string> ()> get_line)
                                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 */
 
@@ -152,19 +162,27 @@ SubripReader::read (function<optional<string> ()> get_line)
        }
 }
 
-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;
@@ -172,25 +190,29 @@ SubripReader::convert_time (string t)
        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);
index 8d3fd4c124357e3a0723a84335b576917257ca7d..e29002f43a7e9e9b33235e51da92cc714c7e97cd 100644 (file)
@@ -41,6 +41,8 @@ public:
        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;
@@ -49,7 +51,6 @@ private:
        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);
index b364d570b60a4569f4a68a2b5f98991e5ed23961..6029b237d10ba0b03f366f1a6f962ab758354bb4 100644 (file)
@@ -408,8 +408,12 @@ BOOST_AUTO_TEST_CASE (subrip_reader_convert_line_test)
 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