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