comparison data/fileio/MP3FileReader.cpp @ 290:92e8dbde73cd

* Revert revision 713. We do like QStrings after all.
author Chris Cannam
date Fri, 24 Aug 2007 11:41:48 +0000
parents 20028c634494
children c022976d18e8
comparison
equal deleted inserted replaced
289:3020904de772 290:92e8dbde73cd
32 32
33 #include <QApplication> 33 #include <QApplication>
34 #include <QFileInfo> 34 #include <QFileInfo>
35 #include <QProgressDialog> 35 #include <QProgressDialog>
36 36
37 MP3FileReader::MP3FileReader(std::string path, DecodeMode decodeMode, CacheMode mode) : 37 MP3FileReader::MP3FileReader(QString path, DecodeMode decodeMode, CacheMode mode) :
38 CodedAudioFileReader(mode), 38 CodedAudioFileReader(mode),
39 m_path(path), 39 m_path(path),
40 m_decodeThread(0) 40 m_decodeThread(0)
41 { 41 {
42 m_frameCount = 0; 42 m_frameCount = 0;
50 m_completion = 0; 50 m_completion = 0;
51 m_done = false; 51 m_done = false;
52 m_progress = 0; 52 m_progress = 0;
53 53
54 struct stat stat; 54 struct stat stat;
55 if (::stat(path.c_str(), &stat) == -1 || stat.st_size == 0) { 55 if (::stat(path.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) {
56 setError("File does not exist", path); 56 m_error = QString("File %1 does not exist.").arg(path);
57 return; 57 return;
58 } 58 }
59 59
60 m_fileSize = stat.st_size; 60 m_fileSize = stat.st_size;
61 61
62 int fd = -1; 62 int fd = -1;
63 if ((fd = ::open(path.c_str(), O_RDONLY 63 if ((fd = ::open(path.toLocal8Bit().data(), O_RDONLY
64 #ifdef _WIN32 64 #ifdef _WIN32
65 | O_BINARY 65 | O_BINARY
66 #endif 66 #endif
67 , 0)) < 0) { 67 , 0)) < 0) {
68 setError("Failed to open file for reading", path); 68 m_error = QString("Failed to open file %1 for reading.").arg(path);
69 return; 69 return;
70 } 70 }
71 71
72 m_filebuffer = 0; 72 m_filebuffer = 0;
73 73
74 try { 74 try {
75 m_filebuffer = new unsigned char[m_fileSize]; 75 m_filebuffer = new unsigned char[m_fileSize];
76 } catch (...) { 76 } catch (...) {
77 setError("Out of memory"); 77 m_error = QString("Out of memory");
78 ::close(fd); 78 ::close(fd);
79 return; 79 return;
80 } 80 }
81 81
82 ssize_t sz = 0; 82 ssize_t sz = 0;
83 size_t offset = 0; 83 size_t offset = 0;
84 while (offset < m_fileSize) { 84 while (offset < m_fileSize) {
85 sz = ::read(fd, m_filebuffer + offset, m_fileSize - offset); 85 sz = ::read(fd, m_filebuffer + offset, m_fileSize - offset);
86 if (sz < 0) { 86 if (sz < 0) {
87 setError("Read error", path); 87 m_error = QString("Read error for file %1 (after %2 bytes)")
88 .arg(path).arg(offset);
88 delete[] m_filebuffer; 89 delete[] m_filebuffer;
89 ::close(fd); 90 ::close(fd);
90 return; 91 return;
91 } else if (sz == 0) { 92 } else if (sz == 0) {
92 std::cerr << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes") 93 std::cerr << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes")
102 loadTags(); 103 loadTags();
103 104
104 if (decodeMode == DecodeAtOnce) { 105 if (decodeMode == DecodeAtOnce) {
105 106
106 m_progress = new QProgressDialog 107 m_progress = new QProgressDialog
107 (QObject::tr("Decoding %1...").arg(QFileInfo(path.c_str()).fileName()), 108 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()),
108 QObject::tr("Stop"), 0, 100); 109 QObject::tr("Stop"), 0, 100);
109 m_progress->hide(); 110 m_progress->hide();
110 111
111 if (!decode(m_filebuffer, m_fileSize)) { 112 if (!decode(m_filebuffer, m_fileSize)) {
112 setError("Failed to decode file", path); 113 m_error = QString("Failed to decode file %1.").arg(path);
113 } 114 }
114 115
115 delete[] m_filebuffer; 116 delete[] m_filebuffer;
116 m_filebuffer = 0; 117 m_filebuffer = 0;
117 118
145 { 146 {
146 m_title = ""; 147 m_title = "";
147 148
148 #ifdef HAVE_ID3TAG 149 #ifdef HAVE_ID3TAG
149 150
150 id3_file *file = id3_file_open(m_path.c_str(), 151 id3_file *file = id3_file_open(m_path.toLocal8Bit().data(),
151 ID3_FILE_MODE_READONLY); 152 ID3_FILE_MODE_READONLY);
152 if (!file) return; 153 if (!file) return;
153 154
154 // We can do this a lot more elegantly, but we'll leave that for 155 // We can do this a lot more elegantly, but we'll leave that for
155 // when we implement support for more than just the one tag! 156 // when we implement support for more than just the one tag!
201 std::cerr << "MP3FileReader::loadTags: ERROR: Internal error: Failed to convert UCS4 to UTF8 in ID3 title" << std::endl; 202 std::cerr << "MP3FileReader::loadTags: ERROR: Internal error: Failed to convert UCS4 to UTF8 in ID3 title" << std::endl;
202 id3_file_close(file); 203 id3_file_close(file);
203 return; 204 return;
204 } 205 }
205 206
206 m_title = (const char *)u8str; 207 m_title = QString::fromUtf8((const char *)u8str);
207 free(u8str); 208 free(u8str);
208 id3_file_close(file); 209 id3_file_close(file);
209 210
210 #else 211 #else
211 #ifdef DEBUG_ID3TAG 212 #ifdef DEBUG_ID3TAG
218 219
219 void 220 void
220 MP3FileReader::DecodeThread::run() 221 MP3FileReader::DecodeThread::run()
221 { 222 {
222 if (!m_reader->decode(m_reader->m_filebuffer, m_reader->m_fileSize)) { 223 if (!m_reader->decode(m_reader->m_filebuffer, m_reader->m_fileSize)) {
223 m_reader->setError("Failed to decode file", m_reader->m_path); 224 m_reader->m_error = QString("Failed to decode file %1.").arg(m_reader->m_path);
224 } 225 }
225 226
226 delete[] m_reader->m_filebuffer; 227 delete[] m_reader->m_filebuffer;
227 m_reader->m_filebuffer = 0; 228 m_reader->m_filebuffer = 0;
228 229
358 359
359 return MAD_FLOW_CONTINUE; 360 return MAD_FLOW_CONTINUE;
360 } 361 }
361 362
362 void 363 void
363 MP3FileReader::getSupportedExtensions(std::set<std::string> &extensions) 364 MP3FileReader::getSupportedExtensions(std::set<QString> &extensions)
364 { 365 {
365 extensions.insert("mp3"); 366 extensions.insert("mp3");
366 } 367 }
367 368
368 #endif 369 #endif