Small FileGroup tidy-up; actually build FileGroup test; rearrange test wscript.
[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         /* Convert pos to `full_pos', which is an offset from the start
77            of all the files.
78         */
79         int64_t full_pos = 0;
80         switch (whence) {
81         case SEEK_SET:
82                 full_pos = pos;
83                 break;
84         case SEEK_CUR:
85                 for (size_t i = 0; i < _current_path; ++i) {
86                         full_pos += boost::filesystem::file_size (_paths[i]);
87                 }
88                 full_pos += ftell (_current_file);
89                 full_pos += pos;
90                 break;
91         case SEEK_END:
92                 full_pos = length() - pos;
93                 break;
94         }
95
96         /* Seek to full_pos */
97         size_t i = 0;
98         int64_t sub_pos = full_pos;
99         while (i < _paths.size ()) {
100                 boost::uintmax_t len = boost::filesystem::file_size (_paths[i]);
101                 if (sub_pos < int64_t (len)) {
102                         break;
103                 }
104                 sub_pos -= len;
105                 ++i;
106         }
107
108         if (i == _paths.size ()) {
109                 return -1;
110         }
111
112         ensure_open_path (i);
113         fseek (_current_file, sub_pos, SEEK_SET);
114         return full_pos;
115 }
116
117 /** Try to read some data from the current position into a buffer.
118  *  @param buffer Buffer to write data into.
119  *  @param amount Number of bytes to read.
120  *  @return Number of bytes read, or -1 in the case of error.
121  */
122 int
123 FileGroup::read (uint8_t* buffer, int amount) const
124 {
125         int read = 0;
126         while (1) {
127                 int const this_time = fread (buffer + read, 1, amount - read, _current_file);
128                 read += this_time;
129                 if (read == amount) {
130                         /* Done */
131                         break;
132                 }
133
134                 /* See if there is another file to use */
135                 if ((_current_path + 1) >= _paths.size()) {
136                         break;
137                 }
138                 ensure_open_path (_current_path + 1);
139         }
140
141         return read;
142 }
143
144 int64_t
145 FileGroup::length () const
146 {
147         int64_t len = 0;
148         for (size_t i = 0; i < _paths.size(); ++i) {
149                 len += boost::filesystem::file_size (_paths[i]);
150         }
151
152         return len;
153 }