comparison data/fileio/MP3FileReader.cpp @ 285:20028c634494

* change some QStrings to std::strings etc
author Chris Cannam
date Thu, 09 Aug 2007 16:29:29 +0000
parents f1f47660483d
children 92e8dbde73cd
comparison
equal deleted inserted replaced
284:41d64b873d87 285:20028c634494
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(QString path, DecodeMode decodeMode, CacheMode mode) : 37 MP3FileReader::MP3FileReader(std::string 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.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) { 55 if (::stat(path.c_str(), &stat) == -1 || stat.st_size == 0) {
56 m_error = QString("File %1 does not exist.").arg(path); 56 setError("File does not exist", 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.toLocal8Bit().data(), O_RDONLY 63 if ((fd = ::open(path.c_str(), 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 m_error = QString("Failed to open file %1 for reading.").arg(path); 68 setError("Failed to open file for reading", 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 m_error = QString("Out of memory"); 77 setError("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 m_error = QString("Read error for file %1 (after %2 bytes)") 87 setError("Read error", path);
88 .arg(path).arg(offset);
89 delete[] m_filebuffer; 88 delete[] m_filebuffer;
90 ::close(fd); 89 ::close(fd);
91 return; 90 return;
92 } else if (sz == 0) { 91 } else if (sz == 0) {
93 std::cerr << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes") 92 std::cerr << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes")
103 loadTags(); 102 loadTags();
104 103
105 if (decodeMode == DecodeAtOnce) { 104 if (decodeMode == DecodeAtOnce) {
106 105
107 m_progress = new QProgressDialog 106 m_progress = new QProgressDialog
108 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()), 107 (QObject::tr("Decoding %1...").arg(QFileInfo(path.c_str()).fileName()),
109 QObject::tr("Stop"), 0, 100); 108 QObject::tr("Stop"), 0, 100);
110 m_progress->hide(); 109 m_progress->hide();
111 110
112 if (!decode(m_filebuffer, m_fileSize)) { 111 if (!decode(m_filebuffer, m_fileSize)) {
113 m_error = QString("Failed to decode file %1.").arg(path); 112 setError("Failed to decode file", path);
114 } 113 }
115 114
116 delete[] m_filebuffer; 115 delete[] m_filebuffer;
117 m_filebuffer = 0; 116 m_filebuffer = 0;
118 117
146 { 145 {
147 m_title = ""; 146 m_title = "";
148 147
149 #ifdef HAVE_ID3TAG 148 #ifdef HAVE_ID3TAG
150 149
151 id3_file *file = id3_file_open(m_path.toLocal8Bit().data(), 150 id3_file *file = id3_file_open(m_path.c_str(),
152 ID3_FILE_MODE_READONLY); 151 ID3_FILE_MODE_READONLY);
153 if (!file) return; 152 if (!file) return;
154 153
155 // We can do this a lot more elegantly, but we'll leave that for 154 // We can do this a lot more elegantly, but we'll leave that for
156 // when we implement support for more than just the one tag! 155 // when we implement support for more than just the one tag!
202 std::cerr << "MP3FileReader::loadTags: ERROR: Internal error: Failed to convert UCS4 to UTF8 in ID3 title" << std::endl; 201 std::cerr << "MP3FileReader::loadTags: ERROR: Internal error: Failed to convert UCS4 to UTF8 in ID3 title" << std::endl;
203 id3_file_close(file); 202 id3_file_close(file);
204 return; 203 return;
205 } 204 }
206 205
207 m_title = QString::fromUtf8((const char *)u8str); 206 m_title = (const char *)u8str;
208 free(u8str); 207 free(u8str);
209 id3_file_close(file); 208 id3_file_close(file);
210 209
211 #else 210 #else
212 #ifdef DEBUG_ID3TAG 211 #ifdef DEBUG_ID3TAG
219 218
220 void 219 void
221 MP3FileReader::DecodeThread::run() 220 MP3FileReader::DecodeThread::run()
222 { 221 {
223 if (!m_reader->decode(m_reader->m_filebuffer, m_reader->m_fileSize)) { 222 if (!m_reader->decode(m_reader->m_filebuffer, m_reader->m_fileSize)) {
224 m_reader->m_error = QString("Failed to decode file %1.").arg(m_reader->m_path); 223 m_reader->setError("Failed to decode file", m_reader->m_path);
225 } 224 }
226 225
227 delete[] m_reader->m_filebuffer; 226 delete[] m_reader->m_filebuffer;
228 m_reader->m_filebuffer = 0; 227 m_reader->m_filebuffer = 0;
229 228
359 358
360 return MAD_FLOW_CONTINUE; 359 return MAD_FLOW_CONTINUE;
361 } 360 }
362 361
363 void 362 void
364 MP3FileReader::getSupportedExtensions(std::set<QString> &extensions) 363 MP3FileReader::getSupportedExtensions(std::set<std::string> &extensions)
365 { 364 {
366 extensions.insert("mp3"); 365 extensions.insert("mp3");
367 } 366 }
368 367
369 #endif 368 #endif