comparison data/fileio/MP3FileReader.cpp @ 229:b176e99f9c38

...
author Chris Cannam
date Mon, 12 Feb 2007 13:20:43 +0000
parents f02989f7e160
children 2854dc429310
comparison
equal deleted inserted replaced
228:f02989f7e160 229:b176e99f9c38
48 return; 48 return;
49 } 49 }
50 50
51 m_fileSize = stat.st_size; 51 m_fileSize = stat.st_size;
52 52
53 int fd; 53 int fd = -1;
54 if ((fd = ::open(path.toLocal8Bit().data(), O_RDONLY, 0)) < 0) { 54 if ((fd = ::open(path.toLocal8Bit().data(), O_RDONLY, 0)) < 0) {
55 m_error = QString("Failed to open file %1 for reading.").arg(path); 55 m_error = QString("Failed to open file %1 for reading.").arg(path);
56 return; 56 return;
57 } 57 }
58 58
59 unsigned char *filebuffer = 0; 59 unsigned char *filebuffer = 0;
60 60
61 try { 61 try {
62 filebuffer = new unsigned char[stat.st_size]; 62 filebuffer = new unsigned char[m_fileSize];
63 } catch (...) { 63 } catch (...) {
64 m_error = QString("Out of memory"); 64 m_error = QString("Out of memory");
65 ::close(fd); 65 ::close(fd);
66 return; 66 return;
67 } 67 }
68 68
69 size_t sz = 0; 69 ssize_t sz = 0;
70 if ((sz = ::read(fd, filebuffer, stat.st_size)) < stat.st_size) { 70 size_t offset = 0;
71 m_error = QString("Failed to read file %1 (expected %2 bytes, got %3 bytes).").arg(path).arg(stat.st_size).arg(sz); 71 while (offset < m_fileSize) {
72 delete[] filebuffer; 72 sz = ::read(fd, filebuffer + offset, m_fileSize - offset);
73 ::close(fd); 73 if (sz < 0) {
74 return; 74 m_error = QString("Read error for file %1 (after %2 bytes)").arg(path).arg(offset);
75 delete[] filebuffer;
76 ::close(fd);
77 return;
78 }
79 offset += sz;
75 } 80 }
76 81
77 ::close(fd); 82 ::close(fd);
78 83
79 if (showProgress) { 84 if (showProgress) {
81 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()), 86 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()),
82 QObject::tr("Stop"), 0, 100); 87 QObject::tr("Stop"), 0, 100);
83 m_progress->hide(); 88 m_progress->hide();
84 } 89 }
85 90
86 if (!decode(filebuffer, stat.st_size)) { 91 if (!decode(filebuffer, m_fileSize)) {
87 m_error = QString("Failed to decode file %1.").arg(path); 92 m_error = QString("Failed to decode file %1.").arg(path);
88 delete[] filebuffer; 93 delete[] filebuffer;
89 return; 94 return;
90 } 95 }
91 96