Add exception missing from previous commit.
[libsub.git] / src / util.cc
index 70cbf1020be0e0aaa8b8976424d95f54b89a5a00..eb68752c4adf11e6637cb088123896bedfd32eca 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 <memory>
 #include <string>
 #include <iostream>
 #include <cstdio>
+#include <map>
 
 using std::string;
 using std::getline;
+using std::ostream;
+using std::map;
+using std::list;
+using std::shared_ptr;
+using std::vector;
 using boost::optional;
+using namespace sub;
 
 /** @param s A string.
  *  @return true if the string contains only space, newline or tab characters, or is empty.
@@ -86,3 +97,76 @@ sub::remove_unicode_bom (optional<string>& line)
                line = line->substr (3);
        }
 }
+
+void
+sub::dump (shared_ptr<const Reader> reader, ostream& os)
+{
+       auto metadata = reader->metadata ();
+       for (auto const& i: metadata) {
+               os << i.first << ": " << i.second << "\n";
+       }
+
+       auto subs = collect<vector<sub::Subtitle>> (reader->subtitles());
+       int n = 0;
+       for (auto const& i: subs) {
+               os << "Subtitle " << n << " at " << i.from << " -> " << i.to << "\n";
+               for (auto const& j: i.lines) {
+
+                       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 (auto const& k: j.blocks) {
+                               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;
+       }
+}