Merge branch 'master' of ssh://houllier/home/carl/git/dvdomatic
[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 #include "dvd.h"
24
25 using namespace std;
26 using namespace boost;
27
28 string
29 find_dvd ()
30 {
31         ifstream f ("/etc/mtab");
32         while (f.good ()) {
33                 string s;
34                 getline (f, s);
35                 vector<string> b;
36                 split (b, s, is_any_of (" "));
37                 if (b.size() >= 3 && b[2] == "udf") {
38                         replace_all (b[1], "\\040", " ");
39                         return b[1];
40                 }
41         }
42
43         return "";
44 }
45
46 list<DVDTitle>
47 dvd_titles (string dvd)
48 {
49         filesystem::path video (dvd);
50         video /= "VIDEO_TS";
51
52         list<DVDTitle> titles;
53         
54         for (filesystem::directory_iterator i = filesystem::directory_iterator (video); i != filesystem::directory_iterator(); ++i) {
55 #if BOOST_FILESYSTEM_VERSION == 3               
56                 string const n = filesystem::path(*i).filename().generic_string();
57 #else           
58                 string const n = filesystem::path(*i).filename();
59 #endif
60                 if (starts_with (n, "VTS_") && ends_with (n, ".VOB")) {
61                         uint64_t const size = filesystem::file_size (filesystem::path (*i));
62                         vector<string> p;
63                         split (p, n, is_any_of ("_."));
64                         if (p.size() == 4) {
65                                 int const a = atoi (p[1].c_str ());
66                                 int const b = atoi (p[2].c_str ());
67                                 if (b == 0) {
68                                         continue;
69                                 }
70
71                                 list<DVDTitle>::iterator j = titles.begin ();
72                                 while (j != titles.end() && j->number != a) {
73                                         ++j;
74                                 }
75
76                                 if (j == titles.end ()) {
77                                         titles.push_back (DVDTitle (a, size));
78                                 } else {
79                                         j->size += size;
80                                 }
81                         }
82                 }
83         }
84
85         titles.sort ();
86                                         
87         return titles;
88 }
89
90
91 bool
92 operator< (DVDTitle const & a, DVDTitle const & b)
93 {
94         return a.number < b.number;
95 }