Keep signing certificates / keys in config.xml rather than on disk; allow configuration.
[dcpomatic.git] / src / lib / subrip.cc
index 50931e12af030dfdfb383093a14cb9ee46dcf107..11ad3302d3659abf107eed8b8fa69d33d8109c69 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include <boost/algorithm/string.hpp>
+#include <boost/lexical_cast.hpp>
 #include "subrip.h"
 #include "subrip_content.h"
 #include "subrip_subtitle.h"
 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<SubRipContent> content)
+SubRip::SubRip (shared_ptr<const SubRipContent> content)
 {
        FILE* f = fopen_boost (content->path (0), "r");
        if (!f) {
@@ -54,19 +56,28 @@ 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:
                {
+                       if (line.empty ()) {
+                               /* a blank line at the start is ok */
+                               break;
+                       }
+                       
                        int x = 0;
                        try {
                                x = lexical_cast<int> (line);
                        } catch (...) {
 
                        }
-                       
+
                        if (x == next_count) {
                                state = METADATA;
                                ++next_count;
@@ -84,8 +95,7 @@ SubRip::SubRip (shared_ptr<SubRipContent> content)
                                throw SubRipError (line, _("a time/position line"), content->path (0));
                        }
 
-                       current->from = convert_time (p[0]);
-                       current->to = convert_time (p[2]);
+                       current->period = ContentTimePeriod (convert_time (p[0]), convert_time (p[2]));
 
                        if (p.size() > 3) {
                                current->x1 = convert_coordinate (p[3]);
@@ -93,6 +103,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,24 +120,29 @@ SubRip::SubRip (shared_ptr<SubRipContent> 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<string> a;
        boost::algorithm::split (a, t, boost::is_any_of (":"));
        assert (a.size() == 3);
-       r += lexical_cast<int> (a[0]) * 60 * 60 * TIME_HZ;
-       r += lexical_cast<int> (a[1]) * 60 * TIME_HZ;
+       r += ContentTime::from_seconds (lexical_cast<int> (a[0]) * 60 * 60);
+       r += ContentTime::from_seconds (lexical_cast<int> (a[1]) * 60);
 
        vector<string> b;
        boost::algorithm::split (b, a[2], boost::is_any_of (","));
-       r += lexical_cast<int> (b[0]) * TIME_HZ;
-       r += lexical_cast<int> (b[1]) * TIME_HZ / 1000;
+       r += ContentTime::from_seconds (lexical_cast<int> (b[0]));
+       r += ContentTime::from_seconds (lexical_cast<double> (b[1]) / 1000);
 
        return r;
 }
@@ -213,3 +229,13 @@ SubRip::convert_content (list<string> t)
 
        return pieces;
 }
+
+ContentTime
+SubRip::length () const
+{
+       if (_subtitles.empty ()) {
+               return ContentTime ();
+       }
+
+       return _subtitles.back().period.to;
+}