X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fsubrip.cc;h=3eac98e638d22c17a1940a2355abee33a3fe1649;hb=a79d78d8bb6d51f6662f1f63b9f8fd19e1a0c5f1;hp=1fdadf87e485f79e7a7be0ea62ea22daa4ebed8c;hpb=f9608c5299d0a58bc01b33e521d89a80be26ed23;p=dcpomatic.git diff --git a/src/lib/subrip.cc b/src/lib/subrip.cc index 1fdadf87e..3eac98e63 100644 --- a/src/lib/subrip.cc +++ b/src/lib/subrip.cc @@ -29,11 +29,12 @@ using std::string; using std::list; using std::vector; +using std::cout; using boost::shared_ptr; using boost::lexical_cast; using boost::algorithm::trim; -SubRip::SubRip (shared_ptr content) +SubRip::SubRip (shared_ptr content) { FILE* f = fopen_boost (content->path (0), "r"); if (!f) { @@ -54,8 +55,12 @@ SubRip::SubRip (shared_ptr 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 content) current->y1 = convert_coordinate (p[5]); current->y2 = convert_coordinate (p[6]); } + state = CONTENT; break; } case CONTENT: @@ -109,24 +115,29 @@ SubRip::SubRip (shared_ptr content) } } + if (state == CONTENT) { + current->pieces = convert_content (lines); + _subtitles.push_back (current.get ()); + } + fclose (f); } -Time +ContentTime SubRip::convert_time (string t) { - Time r = 0; + ContentTime r; vector a; boost::algorithm::split (a, t, boost::is_any_of (":")); assert (a.size() == 3); - r += lexical_cast (a[0]) * 60 * 60 * TIME_HZ; - r += lexical_cast (a[1]) * 60 * TIME_HZ; + r += ContentTime::from_seconds (lexical_cast (a[0]) * 60 * 60); + r += ContentTime::from_seconds (lexical_cast (a[1]) * 60); vector b; boost::algorithm::split (b, a[2], boost::is_any_of (",")); - r += lexical_cast (b[0]) * TIME_HZ; - r += lexical_cast (b[1]) * TIME_HZ / 1000; + r += ContentTime::from_seconds (lexical_cast (b[0])); + r += ContentTime::from_seconds (lexical_cast (b[1]) / 1000); return r; } @@ -214,12 +225,11 @@ SubRip::convert_content (list t) return pieces; } -Time +ContentTime SubRip::length () const { - boost::mutex::scoped_lock lm (_mutex); if (_subtitles.empty ()) { - return 0; + return ContentTime (); } return _subtitles.back().to;