changeset 229:b176e99f9c38

...
author Chris Cannam
date Mon, 12 Feb 2007 13:20:43 +0000
parents f02989f7e160
children 2854dc429310
files data/fileio/MP3FileReader.cpp
diffstat 1 files changed, 14 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/MP3FileReader.cpp	Mon Feb 12 13:09:31 2007 +0000
+++ b/data/fileio/MP3FileReader.cpp	Mon Feb 12 13:20:43 2007 +0000
@@ -50,7 +50,7 @@
 
     m_fileSize = stat.st_size;
 
-    int fd;
+    int fd = -1;
     if ((fd = ::open(path.toLocal8Bit().data(), O_RDONLY, 0)) < 0) {
 	m_error = QString("Failed to open file %1 for reading.").arg(path);
 	return;
@@ -59,19 +59,24 @@
     unsigned char *filebuffer = 0;
 
     try {
-        filebuffer = new unsigned char[stat.st_size];
+        filebuffer = new unsigned char[m_fileSize];
     } catch (...) {
         m_error = QString("Out of memory");
         ::close(fd);
 	return;
     }
     
-    size_t sz = 0;
-    if ((sz = ::read(fd, filebuffer, stat.st_size)) < stat.st_size) {
-	m_error = QString("Failed to read file %1 (expected %2 bytes, got %3 bytes).").arg(path).arg(stat.st_size).arg(sz);
-        delete[] filebuffer;
-        ::close(fd);
-	return;
+    ssize_t sz = 0;
+    size_t offset = 0;
+    while (offset < m_fileSize) {
+        sz = ::read(fd, filebuffer + offset, m_fileSize - offset);
+        if (sz < 0) {
+            m_error = QString("Read error for file %1 (after %2 bytes)").arg(path).arg(offset);
+            delete[] filebuffer;
+            ::close(fd);
+            return;
+        }
+        offset += sz;
     }
 
     ::close(fd);
@@ -83,7 +88,7 @@
 	m_progress->hide();
     }
 
-    if (!decode(filebuffer, stat.st_size)) {
+    if (!decode(filebuffer, m_fileSize)) {
 	m_error = QString("Failed to decode file %1.").arg(path);
         delete[] filebuffer;
 	return;