comparison data/fileio/MatrixFile.cpp @ 843:e802e550a1f2

Drop std:: from cout, cerr, endl -- pull these in through Debug.h
author Chris Cannam
date Tue, 26 Nov 2013 13:35:08 +0000
parents 1424aa29ae95
children 59e7fe1b1003
comparison
equal deleted inserted replaced
842:23d3a6eca5c3 843:e802e550a1f2
76 QDir tempDir(TempDirectory::getInstance()->getPath()); 76 QDir tempDir(TempDirectory::getInstance()->getPath());
77 QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase))); 77 QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase)));
78 bool newFile = !QFileInfo(fileName).exists(); 78 bool newFile = !QFileInfo(fileName).exists();
79 79
80 if (newFile && m_mode == ReadOnly) { 80 if (newFile && m_mode == ReadOnly) {
81 std::cerr << "ERROR: MatrixFile::MatrixFile: Read-only mode " 81 cerr << "ERROR: MatrixFile::MatrixFile: Read-only mode "
82 << "specified, but cache file does not exist" << std::endl; 82 << "specified, but cache file does not exist" << endl;
83 throw FileNotFound(fileName); 83 throw FileNotFound(fileName);
84 } 84 }
85 85
86 if (!newFile && m_mode == WriteOnly) { 86 if (!newFile && m_mode == WriteOnly) {
87 std::cerr << "ERROR: MatrixFile::MatrixFile: Write-only mode " 87 cerr << "ERROR: MatrixFile::MatrixFile: Write-only mode "
88 << "specified, but file already exists" << std::endl; 88 << "specified, but file already exists" << endl;
89 throw FileOperationFailed(fileName, "create"); 89 throw FileOperationFailed(fileName, "create");
90 } 90 }
91 91
92 m_flags = 0; 92 m_flags = 0;
93 m_fmode = S_IRUSR | S_IWUSR; 93 m_fmode = S_IRUSR | S_IWUSR;
101 #ifdef _WIN32 101 #ifdef _WIN32
102 m_flags |= O_BINARY; 102 m_flags |= O_BINARY;
103 #endif 103 #endif
104 104
105 #ifdef DEBUG_MATRIX_FILE 105 #ifdef DEBUG_MATRIX_FILE
106 std::cerr << "MatrixFile(" << this << ")::MatrixFile: opening " << fileName << "..." << std::endl; 106 cerr << "MatrixFile(" << this << ")::MatrixFile: opening " << fileName << "..." << endl;
107 #endif 107 #endif
108 108
109 if ((m_fd = ::open(fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) { 109 if ((m_fd = ::open(fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) {
110 ::perror("Open failed"); 110 ::perror("Open failed");
111 std::cerr << "ERROR: MatrixFile::MatrixFile: " 111 cerr << "ERROR: MatrixFile::MatrixFile: "
112 << "Failed to open cache file \"" 112 << "Failed to open cache file \""
113 << fileName << "\""; 113 << fileName << "\"";
114 if (m_mode == WriteOnly) std::cerr << " for writing"; 114 if (m_mode == WriteOnly) cerr << " for writing";
115 std::cerr << std::endl; 115 cerr << endl;
116 throw FailedToOpenFile(fileName); 116 throw FailedToOpenFile(fileName);
117 } 117 }
118 118
119 m_createMutex.unlock(); 119 m_createMutex.unlock();
120 120
121 #ifdef DEBUG_MATRIX_FILE 121 #ifdef DEBUG_MATRIX_FILE
122 std::cerr << "MatrixFile(" << this << ")::MatrixFile: fd is " << m_fd << std::endl; 122 cerr << "MatrixFile(" << this << ")::MatrixFile: fd is " << m_fd << endl;
123 #endif 123 #endif
124 124
125 if (newFile) { 125 if (newFile) {
126 initialise(); // write header and "unwritten" column tags 126 initialise(); // write header and "unwritten" column tags
127 } else { 127 } else {
128 size_t header[2]; 128 size_t header[2];
129 if (::read(m_fd, header, 2 * sizeof(size_t)) < 0) { 129 if (::read(m_fd, header, 2 * sizeof(size_t)) < 0) {
130 ::perror("MatrixFile::MatrixFile: read failed"); 130 ::perror("MatrixFile::MatrixFile: read failed");
131 std::cerr << "ERROR: MatrixFile::MatrixFile: " 131 cerr << "ERROR: MatrixFile::MatrixFile: "
132 << "Failed to read header (fd " << m_fd << ", file \"" 132 << "Failed to read header (fd " << m_fd << ", file \""
133 << fileName << "\")" << std::endl; 133 << fileName << "\")" << endl;
134 throw FileReadFailed(fileName); 134 throw FileReadFailed(fileName);
135 } 135 }
136 if (header[0] != m_width || header[1] != m_height) { 136 if (header[0] != m_width || header[1] != m_height) {
137 std::cerr << "ERROR: MatrixFile::MatrixFile: " 137 cerr << "ERROR: MatrixFile::MatrixFile: "
138 << "Dimensions in file header (" << header[0] << "x" 138 << "Dimensions in file header (" << header[0] << "x"
139 << header[1] << ") differ from expected dimensions " 139 << header[1] << ") differ from expected dimensions "
140 << m_width << "x" << m_height << std::endl; 140 << m_width << "x" << m_height << endl;
141 throw FailedToOpenFile(fileName); 141 throw FailedToOpenFile(fileName);
142 } 142 }
143 } 143 }
144 144
145 m_fileName = fileName; 145 m_fileName = fileName;
146 ++m_refcount[fileName]; 146 ++m_refcount[fileName];
147 147
148 #ifdef DEBUG_MATRIX_FILE 148 #ifdef DEBUG_MATRIX_FILE
149 std::cerr << "MatrixFile[" << m_fd << "]::MatrixFile: File " << fileName << ", ref " << m_refcount[fileName] << std::endl; 149 cerr << "MatrixFile[" << m_fd << "]::MatrixFile: File " << fileName << ", ref " << m_refcount[fileName] << endl;
150 150
151 std::cerr << "MatrixFile[" << m_fd << "]::MatrixFile: Done, size is " << "(" << m_width << ", " << m_height << ")" << std::endl; 151 cerr << "MatrixFile[" << m_fd << "]::MatrixFile: Done, size is " << "(" << m_width << ", " << m_height << ")" << endl;
152 #endif 152 #endif
153 153
154 ++totalCount; 154 ++totalCount;
155 ++openCount; 155 ++openCount;
156 } 156 }
171 if (m_fileName != "") { 171 if (m_fileName != "") {
172 172
173 if (--m_refcount[m_fileName] == 0) { 173 if (--m_refcount[m_fileName] == 0) {
174 174
175 if (::unlink(m_fileName.toLocal8Bit())) { 175 if (::unlink(m_fileName.toLocal8Bit())) {
176 std::cerr << "WARNING: MatrixFile::~MatrixFile: reference count reached 0, but failed to unlink file \"" << m_fileName << "\"" << std::endl; 176 cerr << "WARNING: MatrixFile::~MatrixFile: reference count reached 0, but failed to unlink file \"" << m_fileName << "\"" << endl;
177 } else { 177 } else {
178 std::cerr << "deleted " << m_fileName << std::endl; 178 cerr << "deleted " << m_fileName << endl;
179 } 179 }
180 } 180 }
181 } 181 }
182 182
183 if (m_mode == WriteOnly) { 183 if (m_mode == WriteOnly) {
184 totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize) + m_width); 184 totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize) + m_width);
185 } 185 }
186 totalCount --; 186 totalCount --;
187 187
188 #ifdef DEBUG_MATRIX_FILE 188 #ifdef DEBUG_MATRIX_FILE
189 std::cerr << "MatrixFile[" << m_fd << "]::~MatrixFile: " << std::endl; 189 cerr << "MatrixFile[" << m_fd << "]::~MatrixFile: " << endl;
190 std::cerr << "MatrixFile: Total storage now " << totalStorage/1024 << "K in " << totalCount << " instances (" << openCount << " open)" << std::endl; 190 cerr << "MatrixFile: Total storage now " << totalStorage/1024 << "K in " << totalCount << " instances (" << openCount << " open)" << endl;
191 #endif 191 #endif
192 } 192 }
193 193
194 void 194 void
195 MatrixFile::initialise() 195 MatrixFile::initialise()
201 m_setColumns = new ResizeableBitset(m_width); 201 m_setColumns = new ResizeableBitset(m_width);
202 202
203 off_t off = m_headerSize + (m_width * m_height * m_cellSize) + m_width; 203 off_t off = m_headerSize + (m_width * m_height * m_cellSize) + m_width;
204 204
205 #ifdef DEBUG_MATRIX_FILE 205 #ifdef DEBUG_MATRIX_FILE
206 std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): cell size " << m_cellSize << ", header size " << m_headerSize << ", resizing file" << std::endl; 206 cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): cell size " << m_cellSize << ", header size " << m_headerSize << ", resizing file" << endl;
207 #endif 207 #endif
208 208
209 if (::lseek(m_fd, off - 1, SEEK_SET) < 0) { 209 if (::lseek(m_fd, off - 1, SEEK_SET) < 0) {
210 ::perror("ERROR: MatrixFile::initialise: seek to end failed"); 210 ::perror("ERROR: MatrixFile::initialise: seek to end failed");
211 throw FileOperationFailed(m_fileName, "lseek"); 211 throw FileOperationFailed(m_fileName, "lseek");
233 if (m_mode == WriteOnly) { 233 if (m_mode == WriteOnly) {
234 totalStorage += (m_headerSize + (m_width * m_height * m_cellSize) + m_width); 234 totalStorage += (m_headerSize + (m_width * m_height * m_cellSize) + m_width);
235 } 235 }
236 236
237 #ifdef DEBUG_MATRIX_FILE 237 #ifdef DEBUG_MATRIX_FILE
238 std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): storage " 238 cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): storage "
239 << (m_headerSize + m_width * m_height * m_cellSize + m_width) << std::endl; 239 << (m_headerSize + m_width * m_height * m_cellSize + m_width) << endl;
240 240
241 std::cerr << "MatrixFile: Total storage " << totalStorage/1024 << "K" << std::endl; 241 cerr << "MatrixFile: Total storage " << totalStorage/1024 << "K" << endl;
242 #endif 242 #endif
243 243
244 seekTo(0); 244 seekTo(0);
245 } 245 }
246 246
255 ::perror("MatrixFile::close: close failed"); 255 ::perror("MatrixFile::close: close failed");
256 } 256 }
257 m_fd = -1; 257 m_fd = -1;
258 -- openCount; 258 -- openCount;
259 #ifdef DEBUG_MATRIX_FILE 259 #ifdef DEBUG_MATRIX_FILE
260 std::cerr << "MatrixFile: Now " << openCount << " open instances" << std::endl; 260 cerr << "MatrixFile: Now " << openCount << " open instances" << endl;
261 #endif 261 #endif
262 } 262 }
263 } 263 }
264 264
265 void 265 void
266 MatrixFile::getColumnAt(size_t x, void *data) 266 MatrixFile::getColumnAt(size_t x, void *data)
267 { 267 {
268 assert(m_mode == ReadOnly); 268 assert(m_mode == ReadOnly);
269 269
270 #ifdef DEBUG_MATRIX_FILE_READ_SET 270 #ifdef DEBUG_MATRIX_FILE_READ_SET
271 std::cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << ")" << std::endl; 271 cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << ")" << endl;
272 #endif 272 #endif
273 273
274 Profiler profiler("MatrixFile::getColumnAt"); 274 Profiler profiler("MatrixFile::getColumnAt");
275 275
276 ssize_t r = -1; 276 ssize_t r = -1;
278 if (m_readyToReadColumn < 0 || 278 if (m_readyToReadColumn < 0 ||
279 size_t(m_readyToReadColumn) != x) { 279 size_t(m_readyToReadColumn) != x) {
280 280
281 unsigned char set = 0; 281 unsigned char set = 0;
282 if (!seekTo(x)) { 282 if (!seekTo(x)) {
283 std::cerr << "ERROR: MatrixFile::getColumnAt(" << x << "): Seek failed" << std::endl; 283 cerr << "ERROR: MatrixFile::getColumnAt(" << x << "): Seek failed" << endl;
284 throw FileOperationFailed(m_fileName, "seek"); 284 throw FileOperationFailed(m_fileName, "seek");
285 } 285 }
286 286
287 r = ::read(m_fd, &set, 1); 287 r = ::read(m_fd, &set, 1);
288 if (r < 0) { 288 if (r < 0) {
289 ::perror("MatrixFile::getColumnAt: read failed"); 289 ::perror("MatrixFile::getColumnAt: read failed");
290 throw FileReadFailed(m_fileName); 290 throw FileReadFailed(m_fileName);
291 } 291 }
292 if (!set) { 292 if (!set) {
293 std::cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << "): Column has not been set" << std::endl; 293 cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << "): Column has not been set" << endl;
294 return; 294 return;
295 } 295 }
296 } 296 }
297 297
298 r = ::read(m_fd, data, m_height * m_cellSize); 298 r = ::read(m_fd, data, m_height * m_cellSize);
313 size_t(m_readyToReadColumn) == x) return true; 313 size_t(m_readyToReadColumn) == x) return true;
314 314
315 Profiler profiler("MatrixFile::haveSetColumnAt"); 315 Profiler profiler("MatrixFile::haveSetColumnAt");
316 316
317 #ifdef DEBUG_MATRIX_FILE_READ_SET 317 #ifdef DEBUG_MATRIX_FILE_READ_SET
318 std::cerr << "MatrixFile[" << m_fd << "]::haveSetColumnAt(" << x << ")" << std::endl; 318 cerr << "MatrixFile[" << m_fd << "]::haveSetColumnAt(" << x << ")" << endl;
319 // std::cerr << "."; 319 // cerr << ".";
320 #endif 320 #endif
321 321
322 unsigned char set = 0; 322 unsigned char set = 0;
323 if (!seekTo(x)) { 323 if (!seekTo(x)) {
324 std::cerr << "ERROR: MatrixFile::haveSetColumnAt(" << x << "): Seek failed" << std::endl; 324 cerr << "ERROR: MatrixFile::haveSetColumnAt(" << x << "): Seek failed" << endl;
325 throw FileOperationFailed(m_fileName, "seek"); 325 throw FileOperationFailed(m_fileName, "seek");
326 } 326 }
327 327
328 ssize_t r = -1; 328 ssize_t r = -1;
329 r = ::read(m_fd, &set, 1); 329 r = ::read(m_fd, &set, 1);
342 { 342 {
343 assert(m_mode == WriteOnly); 343 assert(m_mode == WriteOnly);
344 if (m_fd < 0) return; // closed 344 if (m_fd < 0) return; // closed
345 345
346 #ifdef DEBUG_MATRIX_FILE_READ_SET 346 #ifdef DEBUG_MATRIX_FILE_READ_SET
347 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << ")" << std::endl; 347 cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << ")" << endl;
348 // std::cerr << "."; 348 // cerr << ".";
349 #endif 349 #endif
350 350
351 ssize_t w = 0; 351 ssize_t w = 0;
352 352
353 if (!seekTo(x)) { 353 if (!seekTo(x)) {
354 std::cerr << "ERROR: MatrixFile::setColumnAt(" << x << "): Seek failed" << std::endl; 354 cerr << "ERROR: MatrixFile::setColumnAt(" << x << "): Seek failed" << endl;
355 throw FileOperationFailed(m_fileName, "seek"); 355 throw FileOperationFailed(m_fileName, "seek");
356 } 356 }
357 357
358 unsigned char set = 0; 358 unsigned char set = 0;
359 w = ::write(m_fd, &set, 1); 359 w = ::write(m_fd, &set, 1);
367 ::perror("WARNING: MatrixFile::setColumnAt: write failed (2)"); 367 ::perror("WARNING: MatrixFile::setColumnAt: write failed (2)");
368 throw FileOperationFailed(m_fileName, "write"); 368 throw FileOperationFailed(m_fileName, "write");
369 } 369 }
370 /* 370 /*
371 if (x == 0) { 371 if (x == 0) {
372 std::cerr << "Wrote " << m_height * m_cellSize << " bytes, as follows:" << std::endl; 372 cerr << "Wrote " << m_height * m_cellSize << " bytes, as follows:" << endl;
373 for (int i = 0; i < m_height * m_cellSize; ++i) { 373 for (int i = 0; i < m_height * m_cellSize; ++i) {
374 std::cerr << (int)(((char *)data)[i]) << " "; 374 cerr << (int)(((char *)data)[i]) << " ";
375 } 375 }
376 std::cerr << std::endl; 376 cerr << endl;
377 } 377 }
378 */ 378 */
379 if (!seekTo(x)) { 379 if (!seekTo(x)) {
380 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Seek failed" << std::endl; 380 cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Seek failed" << endl;
381 throw FileOperationFailed(m_fileName, "seek"); 381 throw FileOperationFailed(m_fileName, "seek");
382 } 382 }
383 383
384 set = 1; 384 set = 1;
385 w = ::write(m_fd, &set, 1); 385 w = ::write(m_fd, &set, 1);
390 390
391 m_setColumns->set(x); 391 m_setColumns->set(x);
392 if (m_autoClose) { 392 if (m_autoClose) {
393 if (m_setColumns->isAllOn()) { 393 if (m_setColumns->isAllOn()) {
394 #ifdef DEBUG_MATRIX_FILE 394 #ifdef DEBUG_MATRIX_FILE
395 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): All columns set: auto-closing" << std::endl; 395 cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): All columns set: auto-closing" << endl;
396 #endif 396 #endif
397 close(); 397 close();
398 /* 398 /*
399 } else { 399 } else {
400 int set = 0; 400 int set = 0;
401 for (int i = 0; i < m_width; ++i) { 401 for (int i = 0; i < m_width; ++i) {
402 if (m_setColumns->get(i)) ++set; 402 if (m_setColumns->get(i)) ++set;
403 } 403 }
404 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Auto-close on, but not all columns set yet (" << set << " of " << m_width << ")" << std::endl; 404 cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Auto-close on, but not all columns set yet (" << set << " of " << m_width << ")" << endl;
405 */ 405 */
406 } 406 }
407 } 407 }
408 } 408 }
409 409
410 bool 410 bool
411 MatrixFile::seekTo(size_t x) const 411 MatrixFile::seekTo(size_t x) const
412 { 412 {
413 if (m_fd < 0) { 413 if (m_fd < 0) {
414 std::cerr << "ERROR: MatrixFile::seekTo: File not open" << std::endl; 414 cerr << "ERROR: MatrixFile::seekTo: File not open" << endl;
415 return false; 415 return false;
416 } 416 }
417 417
418 m_readyToReadColumn = -1; // not ready, unless this is subsequently re-set 418 m_readyToReadColumn = -1; // not ready, unless this is subsequently re-set
419 419
420 off_t off = m_headerSize + x * m_height * m_cellSize + x; 420 off_t off = m_headerSize + x * m_height * m_cellSize + x;
421 421
422 #ifdef DEBUG_MATRIX_FILE_READ_SET 422 #ifdef DEBUG_MATRIX_FILE_READ_SET
423 if (m_mode == ReadOnly) { 423 if (m_mode == ReadOnly) {
424 std::cerr << "MatrixFile[" << m_fd << "]::seekTo(" << x << "): off = " << off << std::endl; 424 cerr << "MatrixFile[" << m_fd << "]::seekTo(" << x << "): off = " << off << endl;
425 } 425 }
426 #endif 426 #endif
427 427
428 #ifdef DEBUG_MATRIX_FILE_READ_SET 428 #ifdef DEBUG_MATRIX_FILE_READ_SET
429 std::cerr << "MatrixFile[" << m_fd << "]::seekTo(" << x << "): off = " << off << std::endl; 429 cerr << "MatrixFile[" << m_fd << "]::seekTo(" << x << "): off = " << off << endl;
430 #endif 430 #endif
431 431
432 if (::lseek(m_fd, off, SEEK_SET) == (off_t)-1) { 432 if (::lseek(m_fd, off, SEEK_SET) == (off_t)-1) {
433 ::perror("Seek failed"); 433 ::perror("Seek failed");
434 std::cerr << "ERROR: MatrixFile::seekTo(" << x 434 cerr << "ERROR: MatrixFile::seekTo(" << x
435 << ") = " << off << " failed" << std::endl; 435 << ") = " << off << " failed" << endl;
436 return false; 436 return false;
437 } 437 }
438 438
439 return true; 439 return true;
440 } 440 }