Use exceptions to hold errors even in the keep_going case.
[libdcp.git] / tools / dcpdiff.cc
1 #include <iostream>
2 #include <boost/filesystem.hpp>
3 #include <getopt.h>
4 #include "dcp.h"
5 #include "exceptions.h"
6
7 using namespace std;
8 using namespace boost;
9 using namespace dcp;
10
11 static bool verbose = false;
12
13 static void
14 help (string n)
15 {
16         cerr << "Syntax: " << n << " [OPTION] <DCP> <DCP>\n"
17              << "  -V, --version        show libdcp version\n"
18              << "  -h, --help           show this help\n"
19              << "  -v, --verbose        be verbose\n"
20              << "  -n, --names          allow differing MXF names\n"
21              << "  -m, --mean-pixel     maximum allowed mean pixel error (default 5)\n"
22              << "  -s, --std-dev-pixel  maximum allowed standard deviation of pixel error (default 5)\n"
23              << "  -k, --keep-going     carry on in the event of errors, if possible\n"
24              << "\n"
25              << "The <DCP>s are the DCP directories to compare.\n"
26              << "Comparison is of metadata and content, ignoring timestamps\n"
27              << "and differing UUIDs.\n";
28 }
29
30 void
31 note (NoteType t, string n)
32 {
33         if (t == ERROR || verbose) {
34                 cout << " " << n << "\n";
35         }
36 }
37
38 int
39 main (int argc, char* argv[])
40 {
41         EqualityOptions options;
42         options.max_mean_pixel_error = 5;
43         options.max_std_dev_pixel_error = 5;
44         bool keep_going = false;
45         
46         int option_index = 0;
47         while (1) {
48                 static struct option long_options[] = {
49                         { "version", no_argument, 0, 'V'},
50                         { "help", no_argument, 0, 'h'},
51                         { "verbose", no_argument, 0, 'v'},
52                         { "names", no_argument, 0, 'n'},
53                         { "mean-pixel", required_argument, 0, 'm'},
54                         { "std-dev-pixel", required_argument, 0, 's'},
55                         { "keep-going", no_argument, 0, 'k'},
56                         { 0, 0, 0, 0 }
57                 };
58
59                 int c = getopt_long (argc, argv, "Vhvnm:s:k", long_options, &option_index);
60
61                 if (c == -1) {
62                         break;
63                 }
64
65                 switch (c) {
66                 case 'V':
67                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
68                         exit (EXIT_SUCCESS);
69                 case 'h':
70                         help (argv[0]);
71                         exit (EXIT_SUCCESS);
72                 case 'v':
73                         verbose = true;
74                         break;
75                 case 'n':
76                         options.mxf_names_can_differ = true;
77                         break;
78                 case 'm':
79                         options.max_mean_pixel_error = atof (optarg);
80                         break;
81                 case 's':
82                         options.max_std_dev_pixel_error = atof (optarg);
83                         break;
84                 case 'k':
85                         keep_going = true;
86                         break;
87                 }
88         }
89
90         if (argc <= optind || argc > (optind + 2)) {
91                 help (argv[0]);
92                 exit (EXIT_FAILURE);
93         }
94
95         if (!filesystem::exists (argv[optind])) {
96                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
97                 exit (EXIT_FAILURE);
98         }
99
100         if (!filesystem::exists (argv[optind + 1])) {
101                 cerr << argv[0] << ": DCP " << argv[optind + 1] << " not found.\n";
102                 exit (EXIT_FAILURE);
103         }
104
105         DCP* a = 0;
106         try {
107                 a = new DCP (argv[optind]);
108                 list<shared_ptr<DCPReadError> > errors;
109                 a->read (keep_going, &errors);
110                 for (list<shared_ptr<DCPReadError> >::const_iterator i = errors.begin(); i != errors.end(); ++i) {
111                         cerr << (*i)->what() << "\n";
112                 }
113         } catch (FileError& e) {
114                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << " " << e.filename() << "\n";
115                 exit (EXIT_FAILURE);
116         }
117
118         DCP* b = 0;
119         try {
120                 b = new DCP (argv[optind + 1]);
121                 list<shared_ptr<DCPReadError> > errors;
122                 b->read (keep_going, &errors);
123                 for (list<shared_ptr<DCPReadError> >::const_iterator i = errors.begin(); i != errors.end(); ++i) {
124                         cerr << (*i)->what() << "\n";
125                 }
126         } catch (FileError& e) {
127                 cerr << "Could not read DCP " << argv[optind + 1] << "; " << e.what() << " " << e.filename() << "\n";
128                 exit (EXIT_FAILURE);
129         }
130
131         /* I think this is just below the LSB at 16-bits (ie the 8th most significant bit at 24-bit) */
132         options.max_audio_sample_error = 255;
133
134         bool const equals = a->equals (*b, options, boost::bind (note, _1, _2));
135
136         if (equals) {
137                 exit (EXIT_SUCCESS);
138         }
139
140         exit (EXIT_FAILURE);
141 }