Move things round a bit.
[dcpomatic.git] / src / lib / dvd.cc
1 /*
2     Copyright (C) 2012 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 <fstream>
21 #include <boost/filesystem.hpp>
22 #include <boost/algorithm/string.hpp>
23
24 using namespace std;
25 using namespace boost;
26
27 string
28 find_dvd ()
29 {
30         ifstream f ("/etc/mtab");
31         while (f.good ()) {
32                 string s;
33                 getline (f, s);
34                 vector<string> b;
35                 split (b, s, is_any_of (" "));
36                 if (b.size() >= 3 && b[2] == "udf") {
37                         replace_all (b[1], "\\040", " ");
38                         return b[1];
39                 }
40         }
41
42         return "";
43 }
44
45 vector<uint64_t>
46 dvd_titles (string dvd)
47 {
48         filesystem::path video (dvd);
49         video /= "VIDEO_TS";
50
51         vector<uint64_t> sizes;
52         
53         for (filesystem::directory_iterator i = filesystem::directory_iterator (video); i != filesystem::directory_iterator(); ++i) {
54 #if BOOST_FILESYSTEM_VERSION == 3               
55                 string const n = filesystem::path(*i).filename().generic_string();
56 #else           
57                 string const n = filesystem::path(*i).filename();
58 #endif
59                 if (starts_with (n, "VTS_") && ends_with (n, ".VOB")) {
60                         uint64_t const size = filesystem::file_size (filesystem::path (*i));
61                         vector<string> p;
62                         split (p, n, is_any_of ("_."));
63                         if (p.size() == 4) {
64                                 int const a = atoi (p[1].c_str ());
65                                 int const b = atoi (p[2].c_str ());
66                                 while (a >= int (sizes.size())) {
67                                         sizes.push_back (0);
68                                 }
69
70                                 if (b > 0) {
71                                         sizes[a] += size;
72                                 }
73                         }
74                 }
75         }
76                                         
77         return sizes;           
78 }