Move the body of dumpsubs out into a method.
authorCarl Hetherington <cth@carlh.net>
Thu, 23 Jan 2020 19:58:26 +0000 (20:58 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 23 Jan 2020 19:58:26 +0000 (20:58 +0100)
src/util.cc
src/util.h
tools/dumpsubs.cc

index 70cbf1020be0e0aaa8b8976424d95f54b89a5a00..89af13fb0d33dd8820e338ebc9ec38b8d2f61d9c 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2020 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
 */
 
 #include "util.h"
+#include "reader.h"
+#include "subtitle.h"
+#include "collect.h"
+#include <boost/shared_ptr.hpp>
 #include <string>
 #include <iostream>
 #include <cstdio>
+#include <map>
 
 using std::string;
 using std::getline;
+using std::ostream;
+using std::map;
+using std::list;
 using boost::optional;
+using boost::shared_ptr;
+using namespace sub;
 
 /** @param s A string.
  *  @return true if the string contains only space, newline or tab characters, or is empty.
@@ -86,3 +96,76 @@ sub::remove_unicode_bom (optional<string>& line)
                line = line->substr (3);
        }
 }
+
+void
+sub::dump (shared_ptr<const Reader> reader, ostream& os)
+{
+       map<string, string> metadata = reader->metadata ();
+       for (map<string, string>::const_iterator i = metadata.begin(); i != metadata.end(); ++i) {
+               os << i->first << ": " << i->second << "\n";
+       }
+
+       list<sub::Subtitle> subs = collect<list<sub::Subtitle> > (reader->subtitles ());
+       int n = 0;
+       for (list<sub::Subtitle>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
+               os << "Subtitle " << n << " at " << i->from << " -> " << i->to << "\n";
+               for (list<sub::Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
+
+                       os << "\t";
+
+                       if (j->vertical_position.proportional) {
+                               os << j->vertical_position.proportional.get() << " of screen";
+                       } else if (j->vertical_position.line && j->vertical_position.lines) {
+                               os << j->vertical_position.line.get() << " lines of " << j->vertical_position.lines.get();
+                       }
+                       if (j->vertical_position.reference) {
+                               os << " from ";
+                               switch (j->vertical_position.reference.get()) {
+                               case TOP_OF_SCREEN:
+                                       os << "top";
+                                       break;
+                               case VERTICAL_CENTRE_OF_SCREEN:
+                                       os << "centre";
+                                       break;
+                               case BOTTOM_OF_SCREEN:
+                                       os << "bottom";
+                                       break;
+                               case TOP_OF_SUBTITLE:
+                                       os << "top of subtitle";
+                                       break;
+                               }
+                       }
+
+                       os << "\t";
+                       bool italic = false;
+                       bool underline = false;
+                       for (list<sub::Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
+                               if (k->italic && !italic) {
+                                       os << "<i>";
+                               } else if (italic && !k->italic) {
+                                       os << "</i>";
+                               }
+                               if (k->underline && !underline) {
+                                       os << "<u>";
+                               } else if (underline && !k->underline) {
+                                       os << "</u>";
+                               }
+
+                               italic = k->italic;
+                               underline = k->underline;
+
+                               os << k->text;
+                       }
+
+                       if (italic) {
+                               os << "</i>";
+                       }
+                       if (underline) {
+                               os << "</u>";
+                       }
+                       os << "\n";
+               }
+
+               ++n;
+       }
+}
index 9819e3cc345108c044ae3a9eba25a1dad3f807c2..738a02af5bb4817786c23b4a5d56af3919dc3302 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2020 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
 
 namespace sub {
 
+class Reader;
+
 extern bool empty_or_white_space (std::string s);
 extern void remove_unicode_bom (boost::optional<std::string>& line);
 extern boost::optional<std::string> get_line_file (FILE* f);
 extern boost::optional<std::string> get_line_string (std::string* s);
+extern void dump (boost::shared_ptr<const Reader> read, std::ostream& os);
 
 }
index 991d018dfc750aa5c7b3bf0a9ac73d3d72d76e3b..de1f13ce81bb2eae289d5bd6b2e48c03f43dcbb3 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2020 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
@@ -20,6 +20,7 @@
 #include "reader_factory.h"
 #include "reader.h"
 #include "collect.h"
+#include "util.h"
 #include <getopt.h>
 #include <boost/filesystem.hpp>
 #include <map>
@@ -78,74 +79,7 @@ main (int argc, char* argv[])
                exit (EXIT_FAILURE);
        }
 
-       map<string, string> metadata = reader->metadata ();
-       for (map<string, string>::const_iterator i = metadata.begin(); i != metadata.end(); ++i) {
-               cout << i->first << ": " << i->second << "\n";
-       }
-
-       list<sub::Subtitle> subs = collect<list<sub::Subtitle> > (reader->subtitles ());
-       int n = 0;
-       for (list<sub::Subtitle>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
-               cout << "Subtitle " << n << " at " << i->from << " -> " << i->to << "\n";
-               for (list<sub::Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
-
-                       cout << "\t";
-
-                       if (j->vertical_position.proportional) {
-                               cout << j->vertical_position.proportional.get() << " of screen";
-                       } else if (j->vertical_position.line && j->vertical_position.lines) {
-                               cout << j->vertical_position.line.get() << " lines of " << j->vertical_position.lines.get();
-                       }
-                       if (j->vertical_position.reference) {
-                               cout << " from ";
-                               switch (j->vertical_position.reference.get()) {
-                               case TOP_OF_SCREEN:
-                                       cout << "top";
-                                       break;
-                               case VERTICAL_CENTRE_OF_SCREEN:
-                                       cout << "centre";
-                                       break;
-                               case BOTTOM_OF_SCREEN:
-                                       cout << "bottom";
-                                       break;
-                               case TOP_OF_SUBTITLE:
-                                       cout << "top of subtitle";
-                                       break;
-                               }
-                       }
-
-                       cout << "\t";
-                       bool italic = false;
-                       bool underline = false;
-                       for (list<sub::Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
-                               if (k->italic && !italic) {
-                                       cout << "<i>";
-                               } else if (italic && !k->italic) {
-                                       cout << "</i>";
-                               }
-                               if (k->underline && !underline) {
-                                       cout << "<u>";
-                               } else if (underline && !k->underline) {
-                                       cout << "</u>";
-                               }
-
-                               italic = k->italic;
-                               underline = k->underline;
-
-                               cout << k->text;
-                       }
-
-                       if (italic) {
-                               cout << "</i>";
-                       }
-                       if (underline) {
-                               cout << "</u>";
-                       }
-                       cout << "\n";
-               }
-
-               ++n;
-       }
+       sub::dump (reader, cout);
 
        return 0;
 }