c++11 tidying.
[libsub.git] / src / stl_text_reader.cc
index 0b4141c2f6ad7e50f84dfd4af00d1df474798555..c7b1fcfd91198d01c04d183afb2e55e40eb5d11b 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -22,6 +22,7 @@
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <vector>
+#include <iostream>
 
 using std::list;
 using std::ostream;
@@ -38,8 +39,12 @@ using namespace sub;
 
 STLTextReader::STLTextReader (istream& in)
 {
+       /* This reader extracts no information about where the subtitle
+          should be on screen, so its reference is TOP_OF_SUBTITLE.
+       */
        _subtitle.vertical_position.line = 0;
-       
+       _subtitle.vertical_position.reference = TOP_OF_SUBTITLE;
+
        while (in.good ()) {
                string line;
                getline (in, line);
@@ -74,7 +79,7 @@ STLTextReader::STLTextReader (istream& in)
                        if (divider[0] != string::npos) {
                                divider[1] = line.find_first_of (",", divider[0] + 1);
                        }
-                       
+
                        if (divider[0] == string::npos || divider[1] == string::npos || divider[0] <= 1 || divider[1] >= line.length() - 1) {
                                warn (String::compose ("Unrecognised line %1", line));
                                continue;
@@ -85,97 +90,84 @@ STLTextReader::STLTextReader (istream& in)
                        string to_string = line.substr (divider[0] + 1, divider[1] - divider[0] - 1);
                        trim (to_string);
 
-                       optional<FrameTime> from = time (from_string);
-                       optional<FrameTime> to = time (to_string);
+                       optional<Time> from = time (from_string);
+                       optional<Time> to = time (to_string);
 
                        if (!from || !to) {
                                warn (String::compose ("Unrecognised line %1", line));
                                continue;
                        }
 
-                       _subtitle.from.set_frame (from.get ());
-                       _subtitle.to.set_frame (to.get ());
+                       _subtitle.from = from.get ();
+                       _subtitle.to = to.get ();
 
                        /* Parse ^B/^I/^U */
                        string text = line.substr (divider[1] + 1);
                        for (size_t i = 0; i < text.length(); ++i) {
                                if (text[i] == '|') {
-                                       maybe_push_subtitle ();
+                                       maybe_push ();
                                        _subtitle.vertical_position.line = _subtitle.vertical_position.line.get() + 1;
                                } else if (text[i] == '^') {
-                                       maybe_push_block ();
+                                       maybe_push ();
                                        if ((i + 1) < text.length()) {
                                                switch (text[i + 1]) {
                                                case 'B':
-                                                       _block.bold = !_block.bold;
+                                                       _subtitle.bold = !_subtitle.bold;
                                                        break;
                                                case 'I':
-                                                       _block.italic = !_block.italic;
+                                                       _subtitle.italic = !_subtitle.italic;
                                                        break;
                                                case 'U':
-                                                       _block.underline = !_block.underline;
+                                                       _subtitle.underline = !_subtitle.underline;
                                                        break;
                                                }
                                        }
                                        ++i;
                                } else {
-                                       _block.text += text[i];
+                                       _subtitle.text += text[i];
                                }
                        }
 
-                       maybe_push_subtitle ();
+                       maybe_push ();
                }
        }
 }
 
-optional<FrameTime>
+optional<Time>
 STLTextReader::time (string t) const
 {
        vector<string> b;
        split (b, t, is_any_of (":"));
        if (b.size() != 4) {
                warn (String::compose ("Unrecognised time %1", t));
-               return optional<FrameTime> ();
+               return optional<Time> ();
        }
 
-       return FrameTime (lexical_cast<int> (b[0]), lexical_cast<int> (b[1]), lexical_cast<int> (b[2]), lexical_cast<int> (b[3]));
+       return sub::Time::from_hmsf (lexical_cast<int> (b[0]), lexical_cast<int> (b[1]), lexical_cast<int> (b[2]), lexical_cast<int> (b[3]));
 }
 
 void
 STLTextReader::set (string name, string value)
 {
        if (name == "$FontName") {
-               _block.font = value;
+               _subtitle.font = value;
        } else if (name == "$Bold") {
-               _block.bold = value == "True";
+               _subtitle.bold = value == "True";
        } else if (name == "$Italic") {
-               _block.italic = value == "True";
+               _subtitle.italic = value == "True";
        } else if (name == "$Underlined") {
-               _block.underline = value == "True";
+               _subtitle.underline = value == "True";
        } else if (name == "$FontSize") {
-               _block.font_size.set_points (lexical_cast<int> (value));
+               _subtitle.font_size.set_points (lexical_cast<int> (value));
        }
 }
 
 void
-STLTextReader::maybe_push_subtitle ()
+STLTextReader::maybe_push ()
 {
-       maybe_push_block ();
-       
-       if (!_subtitle.blocks.empty ()) {
+       if (!_subtitle.text.empty ()) {
                _subs.push_back (_subtitle);
-               _subtitle.blocks.clear ();
+               _subtitle.text.clear ();
                _subtitle.vertical_position.line = 0;
        }
 }
-
-void
-STLTextReader::maybe_push_block ()
-{
-       if (!_block.text.empty ()) {
-               _subtitle.blocks.push_back (_block);
-               _block.text.clear ();
-       }
-}
-
-