Fix some coverity-reported stuff.
[dcpomatic.git] / src / tools / dcpomatic_create.cc
1 /*
2     Copyright (C) 2013 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 <string>
21 #include <iostream>
22 #include <cstdlib>
23 #include <stdexcept>
24 #include <getopt.h>
25 #include <boost/filesystem.hpp>
26 #include "lib/version.h"
27 #include "lib/film.h"
28 #include "lib/util.h"
29 #include "lib/content_factory.h"
30 #include "lib/job_manager.h"
31 #include "lib/ui_signaller.h"
32 #include "lib/job.h"
33
34 using std::string;
35 using std::cout;
36 using std::cerr;
37 using std::list;
38 using std::exception;
39 using boost::shared_ptr;
40
41 static void
42 help (string n)
43 {
44         cerr << "Create a film directory (ready for making a DCP) from some content files.\n"
45              << "Syntax: " << n << " [OPTION] <CONTENT> [<CONTENT> ...]\n"
46              << "  -v, --version   show DCP-o-matic version\n"
47              << "  -h, --help      show this help\n"
48              << "  -n, --name      film name\n"
49              << "  -o, --output    output directory (required)\n";
50 }
51
52 int
53 main (int argc, char* argv[])
54 {
55         string name;
56         boost::filesystem::path output;
57         
58         int option_index = 0;
59         while (1) {
60                 static struct option long_options[] = {
61                         { "version", no_argument, 0, 'v'},
62                         { "help", no_argument, 0, 'h'},
63                         { "name", required_argument, 0, 'n'},
64                         { "output", required_argument, 0, 'o'},
65                         { 0, 0, 0, 0}
66                 };
67
68                 int c = getopt_long (argc, argv, "vhn:o:", long_options, &option_index);
69                 if (c == -1) {
70                         break;
71                 }
72
73                 switch (c) {
74                 case 'v':
75                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
76                         exit (EXIT_SUCCESS);
77                 case 'h':
78                         help (argv[0]);
79                         exit (EXIT_SUCCESS);
80                 case 'n':
81                         name = optarg;
82                         break;
83                 case 'o':
84                         output = optarg;
85                         break;
86                 }
87         }
88
89         if (optind > argc) {
90                 help (argv[0]);
91                 exit (EXIT_FAILURE);
92         }
93
94         if (output.empty ()) {
95                 cerr << "Missing required option -o or --output.\n"
96                      << "Use " << argv[0] << " --help for help.\n";
97                 exit (EXIT_FAILURE);
98         }
99
100         dcpomatic_setup ();
101         ui_signaller = new UISignaller ();
102
103         try {
104                 shared_ptr<Film> film (new Film (output));
105                 if (!name.empty ()) {
106                         film->set_name (name);
107                 }
108                 
109                 for (int i = optind; i < argc; ++i) {
110                         film->examine_and_add_content (content_factory (film, argv[i]));
111                 }
112                 
113                 JobManager* jm = JobManager::instance ();
114                 while (jm->work_to_do ()) {
115                         ui_signaller->ui_idle ();
116                 }
117                 
118                 if (jm->errors ()) {
119                         list<shared_ptr<Job> > jobs = jm->get ();
120                         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
121                                 if ((*i)->finished_in_error ()) {
122                                         cerr << (*i)->error_summary () << "\n"
123                                              << (*i)->error_details () << "\n";
124                                 }
125                         }
126                         exit (EXIT_FAILURE);
127                 }
128                 
129                 film->write_metadata ();
130         } catch (exception& e) {
131                 cerr << argv[0] << ": " << e.what() << "\n";
132                 exit (EXIT_FAILURE);
133         }
134                 
135         return 0;
136 }