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