Chris@148
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
2
|
Chris@148
|
3 /*
|
Chris@148
|
4 Sonic Visualiser
|
Chris@148
|
5 An audio file viewer and annotation editor.
|
Chris@148
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@537
|
7 This file copyright 2006-2009 Chris Cannam and QMUL.
|
Chris@148
|
8
|
Chris@148
|
9 This program is free software; you can redistribute it and/or
|
Chris@148
|
10 modify it under the terms of the GNU General Public License as
|
Chris@148
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
12 License, or (at your option) any later version. See the file
|
Chris@148
|
13 COPYING included with this distribution for more information.
|
Chris@148
|
14 */
|
Chris@148
|
15
|
Chris@148
|
16 #include "MatrixFile.h"
|
Chris@148
|
17 #include "base/TempDirectory.h"
|
Chris@150
|
18 #include "system/System.h"
|
Chris@148
|
19 #include "base/Profiler.h"
|
Chris@148
|
20 #include "base/Exceptions.h"
|
Chris@408
|
21 #include "base/Thread.h"
|
Chris@148
|
22
|
Chris@148
|
23 #include <sys/types.h>
|
Chris@148
|
24 #include <sys/stat.h>
|
Chris@148
|
25 #include <fcntl.h>
|
Chris@148
|
26 #include <unistd.h>
|
Chris@148
|
27
|
Chris@148
|
28 #include <iostream>
|
Chris@148
|
29
|
Chris@148
|
30 #include <cstdio>
|
Chris@148
|
31 #include <cassert>
|
Chris@148
|
32
|
Chris@405
|
33 #include <cstdlib>
|
Chris@405
|
34
|
Chris@148
|
35 #include <QFileInfo>
|
Chris@148
|
36 #include <QDir>
|
Chris@148
|
37
|
Chris@537
|
38 #define DEBUG_MATRIX_FILE 1
|
Chris@403
|
39 //#define DEBUG_MATRIX_FILE_READ_SET 1
|
Chris@148
|
40
|
Chris@148
|
41 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@153
|
42 #ifndef DEBUG_MATRIX_FILE
|
Chris@148
|
43 #define DEBUG_MATRIX_FILE 1
|
Chris@148
|
44 #endif
|
Chris@153
|
45 #endif
|
Chris@148
|
46
|
Chris@148
|
47 std::map<QString, int> MatrixFile::m_refcount;
|
Chris@537
|
48 QMutex MatrixFile::m_createMutex;
|
Chris@148
|
49
|
Chris@148
|
50 static size_t totalStorage = 0;
|
Chris@148
|
51 static size_t totalCount = 0;
|
Chris@537
|
52 static size_t openCount = 0;
|
Chris@148
|
53
|
Chris@148
|
54 MatrixFile::MatrixFile(QString fileBase, Mode mode,
|
Chris@537
|
55 size_t cellSize, size_t width, size_t height) :
|
Chris@148
|
56 m_fd(-1),
|
Chris@148
|
57 m_mode(mode),
|
Chris@148
|
58 m_flags(0),
|
Chris@148
|
59 m_fmode(0),
|
Chris@148
|
60 m_cellSize(cellSize),
|
Chris@537
|
61 m_width(width),
|
Chris@537
|
62 m_height(height),
|
Chris@550
|
63 m_headerSize(2 * sizeof(size_t)),
|
Chris@550
|
64 m_setColumns(0),
|
Chris@550
|
65 m_autoClose(false)
|
Chris@148
|
66 {
|
Chris@148
|
67 Profiler profiler("MatrixFile::MatrixFile", true);
|
Chris@148
|
68
|
Chris@537
|
69 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
70 std::cerr << "MatrixFile::MatrixFile(" << fileBase.toStdString() << ", " << int(mode) << ", " << cellSize << ", " << width << ", " << height << ")" << std::endl;
|
Chris@537
|
71 #endif
|
Chris@148
|
72
|
Chris@548
|
73 m_createMutex.lock();
|
Chris@148
|
74
|
Chris@148
|
75 QDir tempDir(TempDirectory::getInstance()->getPath());
|
Chris@148
|
76 QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase)));
|
Chris@148
|
77 bool newFile = !QFileInfo(fileName).exists();
|
Chris@148
|
78
|
Chris@148
|
79 if (newFile && m_mode == ReadOnly) {
|
Chris@148
|
80 std::cerr << "ERROR: MatrixFile::MatrixFile: Read-only mode "
|
Chris@148
|
81 << "specified, but cache file does not exist" << std::endl;
|
Chris@148
|
82 throw FileNotFound(fileName);
|
Chris@148
|
83 }
|
Chris@148
|
84
|
Chris@537
|
85 if (!newFile && m_mode == WriteOnly) {
|
Chris@537
|
86 std::cerr << "ERROR: MatrixFile::MatrixFile: Write-only mode "
|
Chris@537
|
87 << "specified, but file already exists" << std::endl;
|
Chris@537
|
88 throw FileOperationFailed(fileName, "create");
|
Chris@148
|
89 }
|
Chris@148
|
90
|
Chris@148
|
91 m_flags = 0;
|
Chris@148
|
92 m_fmode = S_IRUSR | S_IWUSR;
|
Chris@148
|
93
|
Chris@537
|
94 if (m_mode == WriteOnly) {
|
Chris@537
|
95 m_flags = O_WRONLY | O_CREAT;
|
Chris@148
|
96 } else {
|
Chris@148
|
97 m_flags = O_RDONLY;
|
Chris@148
|
98 }
|
Chris@148
|
99
|
Chris@233
|
100 #ifdef _WIN32
|
Chris@233
|
101 m_flags |= O_BINARY;
|
Chris@233
|
102 #endif
|
Chris@233
|
103
|
Chris@148
|
104 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
105 std::cerr << "MatrixFile(" << this << ")::MatrixFile: opening " << fileName.toStdString() << "..." << std::endl;
|
Chris@148
|
106 #endif
|
Chris@148
|
107
|
Chris@148
|
108 if ((m_fd = ::open(fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) {
|
Chris@148
|
109 ::perror("Open failed");
|
Chris@148
|
110 std::cerr << "ERROR: MatrixFile::MatrixFile: "
|
Chris@148
|
111 << "Failed to open cache file \""
|
Chris@148
|
112 << fileName.toStdString() << "\"";
|
Chris@537
|
113 if (m_mode == WriteOnly) std::cerr << " for writing";
|
Chris@148
|
114 std::cerr << std::endl;
|
Chris@148
|
115 throw FailedToOpenFile(fileName);
|
Chris@148
|
116 }
|
Chris@148
|
117
|
Chris@548
|
118 m_createMutex.unlock();
|
Chris@548
|
119
|
Chris@537
|
120 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
121 std::cerr << "MatrixFile(" << this << ")::MatrixFile: fd is " << m_fd << std::endl;
|
Chris@537
|
122 #endif
|
Chris@537
|
123
|
Chris@148
|
124 if (newFile) {
|
Chris@537
|
125 initialise(); // write header and "unwritten" column tags
|
Chris@148
|
126 } else {
|
Chris@148
|
127 size_t header[2];
|
Chris@148
|
128 if (::read(m_fd, header, 2 * sizeof(size_t)) < 0) {
|
Chris@236
|
129 ::perror("MatrixFile::MatrixFile: read failed");
|
Chris@148
|
130 std::cerr << "ERROR: MatrixFile::MatrixFile: "
|
Chris@148
|
131 << "Failed to read header (fd " << m_fd << ", file \""
|
Chris@148
|
132 << fileName.toStdString() << "\")" << std::endl;
|
Chris@148
|
133 throw FileReadFailed(fileName);
|
Chris@148
|
134 }
|
Chris@537
|
135 if (header[0] != m_width || header[1] != m_height) {
|
Chris@537
|
136 std::cerr << "ERROR: MatrixFile::MatrixFile: "
|
Chris@537
|
137 << "Dimensions in file header (" << header[0] << "x"
|
Chris@537
|
138 << header[1] << ") differ from expected dimensions "
|
Chris@537
|
139 << m_width << "x" << m_height << std::endl;
|
Chris@537
|
140 throw FailedToOpenFile(fileName);
|
Chris@537
|
141 }
|
Chris@148
|
142 }
|
Chris@148
|
143
|
Chris@148
|
144 m_fileName = fileName;
|
Chris@148
|
145 ++m_refcount[fileName];
|
Chris@148
|
146
|
Chris@537
|
147 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
148 std::cerr << "MatrixFile[" << m_fd << "]::MatrixFile: File " << fileName.toStdString() << ", ref " << m_refcount[fileName] << std::endl;
|
Chris@148
|
149
|
Chris@537
|
150 std::cerr << "MatrixFile[" << m_fd << "]::MatrixFile: Done, size is " << "(" << m_width << ", " << m_height << ")" << std::endl;
|
Chris@537
|
151 #endif
|
Chris@148
|
152
|
Chris@148
|
153 ++totalCount;
|
Chris@537
|
154 ++openCount;
|
Chris@148
|
155 }
|
Chris@148
|
156
|
Chris@148
|
157 MatrixFile::~MatrixFile()
|
Chris@148
|
158 {
|
Chris@148
|
159 if (m_fd >= 0) {
|
Chris@148
|
160 if (::close(m_fd) < 0) {
|
Chris@148
|
161 ::perror("MatrixFile::~MatrixFile: close failed");
|
Chris@148
|
162 }
|
Chris@546
|
163 openCount --;
|
Chris@148
|
164 }
|
Chris@148
|
165
|
Chris@537
|
166 QMutexLocker locker(&m_createMutex);
|
Chris@537
|
167
|
Chris@550
|
168 delete m_setColumns;
|
Chris@550
|
169
|
Chris@148
|
170 if (m_fileName != "") {
|
Chris@148
|
171
|
Chris@148
|
172 if (--m_refcount[m_fileName] == 0) {
|
Chris@148
|
173
|
Chris@148
|
174 if (::unlink(m_fileName.toLocal8Bit())) {
|
Chris@537
|
175 std::cerr << "WARNING: MatrixFile::~MatrixFile: reference count reached 0, but failed to unlink file \"" << m_fileName.toStdString() << "\"" << std::endl;
|
Chris@148
|
176 } else {
|
Chris@537
|
177 std::cerr << "deleted " << m_fileName.toStdString() << std::endl;
|
Chris@148
|
178 }
|
Chris@148
|
179 }
|
Chris@148
|
180 }
|
Chris@537
|
181
|
Chris@537
|
182 if (m_mode == WriteOnly) {
|
Chris@537
|
183 totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize) + m_width);
|
Chris@537
|
184 }
|
Chris@148
|
185 totalCount --;
|
Chris@148
|
186
|
Chris@537
|
187 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
188 std::cerr << "MatrixFile[" << m_fd << "]::~MatrixFile: " << std::endl;
|
Chris@537
|
189 std::cerr << "MatrixFile: Total storage now " << totalStorage/1024 << "K in " << totalCount << " instances (" << openCount << " open)" << std::endl;
|
Chris@537
|
190 #endif
|
Chris@148
|
191 }
|
Chris@148
|
192
|
Chris@148
|
193 void
|
Chris@537
|
194 MatrixFile::initialise()
|
Chris@148
|
195 {
|
Chris@537
|
196 Profiler profiler("MatrixFile::initialise", true);
|
Chris@148
|
197
|
Chris@537
|
198 assert(m_mode == WriteOnly);
|
Chris@550
|
199
|
Chris@550
|
200 m_setColumns = new ResizeableBitset(m_width);
|
Chris@148
|
201
|
Chris@537
|
202 off_t off = m_headerSize + (m_width * m_height * m_cellSize) + m_width;
|
Chris@148
|
203
|
Chris@148
|
204 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
205 std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): cell size " << m_cellSize << ", header size " << m_headerSize << ", resizing file" << std::endl;
|
Chris@148
|
206 #endif
|
Chris@148
|
207
|
Chris@537
|
208 if (::lseek(m_fd, off - 1, SEEK_SET) < 0) {
|
Chris@537
|
209 ::perror("ERROR: MatrixFile::initialise: seek to end failed");
|
Chris@537
|
210 throw FileOperationFailed(m_fileName, "lseek");
|
Chris@148
|
211 }
|
Chris@148
|
212
|
Chris@537
|
213 unsigned char byte = 0;
|
Chris@537
|
214 if (::write(m_fd, &byte, 1) != 1) {
|
Chris@537
|
215 ::perror("ERROR: MatrixFile::initialise: write at end failed");
|
Chris@537
|
216 throw FileOperationFailed(m_fileName, "write");
|
Chris@537
|
217 }
|
Chris@148
|
218
|
Chris@537
|
219 if (::lseek(m_fd, 0, SEEK_SET) < 0) {
|
Chris@547
|
220 ::perror("ERROR: MatrixFile::initialise: Seek to write header failed");
|
Chris@148
|
221 throw FileOperationFailed(m_fileName, "lseek");
|
Chris@148
|
222 }
|
Chris@148
|
223
|
Chris@148
|
224 size_t header[2];
|
Chris@537
|
225 header[0] = m_width;
|
Chris@537
|
226 header[1] = m_height;
|
Chris@148
|
227 if (::write(m_fd, header, 2 * sizeof(size_t)) != 2 * sizeof(size_t)) {
|
Chris@547
|
228 ::perror("ERROR: MatrixFile::initialise: Failed to write header");
|
Chris@148
|
229 throw FileOperationFailed(m_fileName, "write");
|
Chris@148
|
230 }
|
Chris@148
|
231
|
Chris@537
|
232 if (m_mode == WriteOnly) {
|
Chris@537
|
233 totalStorage += (m_headerSize + (m_width * m_height * m_cellSize) + m_width);
|
Chris@148
|
234 }
|
Chris@148
|
235
|
Chris@537
|
236 #ifdef DEBUG_MATRIX_FILE
|
Chris@547
|
237 std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): storage "
|
Chris@537
|
238 << (m_headerSize + m_width * m_height * m_cellSize + m_width) << std::endl;
|
Chris@148
|
239
|
Chris@537
|
240 std::cerr << "MatrixFile: Total storage " << totalStorage/1024 << "K" << std::endl;
|
Chris@148
|
241 #endif
|
Chris@148
|
242
|
Chris@148
|
243 seekTo(0, 0);
|
Chris@148
|
244 }
|
Chris@148
|
245
|
Chris@148
|
246 void
|
Chris@537
|
247 MatrixFile::close()
|
Chris@148
|
248 {
|
Chris@537
|
249 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
250 std::cerr << "MatrixFile::close()" << std::endl;
|
Chris@537
|
251 #endif
|
Chris@537
|
252 if (m_fd >= 0) {
|
Chris@537
|
253 if (::close(m_fd) < 0) {
|
Chris@537
|
254 ::perror("MatrixFile::close: close failed");
|
Chris@537
|
255 }
|
Chris@537
|
256 m_fd = -1;
|
Chris@537
|
257 -- openCount;
|
Chris@550
|
258 #ifdef DEBUG_MATRIX_FILE
|
Chris@550
|
259 std::cerr << "MatrixFile: Now " << openCount << " open instances" << std::endl;
|
Chris@550
|
260 #endif
|
Chris@148
|
261 }
|
Chris@148
|
262 }
|
Chris@148
|
263
|
Chris@148
|
264 void
|
Chris@148
|
265 MatrixFile::getColumnAt(size_t x, void *data)
|
Chris@148
|
266 {
|
Chris@537
|
267 assert(m_mode == ReadOnly);
|
Chris@537
|
268
|
Chris@537
|
269 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@537
|
270 std::cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << ")" << std::endl;
|
Chris@537
|
271 #endif
|
Chris@537
|
272
|
Chris@408
|
273 Profiler profiler("MatrixFile::getColumnAt");
|
Chris@148
|
274
|
Chris@537
|
275 unsigned char set = 0;
|
Chris@537
|
276 if (!seekTo(x, 0)) {
|
Chris@537
|
277 std::cerr << "ERROR: MatrixFile::getColumnAt(" << x << "): Seek failed" << std::endl;
|
Chris@537
|
278 throw FileOperationFailed(m_fileName, "seek");
|
Chris@148
|
279 }
|
Chris@148
|
280
|
Chris@537
|
281 ssize_t r = -1;
|
Chris@537
|
282 r = ::read(m_fd, &set, 1);
|
Chris@537
|
283 if (r < 0) {
|
Chris@537
|
284 ::perror("MatrixFile::getColumnAt: read failed");
|
Chris@537
|
285 throw FileReadFailed(m_fileName);
|
Chris@537
|
286 }
|
Chris@537
|
287 if (!set) {
|
Chris@537
|
288 std::cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << "): Column has not been set" << std::endl;
|
Chris@537
|
289 return;
|
Chris@537
|
290 }
|
Chris@537
|
291
|
Chris@537
|
292 r = ::read(m_fd, data, m_height * m_cellSize);
|
Chris@537
|
293 if (r < 0) {
|
Chris@537
|
294 ::perror("MatrixFile::getColumnAt: read failed");
|
Chris@537
|
295 throw FileReadFailed(m_fileName);
|
Chris@537
|
296 }
|
Chris@537
|
297 }
|
Chris@537
|
298
|
Chris@537
|
299 bool
|
Chris@537
|
300 MatrixFile::haveSetColumnAt(size_t x) const
|
Chris@537
|
301 {
|
Chris@550
|
302 if (m_mode == WriteOnly) {
|
Chris@550
|
303 return m_setColumns->get(x);
|
Chris@550
|
304 }
|
Chris@537
|
305
|
Chris@537
|
306 Profiler profiler("MatrixFile::haveSetColumnAt");
|
Chris@537
|
307
|
Chris@537
|
308 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@537
|
309 std::cerr << "MatrixFile[" << m_fd << "]::haveSetColumnAt(" << x << ")" << std::endl;
|
Chris@537
|
310 // std::cerr << ".";
|
Chris@148
|
311 #endif
|
Chris@148
|
312
|
Chris@537
|
313 unsigned char set = 0;
|
Chris@537
|
314 if (!seekTo(x, 0)) {
|
Chris@537
|
315 std::cerr << "ERROR: MatrixFile::haveSetColumnAt(" << x << "): Seek failed" << std::endl;
|
Chris@537
|
316 throw FileOperationFailed(m_fileName, "seek");
|
Chris@537
|
317 }
|
Chris@148
|
318
|
Chris@537
|
319 ssize_t r = -1;
|
Chris@537
|
320 r = ::read(m_fd, &set, 1);
|
Chris@148
|
321 if (r < 0) {
|
Chris@537
|
322 ::perror("MatrixFile::haveSetColumnAt: read failed");
|
Chris@148
|
323 throw FileReadFailed(m_fileName);
|
Chris@148
|
324 }
|
Chris@148
|
325
|
Chris@537
|
326 return set;
|
Chris@148
|
327 }
|
Chris@148
|
328
|
Chris@148
|
329 void
|
Chris@148
|
330 MatrixFile::setColumnAt(size_t x, const void *data)
|
Chris@148
|
331 {
|
Chris@537
|
332 assert(m_mode == WriteOnly);
|
Chris@550
|
333 if (m_fd < 0) return; // closed
|
Chris@148
|
334
|
Chris@148
|
335 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@537
|
336 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << ")" << std::endl;
|
Chris@537
|
337 // std::cerr << ".";
|
Chris@148
|
338 #endif
|
Chris@148
|
339
|
Chris@148
|
340 ssize_t w = 0;
|
Chris@148
|
341
|
Chris@537
|
342 if (!seekTo(x, 0)) {
|
Chris@537
|
343 std::cerr << "ERROR: MatrixFile::setColumnAt(" << x << "): Seek failed" << std::endl;
|
Chris@537
|
344 throw FileOperationFailed(m_fileName, "seek");
|
Chris@148
|
345 }
|
Chris@148
|
346
|
Chris@537
|
347 unsigned char set = 0;
|
Chris@537
|
348 w = ::write(m_fd, &set, 1);
|
Chris@537
|
349 if (w != 1) {
|
Chris@537
|
350 ::perror("WARNING: MatrixFile::setColumnAt: write failed (1)");
|
Chris@148
|
351 throw FileOperationFailed(m_fileName, "write");
|
Chris@537
|
352 }
|
Chris@537
|
353
|
Chris@537
|
354 w = ::write(m_fd, data, m_height * m_cellSize);
|
Chris@537
|
355 if (w != ssize_t(m_height * m_cellSize)) {
|
Chris@537
|
356 ::perror("WARNING: MatrixFile::setColumnAt: write failed (2)");
|
Chris@537
|
357 throw FileOperationFailed(m_fileName, "write");
|
Chris@537
|
358 }
|
Chris@537
|
359 /*
|
Chris@537
|
360 if (x == 0) {
|
Chris@537
|
361 std::cerr << "Wrote " << m_height * m_cellSize << " bytes, as follows:" << std::endl;
|
Chris@537
|
362 for (int i = 0; i < m_height * m_cellSize; ++i) {
|
Chris@537
|
363 std::cerr << (int)(((char *)data)[i]) << " ";
|
Chris@537
|
364 }
|
Chris@537
|
365 std::cerr << std::endl;
|
Chris@537
|
366 }
|
Chris@537
|
367 */
|
Chris@537
|
368 if (!seekTo(x, 0)) {
|
Chris@537
|
369 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Seek failed" << std::endl;
|
Chris@148
|
370 throw FileOperationFailed(m_fileName, "seek");
|
Chris@537
|
371 }
|
Chris@537
|
372
|
Chris@537
|
373 set = 1;
|
Chris@537
|
374 w = ::write(m_fd, &set, 1);
|
Chris@537
|
375 if (w != 1) {
|
Chris@537
|
376 ::perror("WARNING: MatrixFile::setColumnAt: write failed (3)");
|
Chris@537
|
377 throw FileOperationFailed(m_fileName, "write");
|
Chris@148
|
378 }
|
Chris@550
|
379
|
Chris@550
|
380 m_setColumns->set(x);
|
Chris@550
|
381 if (m_autoClose) {
|
Chris@550
|
382 if (m_setColumns->isAllOn()) {
|
Chris@550
|
383 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): All columns set: auto-closing" << std::endl;
|
Chris@550
|
384 close();
|
Chris@550
|
385 /*
|
Chris@550
|
386 } else {
|
Chris@550
|
387 int set = 0;
|
Chris@550
|
388 for (int i = 0; i < m_width; ++i) {
|
Chris@550
|
389 if (m_setColumns->get(i)) ++set;
|
Chris@550
|
390 }
|
Chris@550
|
391 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Auto-close on, but not all columns set yet (" << set << " of " << m_width << ")" << std::endl;
|
Chris@550
|
392 */
|
Chris@550
|
393 }
|
Chris@550
|
394 }
|
Chris@148
|
395 }
|
Chris@148
|
396
|
Chris@537
|
397 bool
|
Chris@537
|
398 MatrixFile::seekTo(size_t x, size_t y) const
|
Chris@148
|
399 {
|
Chris@537
|
400 if (m_fd < 0) {
|
Chris@537
|
401 std::cerr << "ERROR: MatrixFile::seekTo: File not open" << std::endl;
|
Chris@537
|
402 return false;
|
Chris@148
|
403 }
|
Chris@148
|
404
|
Chris@537
|
405 off_t off = m_headerSize + x * m_height * m_cellSize + x + y * m_cellSize;
|
Chris@148
|
406
|
Chris@148
|
407 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@537
|
408 std::cerr << "MatrixFile[" << m_fd << "]::seekTo(" << x << "," << y << "): off = " << off << std::endl;
|
Chris@148
|
409 #endif
|
Chris@148
|
410
|
Chris@148
|
411 if (::lseek(m_fd, off, SEEK_SET) == (off_t)-1) {
|
Chris@148
|
412 ::perror("Seek failed");
|
Chris@148
|
413 std::cerr << "ERROR: MatrixFile::seekTo(" << x << ", " << y
|
Chris@537
|
414 << ") = " << off << " failed" << std::endl;
|
Chris@148
|
415 return false;
|
Chris@148
|
416 }
|
Chris@148
|
417
|
Chris@148
|
418 return true;
|
Chris@148
|
419 }
|
Chris@148
|
420
|