2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/lib/file_group.cc
22 * @brief FileGroup class.
27 #include "file_group.h"
28 #include "exceptions.h"
35 /** Construct a FileGroup with no files */
36 FileGroup::FileGroup ()
43 /** Construct a FileGroup with a single file */
44 FileGroup::FileGroup (boost::filesystem::path p)
53 /** Construct a FileGroup with multiple files */
54 FileGroup::FileGroup (vector<boost::filesystem::path> const & p)
63 /** Destroy a FileGroup, closing any open file */
64 FileGroup::~FileGroup ()
67 fclose (_current_file);
72 FileGroup::set_paths (vector<boost::filesystem::path> const & p)
79 /** Ensure that the given path index in the content is the _current_file */
81 FileGroup::ensure_open_path (size_t p) const
83 if (_current_file && _current_path == p) {
89 fclose (_current_file);
93 _current_file = fopen_boost (_paths[_current_path], "rb");
94 if (_current_file == 0) {
95 throw OpenFileError (_paths[_current_path]);
100 FileGroup::seek (int64_t pos, int whence) const
102 /* Convert pos to `full_pos', which is an offset from the start
105 int64_t full_pos = 0;
111 for (size_t i = 0; i < _current_path; ++i) {
112 full_pos += boost::filesystem::file_size (_paths[i]);
114 #ifdef DCPOMATIC_WINDOWS
115 full_pos += _ftelli64 (_current_file);
117 full_pos += ftell (_current_file);
122 full_pos = length() - pos;
126 /* Seek to full_pos */
128 int64_t sub_pos = full_pos;
129 while (i < _paths.size ()) {
130 boost::uintmax_t len = boost::filesystem::file_size (_paths[i]);
131 if (sub_pos < int64_t (len)) {
138 if (i == _paths.size ()) {
142 ensure_open_path (i);
143 dcpomatic_fseek (_current_file, sub_pos, SEEK_SET);
147 /** Try to read some data from the current position into a buffer.
148 * @param buffer Buffer to write data into.
149 * @param amount Number of bytes to read.
150 * @return Number of bytes read, or -1 in the case of error.
153 FileGroup::read (uint8_t* buffer, int amount) const
157 int const this_time = fread (buffer + read, 1, amount - read, _current_file);
159 if (read == amount) {
164 /* See if there is another file to use */
165 if ((_current_path + 1) >= _paths.size()) {
168 ensure_open_path (_current_path + 1);
174 /** @return Combined length of all the files */
176 FileGroup::length () const
179 for (size_t i = 0; i < _paths.size(); ++i) {
180 len += boost::filesystem::file_size (_paths[i]);