Switch to using QXmlStreamReader instead of SAX parser (part #2).
[LameXP.git] / src / Thread_FileAnalyzer_Task.cpp
blob02a96ad2a83446807c016868c499464832734bed
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Thread_FileAnalyzer_Task.h"
25 //Internal
26 #include "Global.h"
27 #include "LockedFile.h"
28 #include "Model_AudioFile.h"
29 #include "MimeTypes.h"
31 //MUtils
32 #include <MUtils/Global.h>
33 #include <MUtils/OSSupport.h>
34 #include <MUtils/Exception.h>
36 //Qt
37 #include <QDir>
38 #include <QFileInfo>
39 #include <QProcess>
40 #include <QDate>
41 #include <QTime>
42 #include <QDebug>
43 #include <QImage>
44 #include <QReadLocker>
45 #include <QWriteLocker>
46 #include <QThread>
47 #include <QXmlSimpleReader>
48 #include <QXmlInputSource>
49 #include <QXmlStreamReader>
50 #include <QStack>
52 //CRT
53 #include <math.h>
54 #include <time.h>
55 #include <assert.h>
57 //Debug
58 #undef DUMP_MI_OUTPUT
60 ////////////////////////////////////////////////////////////
61 // Helper Macros
62 ////////////////////////////////////////////////////////////
64 #define ADD_PROPTERY_MAPPING_1(TYPE, NAME) do \
65 { \
66 ADD_PROPTERY_MAPPING_2(TYPE, NAME, NAME); \
67 } \
68 while(0)
70 #define ADD_PROPTERY_MAPPING_2(TYPE, MI_NAME, LX_NAME) do \
71 { \
72 builder->insert(qMakePair(trackType_##TYPE, QString::fromLatin1(#MI_NAME)), propertyId_##LX_NAME); \
73 } \
74 while(0)
77 #define SET_OPTIONAL(TYPE, IF_CMD, THEN_CMD) do \
78 { \
79 TYPE _tmp;\
80 if((IF_CMD)) { THEN_CMD; } \
81 } \
82 while(0)
84 #define STRICMP(A,B) ((A).compare((B), Qt::CaseInsensitive) == 0)
86 ////////////////////////////////////////////////////////////
87 // Constructor
88 ////////////////////////////////////////////////////////////
90 AnalyzeTask::AnalyzeTask(const int taskId, const QString &inputFile, QAtomicInt &abortFlag)
92 m_taskId(taskId),
93 m_inputFile(inputFile),
94 m_mediaInfoBin(lamexp_tools_lookup("mediainfo.exe")),
95 m_mediaInfoVer(lamexp_tools_version("mediainfo.exe")),
96 m_avs2wavBin(lamexp_tools_lookup("avs2wav.exe")),
97 m_abortFlag(abortFlag),
98 m_propertiesIdx(initPropertiesIdx()),
99 m_trackTypes(initTrackTypes())
101 if(m_mediaInfoBin.isEmpty() || m_avs2wavBin.isEmpty())
103 qFatal("Invalid path to MediaInfo binary. Tool not initialized properly.");
107 AnalyzeTask::~AnalyzeTask(void)
109 emit taskCompleted(m_taskId);
112 ////////////////////////////////////////////////////////////
113 // Static initialization
114 ////////////////////////////////////////////////////////////
116 QReadWriteLock AnalyzeTask::s_lock;
117 QScopedPointer<const QMap<QPair<AnalyzeTask::MI_trackType_t, QString>, AnalyzeTask::MI_propertyId_t>> AnalyzeTask::s_pPropertiesIdx;
118 QScopedPointer<const QMap<QString, AnalyzeTask::MI_trackType_t>> AnalyzeTask::s_pTrackTypes;
120 const QMap<QPair<AnalyzeTask::MI_trackType_t, QString>, AnalyzeTask::MI_propertyId_t> &AnalyzeTask::initPropertiesIdx(void)
122 QReadLocker rdLocker(&s_lock);
123 if (s_pPropertiesIdx.isNull())
125 rdLocker.unlock();
126 QWriteLocker wrLocker(&s_lock);
127 if (s_pPropertiesIdx.isNull())
129 QMap<QPair<MI_trackType_t, QString>, MI_propertyId_t> *const builder = new QMap<QPair<MI_trackType_t, QString>, MI_propertyId_t>();
130 ADD_PROPTERY_MAPPING_2(gen, format, container);
131 ADD_PROPTERY_MAPPING_2(gen, format_profile, container_profile);
132 ADD_PROPTERY_MAPPING_1(gen, duration);
133 ADD_PROPTERY_MAPPING_1(gen, title);
134 ADD_PROPTERY_MAPPING_2(gen, track, title);
135 ADD_PROPTERY_MAPPING_1(gen, artist);
136 ADD_PROPTERY_MAPPING_2(gen, performer, artist);
137 ADD_PROPTERY_MAPPING_1(gen, album);
138 ADD_PROPTERY_MAPPING_1(gen, genre);
139 ADD_PROPTERY_MAPPING_1(gen, released_date);
140 ADD_PROPTERY_MAPPING_2(gen, recorded_date, released_date);
141 ADD_PROPTERY_MAPPING_1(gen, track_position);
142 ADD_PROPTERY_MAPPING_1(gen, comment);
143 ADD_PROPTERY_MAPPING_1(aud, format);
144 ADD_PROPTERY_MAPPING_1(aud, format_version);
145 ADD_PROPTERY_MAPPING_1(aud, format_profile);
146 ADD_PROPTERY_MAPPING_1(aud, duration);
147 ADD_PROPTERY_MAPPING_1(aud, channel_s_);
148 ADD_PROPTERY_MAPPING_1(aud, samplingrate);
149 ADD_PROPTERY_MAPPING_1(aud, bitdepth);
150 ADD_PROPTERY_MAPPING_1(aud, bitrate);
151 ADD_PROPTERY_MAPPING_1(aud, bitrate_mode);
152 ADD_PROPTERY_MAPPING_1(aud, encoded_library);
153 s_pPropertiesIdx.reset(builder);
155 wrLocker.unlock();
156 rdLocker.relock();
158 return (*s_pPropertiesIdx);
161 const QMap<QString, AnalyzeTask::MI_trackType_t> &AnalyzeTask::initTrackTypes(void)
163 QReadLocker rdLocker(&s_lock);
164 if (s_pTrackTypes.isNull())
166 rdLocker.unlock();
167 QWriteLocker wrLocker(&s_lock);
168 if (s_pTrackTypes.isNull())
170 QMap<QString, AnalyzeTask::MI_trackType_t> *const builder = new QMap<QString, AnalyzeTask::MI_trackType_t>();
171 builder->insert("general", trackType_gen);
172 builder->insert("audio", trackType_aud);
173 s_pTrackTypes.reset(builder);
175 wrLocker.unlock();
176 rdLocker.relock();
178 return (*s_pTrackTypes);
181 ////////////////////////////////////////////////////////////
182 // Thread Main
183 ////////////////////////////////////////////////////////////
185 void AnalyzeTask::run()
189 run_ex();
191 catch(const std::exception &error)
193 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
194 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
196 catch(...)
198 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
199 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
203 void AnalyzeTask::run_ex(void)
205 int fileType = fileTypeNormal;
206 QString currentFile = QDir::fromNativeSeparators(m_inputFile);
207 qDebug("Analyzing: %s", MUTILS_UTF8(currentFile));
209 AudioFileModel fileInfo(currentFile);
210 analyzeFile(currentFile, fileInfo, &fileType);
212 if(MUTILS_BOOLIFY(m_abortFlag))
214 qWarning("Operation cancelled by user!");
215 return;
218 switch(fileType)
220 case fileTypeDenied:
221 qWarning("Cannot access file for reading, skipping!");
222 break;
223 case fileTypeCDDA:
224 qWarning("Dummy CDDA file detected, skipping!");
225 break;
226 default:
227 if(fileInfo.metaInfo().title().isEmpty() || fileInfo.techInfo().containerType().isEmpty() || fileInfo.techInfo().audioType().isEmpty())
229 fileType = fileTypeUnknown;
230 if(!QFileInfo(currentFile).suffix().compare("cue", Qt::CaseInsensitive))
232 qWarning("Cue Sheet file detected, skipping!");
233 fileType = fileTypeCueSheet;
235 else if(!QFileInfo(currentFile).suffix().compare("avs", Qt::CaseInsensitive))
237 qDebug("Found a potential Avisynth script, investigating...");
238 if(analyzeAvisynthFile(currentFile, fileInfo))
240 fileType = fileTypeNormal;
242 else
244 qDebug("Rejected Avisynth file: %s", MUTILS_UTF8(fileInfo.filePath()));
247 else
249 qDebug("Rejected file of unknown type: %s", MUTILS_UTF8(fileInfo.filePath()));
252 break;
255 //Emit the file now!
256 emit fileAnalyzed(m_taskId, fileType, fileInfo);
259 ////////////////////////////////////////////////////////////
260 // Privtae Functions
261 ////////////////////////////////////////////////////////////
263 const AudioFileModel& AnalyzeTask::analyzeFile(const QString &filePath, AudioFileModel &audioFile, int *const type)
265 *type = fileTypeNormal;
266 QFile readTest(filePath);
268 if (!readTest.open(QIODevice::ReadOnly))
270 *type = fileTypeDenied;
271 return audioFile;
274 if (checkFile_CDDA(readTest))
276 *type = fileTypeCDDA;
277 return audioFile;
280 readTest.close();
281 return analyzeMediaFile(filePath, audioFile);
284 const AudioFileModel& AnalyzeTask::analyzeMediaFile(const QString &filePath, AudioFileModel &audioFile)
286 //bool skipNext = false;
287 QPair<quint32, quint32> id_val(UINT_MAX, UINT_MAX);
288 quint32 coverType = UINT_MAX;
289 QByteArray coverData;
291 QStringList params;
292 params << QString("--Language=raw");
293 params << QString("-f");
294 params << QString("--Output=XML");
295 params << QDir::toNativeSeparators(filePath);
297 QProcess process;
298 MUtils::init_process(process, QFileInfo(m_mediaInfoBin).absolutePath());
299 process.start(m_mediaInfoBin, params);
301 QByteArray data;
302 data.reserve(16384);
304 if(!process.waitForStarted())
306 qWarning("MediaInfo process failed to create!");
307 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
308 process.kill();
309 process.waitForFinished(-1);
310 return audioFile;
313 while(process.state() != QProcess::NotRunning)
315 if(MUTILS_BOOLIFY(m_abortFlag))
317 process.kill();
318 qWarning("Process was aborted on user request!");
319 break;
322 if(!process.waitForReadyRead())
324 if(process.state() == QProcess::Running)
326 qWarning("MediaInfo time out. Killing the process now!");
327 process.kill();
328 process.waitForFinished(-1);
329 break;
333 forever
335 const QByteArray dataNext = process.readAll();
336 if (dataNext.isEmpty()) {
337 break; /*no more input data*/
339 data += dataNext;
343 process.waitForFinished();
344 if (process.state() != QProcess::NotRunning)
346 process.kill();
347 process.waitForFinished(-1);
350 while (!process.atEnd())
352 const QByteArray dataNext = process.readAll();
353 if (dataNext.isEmpty()) {
354 break; /*no more input data*/
356 data += dataNext;
359 #if defined(DUMP_MI_OUTPUT)
360 qDebug("!!!--START-->>>\n%s\n<<<--END--!!!", data.constData());
361 #endif //DUMP_MI_OUTPUT
363 return parseMediaInfo(data, audioFile);
366 const AudioFileModel& AnalyzeTask::parseMediaInfo(const QByteArray &data, AudioFileModel &audioFile)
368 QXmlStreamReader xmlStream(data);
369 bool firstFile = true;
371 if (findNextElement(QLatin1String("MediaInfo"), xmlStream))
373 const QString version = findAttribute(QLatin1String("Version"), xmlStream.attributes());
374 if (version.isEmpty() || (!STRICMP(version, QString().sprintf("0.%u.%02u", m_mediaInfoVer / 100U, m_mediaInfoVer % 100))))
376 qWarning("Invalid version property \"%s\" was detected!", MUTILS_UTF8(version));
377 return audioFile;
379 while (findNextElement(QLatin1String("File"), xmlStream))
381 if (firstFile)
383 firstFile = false;
384 parseFileInfo(xmlStream, audioFile);
386 else
388 qWarning("Skipping non-primary file!");
389 xmlStream.skipCurrentElement();
394 if (!(audioFile.techInfo().containerType().isEmpty() || audioFile.techInfo().audioType().isEmpty()))
396 if (audioFile.metaInfo().title().isEmpty())
398 QString baseName = QFileInfo(audioFile.filePath()).fileName();
399 int index;
400 if ((index = baseName.lastIndexOf(".")) >= 0)
402 baseName = baseName.left(index);
404 baseName = baseName.replace("_", " ").simplified();
405 if ((index = baseName.lastIndexOf(" - ")) >= 0)
407 baseName = baseName.mid(index + 3).trimmed();
409 audioFile.metaInfo().setTitle(baseName);
411 if ((audioFile.techInfo().audioType().compare("PCM", Qt::CaseInsensitive) == 0) && (audioFile.techInfo().audioProfile().compare("Float", Qt::CaseInsensitive) == 0))
413 if (audioFile.techInfo().audioBitdepth() == 32) audioFile.techInfo().setAudioBitdepth(AudioFileModel::BITDEPTH_IEEE_FLOAT32);
416 else
418 qWarning("Audio file format could *not* be recognized!");
421 return audioFile;
424 void AnalyzeTask::parseFileInfo(QXmlStreamReader &xmlStream, AudioFileModel &audioFile)
426 MI_trackType_t trackType;
427 QSet<MI_trackType_t> tracksProcessed;
429 while (findNextElement(QLatin1String("Track"), xmlStream))
431 const QString typeString = findAttribute(QLatin1String("Type"), xmlStream.attributes());
432 if ((trackType = m_trackTypes.value(typeString.toLower(), MI_trackType_t(-1))) != MI_trackType_t(-1))
434 if (!tracksProcessed.contains(trackType))
436 tracksProcessed << trackType;
437 parseTrackInfo(xmlStream, trackType, audioFile);
439 else
441 qWarning("Skipping non-primary '%s' track!", MUTILS_UTF8(typeString));
442 xmlStream.skipCurrentElement();
445 else
447 qWarning("Skipping unsupported '%s' track!", MUTILS_UTF8(typeString));
448 xmlStream.skipCurrentElement();
453 void AnalyzeTask::parseTrackInfo(QXmlStreamReader &xmlStream, const MI_trackType_t trackType, AudioFileModel &audioFile)
455 while (xmlStream.readNextStartElement())
457 qDebug("%d::%s", trackType, MUTILS_UTF8(xmlStream.name()));
458 const MI_propertyId_t idx = s_pPropertiesIdx->value(qMakePair(trackType, xmlStream.name().toString().simplified().toLower()), MI_propertyId_t(-1));
459 if (idx != MI_propertyId_t(-1))
461 const QString encoding = findAttribute(QLatin1String("dt"), xmlStream.attributes());
462 const QString value = xmlStream.readElementText(QXmlStreamReader::SkipChildElements).simplified();
463 if (!value.isEmpty())
465 parseProperty(encoding.isEmpty() ? value : decodeStr(value, encoding), idx, audioFile);
468 else
470 xmlStream.skipCurrentElement();
475 void AnalyzeTask::parseProperty(const QString &value, const MI_propertyId_t propertyIdx, AudioFileModel &audioFile)
477 qWarning("--> %d: \"%s\"", propertyIdx, MUTILS_UTF8(value));
478 switch (propertyIdx)
480 case propertyId_container: audioFile.techInfo().setContainerType(value); return;
481 case propertyId_container_profile: audioFile.techInfo().setContainerProfile(value); return;
482 case propertyId_duration: SET_OPTIONAL(quint32, parseDuration(value, _tmp), audioFile.techInfo().setDuration(_tmp)); return;
483 case propertyId_title: audioFile.metaInfo().setTitle(value); return;
484 case propertyId_artist: audioFile.metaInfo().setArtist(value); return;
485 case propertyId_album: audioFile.metaInfo().setAlbum(value); return;
486 case propertyId_genre: audioFile.metaInfo().setGenre(value); return;
487 case propertyId_released_date: SET_OPTIONAL(quint32, parseYear(value, _tmp), audioFile.metaInfo().setYear(_tmp)); return;
488 case propertyId_track_position: SET_OPTIONAL(quint32, parseUnsigned(value, _tmp), audioFile.metaInfo().setPosition(_tmp)); return;
489 case propertyId_comment: audioFile.metaInfo().setComment(value); return;
490 case propertyId_format: audioFile.techInfo().setAudioType(value); return;
491 case propertyId_format_version: audioFile.techInfo().setAudioVersion(value); return;
492 case propertyId_format_profile: audioFile.techInfo().setAudioProfile(value); return;
493 case propertyId_channel_s_: SET_OPTIONAL(quint32, parseUnsigned(value, _tmp), audioFile.techInfo().setAudioChannels(_tmp)); return;
494 case propertyId_samplingrate: SET_OPTIONAL(quint32, parseUnsigned(value, _tmp), audioFile.techInfo().setAudioSamplerate(_tmp)); return;
495 case propertyId_bitdepth: SET_OPTIONAL(quint32, parseUnsigned(value, _tmp), audioFile.techInfo().setAudioBitdepth(_tmp)); return;
496 case propertyId_bitrate: SET_OPTIONAL(quint32, parseDuration(value, _tmp), audioFile.techInfo().setAudioBitrate(_tmp)); return;
497 case propertyId_bitrate_mode: SET_OPTIONAL(quint32, parseRCMode(value, _tmp), audioFile.techInfo().setAudioBitrateMode(_tmp)); return;
498 case propertyId_encoded_library: audioFile.techInfo().setAudioEncodeLib(cleanAsciiStr(value)); return;
499 default: MUTILS_THROW_FMT("Invalid property ID: %d", propertyIdx);
503 bool AnalyzeTask::checkFile_CDDA(QFile &file)
505 file.reset();
506 QByteArray data = file.read(128);
508 int i = data.indexOf("RIFF");
509 int j = data.indexOf("CDDA");
510 int k = data.indexOf("fmt ");
512 return ((i >= 0) && (j >= 0) && (k >= 0) && (k > j) && (j > i));
516 void AnalyzeTask::retrieveCover(AudioFileModel &audioFile, const quint32 coverType, const QByteArray &coverData)
518 qDebug("Retrieving cover! (MIME_TYPES_MAX=%u)", MIME_TYPES_MAX);
520 static const QString ext = QString::fromLatin1(MIME_TYPES[qBound(0U, coverType, MIME_TYPES_MAX)].ext[0]);
521 if(!(QImage::fromData(coverData, ext.toUpper().toLatin1().constData()).isNull()))
523 QFile coverFile(QString("%1/%2.%3").arg(MUtils::temp_folder(), MUtils::next_rand_str(), ext));
524 if(coverFile.open(QIODevice::WriteOnly))
526 coverFile.write(coverData);
527 coverFile.close();
528 audioFile.metaInfo().setCover(coverFile.fileName(), true);
531 else
533 qWarning("Image data seems to be invalid :-(");
538 bool AnalyzeTask::analyzeAvisynthFile(const QString &filePath, AudioFileModel &info)
540 QProcess process;
541 MUtils::init_process(process, QFileInfo(m_avs2wavBin).absolutePath());
543 process.start(m_avs2wavBin, QStringList() << QDir::toNativeSeparators(filePath) << "?");
545 if(!process.waitForStarted())
547 qWarning("AVS2WAV process failed to create!");
548 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
549 process.kill();
550 process.waitForFinished(-1);
551 return false;
554 bool bInfoHeaderFound = false;
556 while(process.state() != QProcess::NotRunning)
558 if(MUTILS_BOOLIFY(m_abortFlag))
560 process.kill();
561 qWarning("Process was aborted on user request!");
562 break;
565 if(!process.waitForReadyRead())
567 if(process.state() == QProcess::Running)
569 qWarning("AVS2WAV time out. Killing process and skipping file!");
570 process.kill();
571 process.waitForFinished(-1);
572 return false;
576 QByteArray data;
578 while(process.canReadLine())
580 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
581 if(!line.isEmpty())
583 int index = line.indexOf(':');
584 if(index > 0)
586 QString key = line.left(index).trimmed();
587 QString val = line.mid(index+1).trimmed();
589 if(bInfoHeaderFound && !key.isEmpty() && !val.isEmpty())
591 if(key.compare("TotalSeconds", Qt::CaseInsensitive) == 0)
593 bool ok = false;
594 unsigned int duration = val.toUInt(&ok);
595 if(ok) info.techInfo().setDuration(duration);
597 if(key.compare("SamplesPerSec", Qt::CaseInsensitive) == 0)
599 bool ok = false;
600 unsigned int samplerate = val.toUInt(&ok);
601 if(ok) info.techInfo().setAudioSamplerate (samplerate);
603 if(key.compare("Channels", Qt::CaseInsensitive) == 0)
605 bool ok = false;
606 unsigned int channels = val.toUInt(&ok);
607 if(ok) info.techInfo().setAudioChannels(channels);
609 if(key.compare("BitsPerSample", Qt::CaseInsensitive) == 0)
611 bool ok = false;
612 unsigned int bitdepth = val.toUInt(&ok);
613 if(ok) info.techInfo().setAudioBitdepth(bitdepth);
617 else
619 if(line.contains("[Audio Info]", Qt::CaseInsensitive))
621 info.techInfo().setAudioType("Avisynth");
622 info.techInfo().setContainerType("Avisynth");
623 bInfoHeaderFound = true;
630 process.waitForFinished();
631 if(process.state() != QProcess::NotRunning)
633 process.kill();
634 process.waitForFinished(-1);
637 //Check exit code
638 switch(process.exitCode())
640 case 0:
641 qDebug("Avisynth script was analyzed successfully.");
642 return true;
643 break;
644 case -5:
645 qWarning("It appears that Avisynth is not installed on the system!");
646 return false;
647 break;
648 default:
649 qWarning("Failed to open the Avisynth script, bad AVS file?");
650 return false;
651 break;
655 // ---------------------------------------------------------
656 // Utility Functions
657 // ---------------------------------------------------------
659 QString AnalyzeTask::decodeStr(const QString &str, const QString &encoding)
661 if (STRICMP(encoding, QLatin1String("binary.base64")))
663 const QString decoded = QString::fromUtf8(QByteArray::fromBase64(str.toLatin1()));
664 return decoded;
666 return QString();
669 bool AnalyzeTask::parseUnsigned(const QString &str, quint32 &value)
671 bool okay = false;
672 value = str.toUInt(&okay);
673 return okay;
675 bool AnalyzeTask::parseDuration(const QString &str, quint32 &value)
677 if (parseUnsigned(str, value))
679 value = (value + 500U) / 1000U;
680 return true;
682 return false;
685 bool AnalyzeTask::parseYear(const QString &str, quint32 &value)
687 if (str.startsWith(QLatin1String("UTC"), Qt::CaseInsensitive))
689 const QDate date = QDate::fromString(str.mid(3).trimmed().left(10), QLatin1String("yyyy-MM-dd"));
690 if (date.isValid())
692 value = date.year();
693 return true;
695 return false;
697 else
699 return parseUnsigned(str, value);
703 bool AnalyzeTask::parseRCMode(const QString &str, quint32 &value)
705 if (STRICMP(str, QLatin1String("CBR")))
707 value = AudioFileModel::BitrateModeConstant;
708 return true;
710 if (STRICMP(str, QLatin1String("VBR")))
712 value = AudioFileModel::BitrateModeVariable;
713 return true;
715 return false;
718 QString AnalyzeTask::cleanAsciiStr(const QString &str)
720 QByteArray ascii = str.toLatin1();
721 for (QByteArray::Iterator iter = ascii.begin(); iter != ascii.end(); ++iter)
723 if ((*iter < 0x20) || (*iter >= 0x7F)) *iter = 0x3F;
725 return QString::fromLatin1(ascii).remove(QLatin1Char('?')).simplified();
728 bool AnalyzeTask::findNextElement(const QString &name, QXmlStreamReader &xmlStream)
730 while (xmlStream.readNextStartElement())
732 if (STRICMP(xmlStream.name(), name))
734 return true;
736 xmlStream.skipCurrentElement();
738 return false;
741 QString AnalyzeTask::findAttribute(const QString &name, const QXmlStreamAttributes &xmlAttributes)
743 for (QXmlStreamAttributes::ConstIterator iter = xmlAttributes.constBegin(); iter != xmlAttributes.constEnd(); ++iter)
745 if (STRICMP(iter->name(), name))
747 const QString value = iter->value().toString().simplified();
748 if (!value.isEmpty())
750 return value; /*found*/
754 return QString();
757 ////////////////////////////////////////////////////////////
758 // Public Functions
759 ////////////////////////////////////////////////////////////
761 /*NONE*/
763 ////////////////////////////////////////////////////////////
764 // EVENTS
765 ////////////////////////////////////////////////////////////
767 /*NONE*/