Fix a number of missing fopen_boost replacements; might fix problems with adding...
[dcpomatic.git] / src / lib / file_group.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 <cstdio>
21 #include <sndfile.h>
22 #include "file_group.h"
23 #include "exceptions.h"
24 #include "cross.h"
25
26 using std::vector;
27 using std::cout;
28
29 FileGroup::FileGroup ()
30         : _current_path (0)
31         , _current_file (0)
32 {
33
34 }
35
36 FileGroup::FileGroup (boost::filesystem::path p)
37         : _current_path (0)
38         , _current_file (0)
39 {
40         _paths.push_back (p);
41         seek (0, SEEK_SET);
42 }
43
44 FileGroup::FileGroup (vector<boost::filesystem::path> const & p)
45         : _paths (p)
46         , _current_path (0)
47         , _current_file (0)
48 {
49         ensure_open_path (0);
50         seek (0, SEEK_SET);
51 }
52
53 FileGroup::~FileGroup ()
54 {
55         if (_current_file) {
56                 fclose (_current_file);
57         }
58 }
59
60 void
61 FileGroup::set_paths (vector<boost::filesystem::path> const & p)
62 {
63         _paths = p;
64         ensure_open_path (0);
65         seek (0, SEEK_SET);
66 }
67
68 /** Ensure that the given path index in the content is the _current_file */
69 void
70 FileGroup::ensure_open_path (size_t p) const
71 {
72         if (_current_file && _current_path == p) {
73                 /* Already open */
74                 return;
75         }
76         
77         if (_current_file) {
78                 fclose (_current_file);
79         }
80
81         _current_path = p;
82         _current_file = fopen_boost (_paths[_current_path], "rb");
83         if (_current_file == 0) {
84                 throw OpenFileError (_paths[_current_path]);
85         }
86 }
87
88 int64_t
89 FileGroup::seek (int64_t pos, int whence) const
90 {
91         /* Convert pos to `full_pos', which is an offset from the start
92            of all the files.
93         */
94         int64_t full_pos = 0;
95         switch (whence) {
96         case SEEK_SET:
97                 full_pos = pos;
98                 break;
99         case SEEK_CUR:
100                 for (size_t i = 0; i < _current_path; ++i) {
101                         full_pos += boost::filesystem::file_size (_paths[i]);
102                 }
103                 full_pos += ftell (_current_file);
104                 full_pos += pos;
105                 break;
106         case SEEK_END:
107                 full_pos = length() - pos;
108                 break;
109         }
110
111         /* Seek to full_pos */
112         size_t i = 0;
113         int64_t sub_pos = full_pos;
114         while (i < _paths.size ()) {
115                 boost::uintmax_t len = boost::filesystem::file_size (_paths[i]);
116                 if (sub_pos < int64_t (len)) {
117                         break;
118                 }
119                 sub_pos -= len;
120                 ++i;
121         }
122
123         if (i == _paths.size ()) {
124                 return -1;
125         }
126
127         ensure_open_path (i);
128         fseek (_current_file, sub_pos, SEEK_SET);
129         return full_pos;
130 }
131
132 /** Try to read some data from the current position into a buffer.
133  *  @param buffer Buffer to write data into.
134  *  @param amount Number of bytes to read.
135  *  @return Number of bytes read, or -1 in the case of error.
136  */
137 int
138 FileGroup::read (uint8_t* buffer, int amount) const
139 {
140         int read = 0;
141         while (1) {
142                 int const this_time = fread (buffer + read, 1, amount - read, _current_file);
143                 read += this_time;
144                 if (read == amount) {
145                         /* Done */
146                         break;
147                 }
148
149                 /* See if there is another file to use */
150                 if ((_current_path + 1) >= _paths.size()) {
151                         break;
152                 }
153                 ensure_open_path (_current_path + 1);
154         }
155
156         return read;
157 }
158
159 int64_t
160 FileGroup::length () const
161 {
162         int64_t len = 0;
163         for (size_t i = 0; i < _paths.size(); ++i) {
164                 len += boost::filesystem::file_size (_paths[i]);
165         }
166
167         return len;
168 }