More testing / SubRip parse fixes.
authorCarl Hetherington <cth@carlh.net>
Wed, 15 Jan 2014 11:07:04 +0000 (11:07 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 15 Jan 2014 11:07:04 +0000 (11:07 +0000)
src/lib/subrip.cc
src/lib/subrip.h
src/lib/subrip_content.cc
src/lib/subrip_content.h
test/subrip_test.cc

index 1fdadf87e485f79e7a7be0ea62ea22daa4ebed8c..04765532fc2bfaad0f287e8d833aba220f55c3df 100644 (file)
@@ -29,6 +29,7 @@
 using std::string;
 using std::list;
 using std::vector;
+using std::cout;
 using boost::shared_ptr;
 using boost::lexical_cast;
 using boost::algorithm::trim;
@@ -54,8 +55,12 @@ SubRip::SubRip (shared_ptr<SubRipContent> content)
        
        while (!feof (f)) {
                fgets (buffer, sizeof (buffer), f);
+               if (feof (f)) {
+                       break;
+               }
+               
                string line (buffer);
-               trim (line);
+               trim_right_if (line, boost::is_any_of ("\n\r"));
                
                switch (state) {
                case COUNTER:
@@ -93,6 +98,7 @@ SubRip::SubRip (shared_ptr<SubRipContent> content)
                                current->y1 = convert_coordinate (p[5]);
                                current->y2 = convert_coordinate (p[6]);
                        }
+                       state = CONTENT;
                        break;
                }
                case CONTENT:
@@ -109,6 +115,11 @@ SubRip::SubRip (shared_ptr<SubRipContent> content)
                }
        }
 
+       if (state == CONTENT) {
+               current->pieces = convert_content (lines);
+               _subtitles.push_back (current.get ());
+       }
+
        fclose (f);
 }
 
@@ -217,7 +228,6 @@ SubRip::convert_content (list<string> t)
 Time
 SubRip::length () const
 {
-       boost::mutex::scoped_lock lm (_mutex);
        if (_subtitles.empty ()) {
                return 0;
        }
index 0f4fe5d294c557be548d8baef082b03fb728070a..6451c105fe6961302b4a39ecc9c4e048edf445cb 100644 (file)
@@ -23,6 +23,7 @@ class SubRipContent;
 class subrip_time_test;
 class subrip_coordinate_test;
 class subrip_content_test;
+class subrip_parse_test;
 
 class SubRip
 {
@@ -35,6 +36,7 @@ private:
        friend class subrip_time_test;
        friend class subrip_coordinate_test;
        friend class subrip_content_test;
+       friend class subrip_parse_test;
        
        static Time convert_time (std::string);
        static int convert_coordinate (std::string);
index 74bba33bae44452bdc404bd0bef9a1ff10be54cf..b13f64dedc29d56fd7000f86ee4ed6f9e3ff3fda 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "subrip_content.h"
 #include "util.h"
+#include "subrip.h"
 
 #include "i18n.h"
 
@@ -33,7 +34,7 @@ SubRipContent::SubRipContent (shared_ptr<const Film> film, boost::filesystem::pa
 
 }
 
-SubRipContent::SubRipContent (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node, int version)
+SubRipContent::SubRipContent (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node, int)
        : Content (film, node)
        , SubtitleContent (film, node)
 {
@@ -96,4 +97,3 @@ SubRipContent::identifier () const
 
        return s.str ();
 }
-
index 741e1a4c3ba5618f0ca94bbac0b9a69d798232b9..30a824eec1f920c7dc08acd2e2c39725e9f6acac 100644 (file)
@@ -25,6 +25,10 @@ public:
        SubRipContent (boost::shared_ptr<const Film>, boost::filesystem::path);
        SubRipContent (boost::shared_ptr<const Film>, boost::shared_ptr<const cxml::Node>, int);
 
+       boost::shared_ptr<SubRipContent> shared_from_this () {
+               return boost::dynamic_pointer_cast<SubRipContent> (Content::shared_from_this ());
+       }
+       
        void examine (boost::shared_ptr<Job>);
        std::string summary () const;
        std::string technical_summary () const;
index f23fc8a0b48ba248aaa5adc3d446764766282b19..a66ee1afd347bd4113ad017b17be46f45463b19e 100644 (file)
@@ -115,6 +115,57 @@ BOOST_AUTO_TEST_CASE (subrip_content_test)
 /** Test parsing of full SubRip file content */
 BOOST_AUTO_TEST_CASE (subrip_parse_test)
 {
-       SubRipContent content (shared_ptr<Film> (), "test/data/subrip.srt");
-       content.examine (shared_ptr<Job> ());
+       shared_ptr<SubRipContent> content (new SubRipContent (shared_ptr<Film> (), "test/data/subrip.srt"));
+       content->examine (shared_ptr<Job> ());
+       BOOST_CHECK_EQUAL (content->full_length(), ((3 * 60) + 56.471) * TIME_HZ);
+
+       SubRip s (content);
+
+       list<SubRipSubtitle>::const_iterator i = s._subtitles.begin();
+
+       BOOST_CHECK (i != s._subtitles.end ());
+       BOOST_CHECK_EQUAL (i->from, ((1 * 60) + 49.200) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->to, ((1 * 60) + 52.351) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->pieces.size(), 1);
+       BOOST_CHECK_EQUAL (i->pieces.front().text, "This is a subtitle, and it goes over two lines.");
+
+       ++i;
+       BOOST_CHECK (i != s._subtitles.end ());
+       BOOST_CHECK_EQUAL (i->from, ((1 * 60) + 52.440) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->to, ((1 * 60) + 54.351) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->pieces.size(), 1);
+       BOOST_CHECK_EQUAL (i->pieces.front().text, "We have emboldened this");
+       BOOST_CHECK_EQUAL (i->pieces.front().bold, true);
+
+       ++i;
+       BOOST_CHECK (i != s._subtitles.end ());
+       BOOST_CHECK_EQUAL (i->from, ((1 * 60) + 54.440) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->to, ((1 * 60) + 56.590) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->pieces.size(), 1);
+       BOOST_CHECK_EQUAL (i->pieces.front().text, "And italicised this.");
+       BOOST_CHECK_EQUAL (i->pieces.front().italic, true);
+
+       ++i;
+       BOOST_CHECK (i != s._subtitles.end ());
+       BOOST_CHECK_EQUAL (i->from, ((1 * 60) + 56.680) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->to, ((1 * 60) + 58.955) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->pieces.size(), 1);
+       BOOST_CHECK_EQUAL (i->pieces.front().text, "Shall I compare thee to a summers' day?");
+
+       ++i;
+       BOOST_CHECK (i != s._subtitles.end ());
+       BOOST_CHECK_EQUAL (i->from, ((2 * 60) + 0.840) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->to, ((2 * 60) + 3.400) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->pieces.size(), 1);
+       BOOST_CHECK_EQUAL (i->pieces.front().text, "Is this a dagger I see before me?");
+
+       ++i;
+       BOOST_CHECK (i != s._subtitles.end ());
+       BOOST_CHECK_EQUAL (i->from, ((3 * 60) + 54.560) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->to, ((3 * 60) + 56.471) * TIME_HZ);
+       BOOST_CHECK_EQUAL (i->pieces.size(), 1);
+       BOOST_CHECK_EQUAL (i->pieces.front().text, "Hello world.");
+
+       ++i;
+       BOOST_CHECK (i == s._subtitles.end ());
 }