Check sound; various fixups.
[libdcp.git] / tools / dcpdiff.cc
1 #include <boost/filesystem.hpp>
2 #include <getopt.h>
3 #include "dcp.h"
4 #include "exceptions.h"
5
6 using namespace std;
7 using namespace boost;
8 using namespace libdcp;
9
10 static void
11 help (string n)
12 {
13         cerr << "Syntax: " << n << " [OPTION] <DCP> <DCP>\n"
14              << "  -b, --bitwise      bitwise check\n"
15              << "  -v, --version      show libdcp version\n"
16              << "  -h, --help         show this help\n"
17              << "\n"
18              << "The <DCP>s are the DCP directories to compare.\n"
19              << "Default is to compare metadata and content ignoring timestamps\n"
20              << "and differing UUIDs.  Pass -b to perform a bitwise comparison.\n";
21 }
22
23 int
24 main (int argc, char* argv[])
25 {
26         bool bitwise = false;
27         
28         int option_index = 0;
29         while (1) {
30                 static struct option long_options[] = {
31                         { "bitwise", no_argument, 0, 'b'},
32                         { "version", no_argument, 0, 'v'},
33                         { "help", no_argument, 0, 'h'},
34                         { 0, 0, 0, 0 }
35                 };
36
37                 int c = getopt_long (argc, argv, "bvh", long_options, &option_index);
38
39                 if (c == -1) {
40                         break;
41                 }
42
43                 switch (c) {
44                 case 'b':
45                         bitwise = true;
46                         break;
47                 case 'v':
48                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
49                         exit (EXIT_SUCCESS);
50                 case 'h':
51                         help (argv[0]);
52                         exit (EXIT_SUCCESS);
53                 }
54         }
55
56         if (argc <= optind || argc > (optind + 2)) {
57                 help (argv[0]);
58                 exit (EXIT_FAILURE);
59         }
60
61         if (!filesystem::exists (argv[optind])) {
62                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
63                 exit (EXIT_FAILURE);
64         }
65
66         if (!filesystem::exists (argv[optind + 1])) {
67                 cerr << argv[0] << ": DCP " << argv[optind + 1] << " not found.\n";
68                 exit (EXIT_FAILURE);
69         }
70
71         DCP* a = 0;
72         try {
73                 a = new DCP (argv[optind]);
74         } catch (FileError& e) {
75                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << " " << e.filename() << "\n";
76                 exit (EXIT_FAILURE);
77         }
78
79         DCP* b = 0;
80         try {
81                 b = new DCP (argv[optind + 1]);
82         } catch (FileError& e) {
83                 cerr << "Could not read DCP " << argv[optind + 1] << "; " << e.what() << " " << e.filename() << "\n";
84                 exit (EXIT_FAILURE);
85         }
86
87         EqualityFlags flags = EqualityFlags (LIBDCP_METADATA | MXF_INSPECT);
88         if (bitwise) {
89                 flags = EqualityFlags (flags | MXF_BITWISE);
90         }
91
92         list<string> notes = a->equals (*b, flags);
93         if (notes.empty ()) {
94                 cout << "DCPs identical\n";
95                 exit (EXIT_SUCCESS);
96         }
97
98         for (list<string>::iterator i = notes.begin(); i != notes.end(); ++i) {
99                 cout << "  " << *i << "\n";
100         }
101
102         exit (EXIT_FAILURE);
103 }