From: Carl Hetherington Date: Thu, 23 Jan 2020 19:58:26 +0000 (+0100) Subject: Move the body of dumpsubs out into a method. X-Git-Tag: v1.6.0~115 X-Git-Url: https://main.carlh.net/gitweb/?p=libsub.git;a=commitdiff_plain;h=944ceca80f4452e6948fd7dd984c549299c49c52 Move the body of dumpsubs out into a method. --- diff --git a/src/util.cc b/src/util.cc index 70cbf10..89af13f 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2012-2015 Carl Hetherington + Copyright (C) 2012-2020 Carl Hetherington 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 @@ -18,13 +18,23 @@ */ #include "util.h" +#include "reader.h" +#include "subtitle.h" +#include "collect.h" +#include #include #include #include +#include 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& line) line = line->substr (3); } } + +void +sub::dump (shared_ptr reader, ostream& os) +{ + map metadata = reader->metadata (); + for (map::const_iterator i = metadata.begin(); i != metadata.end(); ++i) { + os << i->first << ": " << i->second << "\n"; + } + + list subs = collect > (reader->subtitles ()); + int n = 0; + for (list::const_iterator i = subs.begin(); i != subs.end(); ++i) { + os << "Subtitle " << n << " at " << i->from << " -> " << i->to << "\n"; + for (list::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::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) { + if (k->italic && !italic) { + os << ""; + } else if (italic && !k->italic) { + os << ""; + } + if (k->underline && !underline) { + os << ""; + } else if (underline && !k->underline) { + os << ""; + } + + italic = k->italic; + underline = k->underline; + + os << k->text; + } + + if (italic) { + os << ""; + } + if (underline) { + os << ""; + } + os << "\n"; + } + + ++n; + } +} diff --git a/src/util.h b/src/util.h index 9819e3c..738a02a 100644 --- a/src/util.h +++ b/src/util.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2012-2015 Carl Hetherington + Copyright (C) 2012-2020 Carl Hetherington 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 @@ -26,9 +26,12 @@ namespace sub { +class Reader; + extern bool empty_or_white_space (std::string s); extern void remove_unicode_bom (boost::optional& line); extern boost::optional get_line_file (FILE* f); extern boost::optional get_line_string (std::string* s); +extern void dump (boost::shared_ptr read, std::ostream& os); } diff --git a/tools/dumpsubs.cc b/tools/dumpsubs.cc index 991d018..de1f13c 100644 --- a/tools/dumpsubs.cc +++ b/tools/dumpsubs.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2014-2015 Carl Hetherington + Copyright (C) 2014-2020 Carl Hetherington 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 #include #include @@ -78,74 +79,7 @@ main (int argc, char* argv[]) exit (EXIT_FAILURE); } - map metadata = reader->metadata (); - for (map::const_iterator i = metadata.begin(); i != metadata.end(); ++i) { - cout << i->first << ": " << i->second << "\n"; - } - - list subs = collect > (reader->subtitles ()); - int n = 0; - for (list::const_iterator i = subs.begin(); i != subs.end(); ++i) { - cout << "Subtitle " << n << " at " << i->from << " -> " << i->to << "\n"; - for (list::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::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) { - if (k->italic && !italic) { - cout << ""; - } else if (italic && !k->italic) { - cout << ""; - } - if (k->underline && !underline) { - cout << ""; - } else if (underline && !k->underline) { - cout << ""; - } - - italic = k->italic; - underline = k->underline; - - cout << k->text; - } - - if (italic) { - cout << ""; - } - if (underline) { - cout << ""; - } - cout << "\n"; - } - - ++n; - } + sub::dump (reader, cout); return 0; }