Bump libdcp.
[libsub.git] / tools / dumpsubs.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "reader_factory.h"
21 #include "reader.h"
22 #include "collect.h"
23 #include <getopt.h>
24 #include <boost/filesystem.hpp>
25 #include <map>
26 #include <iostream>
27
28 using std::string;
29 using std::cerr;
30 using std::cout;
31 using std::map;
32 using std::list;
33 using boost::shared_ptr;
34 using namespace sub;
35
36 static void
37 help (string n)
38 {
39         cerr << "Syntax: " << n << " <file>\n";
40 }
41
42 int
43 main (int argc, char* argv[])
44 {
45         int option_index = 0;
46         while (1) {
47                 static struct option long_options[] = {
48                         { "help", no_argument, 0, 'h'},
49                         { 0, 0, 0, 0 }
50                 };
51
52                 int c = getopt_long (argc, argv, "h", long_options, &option_index);
53
54                 if (c == -1) {
55                         break;
56                 }
57
58                 switch (c) {
59                 case 'h':
60                         help (argv[0]);
61                         exit (EXIT_SUCCESS);
62                 }
63         }
64
65         if (argc <= optind || argc > (optind + 1)) {
66                 help (argv[0]);
67                 exit (EXIT_FAILURE);
68         }
69
70         if (!boost::filesystem::exists (argv[optind])) {
71                 cerr << argv[0] << ": file " << argv[optind] << " not found.\n";
72                 exit (EXIT_FAILURE);
73         }
74
75         shared_ptr<Reader> reader = reader_factory (argv[optind]);
76         if (!reader) {
77                 cerr << argv[0] << ": could not read subtitle file " << argv[optind] << "\n";
78                 exit (EXIT_FAILURE);
79         }
80
81         map<string, string> metadata = reader->metadata ();
82         for (map<string, string>::const_iterator i = metadata.begin(); i != metadata.end(); ++i) {
83                 cout << i->first << ": " << i->second << "\n";
84         }
85
86         list<sub::Subtitle> subs = collect<list<sub::Subtitle> > (reader->subtitles ());
87         int n = 0;
88         for (list<sub::Subtitle>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
89                 cout << "Subtitle " << n << " at " << i->from << " -> " << i->to << "\n";
90                 for (list<sub::Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
91
92                         cout << "\t";
93
94                         if (j->vertical_position.proportional) {
95                                 cout << j->vertical_position.proportional.get() << " of screen";
96                         } else if (j->vertical_position.line) {
97                                 cout << j->vertical_position.line.get() << " lines of " << j->vertical_position.lines.get();
98                         }
99                         if (j->vertical_position.reference) {
100                                 cout << " from ";
101                                 switch (j->vertical_position.reference.get()) {
102                                 case TOP_OF_SCREEN:
103                                         cout << "top";
104                                         break;
105                                 case VERTICAL_CENTRE_OF_SCREEN:
106                                         cout << "centre";
107                                         break;
108                                 case BOTTOM_OF_SCREEN:
109                                         cout << "bottom";
110                                         break;
111                                 case TOP_OF_SUBTITLE:
112                                         cout << "top of subtitle";
113                                         break;
114                                 }
115                         }
116
117                         cout << "\t";
118                         bool italic = false;
119                         bool underline = false;
120                         for (list<sub::Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
121                                 if (k->italic && !italic) {
122                                         cout << "<i>";
123                                 } else if (italic && !k->italic) {
124                                         cout << "</i>";
125                                 }
126                                 if (k->underline && !underline) {
127                                         cout << "<u>";
128                                 } else if (underline && !k->underline) {
129                                         cout << "</u>";
130                                 }
131
132                                 italic = k->italic;
133                                 underline = k->underline;
134
135                                 cout << k->text;
136                         }
137
138                         if (italic) {
139                                 cout << "</i>";
140                         }
141                         if (underline) {
142                                 cout << "</u>";
143                         }
144                         cout << "\n";
145                 }
146
147                 ++n;
148         }
149
150         return 0;
151 }