aaf94acf4b17a2eaf25595ba61fe9c4d5d964ed4
[dcpomatic.git] / src / lib / file_group.cc
1 /*
2     Copyright (C) 2013-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/lib/file_group.cc
22  *  @brief FileGroup class.
23  */
24
25 #include "file_group.h"
26 #include "exceptions.h"
27 #include "cross.h"
28 #include "compose.hpp"
29 #include "dcpomatic_assert.h"
30 #include <sndfile.h>
31 #include <cstdio>
32
33 using std::vector;
34
35 /** Construct a FileGroup with no files */
36 FileGroup::FileGroup ()
37         : _current_path (0)
38         , _current_file (0)
39         , _current_size (0)
40         , _position (0)
41 {
42
43 }
44
45 /** Construct a FileGroup with a single file */
46 FileGroup::FileGroup (boost::filesystem::path p)
47         : _current_path (0)
48         , _current_file (0)
49         , _current_size (0)
50 {
51         _paths.push_back (p);
52         ensure_open_path (0);
53         seek (0, SEEK_SET);
54 }
55
56 /** Construct a FileGroup with multiple files */
57 FileGroup::FileGroup (vector<boost::filesystem::path> const & p)
58         : _paths (p)
59         , _current_path (0)
60         , _current_file (0)
61 {
62         ensure_open_path (0);
63         seek (0, SEEK_SET);
64 }
65
66 /** Destroy a FileGroup, closing any open file */
67 FileGroup::~FileGroup ()
68 {
69         if (_current_file) {
70                 fclose (_current_file);
71         }
72 }
73
74 void
75 FileGroup::set_paths (vector<boost::filesystem::path> const & p)
76 {
77         _paths = p;
78         ensure_open_path (0);
79         seek (0, SEEK_SET);
80 }
81
82 /** Ensure that the given path index in the content is the _current_file */
83 void
84 FileGroup::ensure_open_path (size_t p) const
85 {
86         if (_current_file && _current_path == p) {
87                 /* Already open */
88                 return;
89         }
90
91         if (_current_file) {
92                 fclose (_current_file);
93         }
94
95         _current_path = p;
96         _current_file = fopen_boost (_paths[_current_path], "rb");
97         if (_current_file == 0) {
98                 throw OpenFileError (_paths[_current_path], errno, OpenFileError::READ);
99         }
100         _current_size = boost::filesystem::file_size (_paths[_current_path]);
101 }
102
103 int64_t
104 FileGroup::seek (int64_t pos, int whence) const
105 {
106         switch (whence) {
107         case SEEK_SET:
108                 _position = pos;
109                 break;
110         case SEEK_CUR:
111                 _position += pos;
112                 break;
113         case SEEK_END:
114                 _position = length() - pos;
115                 break;
116         }
117
118         /* Find an offset within one of the files, if _position is within a file */
119         size_t i = 0;
120         int64_t sub_pos = _position;
121         while (i < _paths.size()) {
122                 boost::uintmax_t len = boost::filesystem::file_size (_paths[i]);
123                 if (sub_pos < int64_t(len)) {
124                         break;
125                 }
126                 sub_pos -= int64_t(len);
127                 ++i;
128         }
129
130         if (i < _paths.size()) {
131                 ensure_open_path (i);
132                 dcpomatic_fseek (_current_file, sub_pos, SEEK_SET);
133         } else {
134                 ensure_open_path (_paths.size() - 1);
135                 dcpomatic_fseek (_current_file, _current_size, SEEK_SET);
136         }
137
138         return _position;
139 }
140
141 /** Try to read some data from the current position into a buffer.
142  *  @param buffer Buffer to write data into.
143  *  @param amount Number of bytes to read.
144  *  @return Number of bytes read.
145  */
146 int
147 FileGroup::read (uint8_t* buffer, int amount) const
148 {
149         int read = 0;
150         while (true) {
151
152                 bool eof = false;
153                 size_t to_read = amount - read;
154
155                 DCPOMATIC_ASSERT (_current_file);
156
157 #ifdef DCPOMATIC_WINDOWS
158                 int64_t const current_position = _ftelli64 (_current_file);
159                 if (current_position == -1) {
160                         to_read = 0;
161                         eof = true;
162                 } else if ((current_position + to_read) > _current_size) {
163                         to_read = _current_size - current_position;
164                         eof = true;
165                 }
166 #else
167                 long const current_position = ftell(_current_file);
168                 if ((current_position + to_read) > _current_size) {
169                         to_read = _current_size - current_position;
170                         eof = true;
171                 }
172 #endif
173
174                 int const this_time = fread (buffer + read, 1, to_read, _current_file);
175                 read += this_time;
176                 _position += this_time;
177                 if (read == amount) {
178                         /* Done */
179                         break;
180                 }
181
182                 if (ferror(_current_file)) {
183                         throw FileError (String::compose("fread error %1", errno), _paths[_current_path]);
184                 }
185
186                 if (eof) {
187                         /* See if there is another file to use */
188                         if ((_current_path + 1) >= _paths.size()) {
189                                 break;
190                         }
191                         ensure_open_path (_current_path + 1);
192                 }
193         }
194
195         return read;
196 }
197
198 /** @return Combined length of all the files */
199 int64_t
200 FileGroup::length () const
201 {
202         int64_t len = 0;
203         for (size_t i = 0; i < _paths.size(); ++i) {
204                 len += boost::filesystem::file_size (_paths[i]);
205         }
206
207         return len;
208 }