94470e8e958ac363491714a871e82f56782939a2
[libsub.git] / tools / dumpsubs.cc
1 /*
2     Copyright (C) 2014-2020 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 "util.h"
24 #include <getopt.h>
25 #include <boost/filesystem.hpp>
26 #include <map>
27 #include <iostream>
28
29 using std::string;
30 using std::cerr;
31 using std::cout;
32 using std::map;
33 using std::list;
34 using std::shared_ptr;
35 using namespace sub;
36
37 static void
38 help (string n)
39 {
40         cerr << "Syntax: " << n << " <file>\n";
41 }
42
43 int
44 main (int argc, char* argv[])
45 {
46         int option_index = 0;
47         while (1) {
48                 static struct option long_options[] = {
49                         { "help", no_argument, 0, 'h'},
50                         { 0, 0, 0, 0 }
51                 };
52
53                 int c = getopt_long (argc, argv, "h", long_options, &option_index);
54
55                 if (c == -1) {
56                         break;
57                 }
58
59                 switch (c) {
60                 case 'h':
61                         help (argv[0]);
62                         exit (EXIT_SUCCESS);
63                 }
64         }
65
66         if (argc <= optind || argc > (optind + 1)) {
67                 help (argv[0]);
68                 exit (EXIT_FAILURE);
69         }
70
71         if (!boost::filesystem::exists (argv[optind])) {
72                 cerr << argv[0] << ": file " << argv[optind] << " not found.\n";
73                 exit (EXIT_FAILURE);
74         }
75
76         shared_ptr<Reader> reader = reader_factory (argv[optind]);
77         if (!reader) {
78                 cerr << argv[0] << ": could not read subtitle file " << argv[optind] << "\n";
79                 exit (EXIT_FAILURE);
80         }
81
82         sub::dump (reader, cout);
83
84         return 0;
85 }