diff options
Diffstat (limited to 'src/plugins/Input/cue/cuefile.cpp')
| -rw-r--r-- | src/plugins/Input/cue/cuefile.cpp | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/src/plugins/Input/cue/cuefile.cpp b/src/plugins/Input/cue/cuefile.cpp new file mode 100644 index 000000000..69a4622b9 --- /dev/null +++ b/src/plugins/Input/cue/cuefile.cpp @@ -0,0 +1,201 @@ +/*************************************************************************** + * Copyright (C) 2008-2019 by Ilya Kotov * + * forkotov02@ya.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include <QFile> +#include <QDir> +#include <QDirIterator> +#include <QSettings> +#include <QTextStream> +#include <QTextCodec> +#include <qmmp/decoder.h> +#include <qmmp/metadatamanager.h> +#ifdef WITH_ENCA +#include <enca.h> +#endif +#include "cuefile.h" + +CueFile::CueFile(const QString &path) : CueParser() +{ + QString fileName = path; + if(path.contains("://")) + { + fileName.remove("cue://"); + fileName.remove(QRegExp("#\\d+$")); + } + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly)) + { + qDebug("CueFile: error: %s", qPrintable(file.errorString())); + return; + } + QByteArray data = file.readAll(); + file.close(); + + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + settings.beginGroup("CUE"); + m_dirty = settings.value("dirty_cue", false).toBool(); + QTextCodec *codec = nullptr; +#ifdef WITH_ENCA + EncaAnalyser analyser = nullptr; + if(settings.value("use_enca", false).toBool()) + { + analyser = enca_analyser_alloc(settings.value("enca_lang").toByteArray ().constData()); + + if(analyser) + { + enca_set_threshold(analyser, 1.38); + EncaEncoding encoding = enca_analyse(analyser, (uchar *)data.constData(), data.size()); + file.reset(); + if(encoding.charset != ENCA_CS_UNKNOWN) + { + codec = QTextCodec::codecForName(enca_charset_name(encoding.charset,ENCA_NAME_STYLE_ENCA)); + //qDebug("CUEParser: detected charset: %s", + // enca_charset_name(encoding.charset,ENCA_NAME_STYLE_ENCA)); + } + enca_analyser_free(analyser); + } + } +#endif + if(!codec) + codec = QTextCodec::codecForName(settings.value("encoding","UTF-8").toByteArray ()); + if(!codec) + codec = QTextCodec::codecForName("UTF-8"); + settings.endGroup(); + //qDebug("CUEParser: using %s encoding", codec->name().constData()); + loadData(data, codec->name()); + setUrl("cue", path); + for(const QString &dataFileName : files()) + { + QString dataFilePath = getDirtyPath(dataFileName, QFileInfo(fileName).dir().filePath(dataFileName)); + m_dataFiles.insert(dataFileName, dataFilePath); + QList<TrackInfo *> pl = MetaDataManager::instance()->createPlayList(dataFilePath, TrackInfo::Properties); + if(!pl.isEmpty()) + { + setProperties(dataFileName, pl.first()->properties()); + setDuration(dataFileName, pl.first()->duration()); + qDeleteAll(pl); + pl.clear(); + } + } +} + +CueFile::~CueFile() +{} + +QString CueFile::dataFilePath(int track) const +{ + return m_dataFiles.value(file(track)); +} + +QStringList CueFile::dataFilePaths() const +{ + return m_dataFiles.values(); +} + +QStringList CueFile::splitLine(const QString &line) +{ + //qDebug("raw string = %s",qPrintable(line)); + QStringList list; + QString buf = line.trimmed(); + if (buf.isEmpty()) + return list; + while (!buf.isEmpty()) + { + //qDebug(qPrintable(buf)); + if (buf.startsWith('"')) + { + int end = buf.indexOf('"',1); + if(end == -1) //ignore invalid line + { + list.clear(); + qWarning("CUEParser: unable to parse line: %s",qPrintable(line)); + return list; + } + list << buf.mid (1, end - 1); + buf.remove (0, end+1); + } + else + { + int end = buf.indexOf(' ', 0); + if (end < 0) + end = buf.size(); + list << buf.mid (0, end); + buf.remove (0, end); + } + buf = buf.trimmed(); + } + return list; +} + +/*qint64 CueFile::getLength(const QString &str) +{ + QStringList list = str.split(":"); + if (list.size() == 2) + return (qint64)list.at(0).toInt()*60000 + list.at(1).toInt()*1000; + else if (list.size() == 3) + return (qint64)list.at(0).toInt()*60000 + list.at(1).toInt()*1000 + list.at(2).toInt()*1000/75; + return 0; +}*/ + +QString CueFile::getDirtyPath(const QString &cue_path, const QString &path) +{ + if((QFile::exists(path) && Decoder::findByFilePath(path)) || !m_dirty) + return path; + + QStringList candidates; + QDirIterator it(QFileInfo(path).dir().path(), QDir::Files); + while (it.hasNext()) + { + it.next(); + QString f = it.filePath(); + if ((f != cue_path) && Decoder::findByFilePath(f)) + candidates.push_back(f); + } + + if (candidates.empty()) + return path; + else if (candidates.count() == 1) + return candidates.first(); + + int dot = cue_path.lastIndexOf('.'); + if (dot != -1) + { + QRegExp r(QRegExp::escape(cue_path.left(dot)) + "\\.[^\\.]+$"); + + int index = candidates.indexOf(r); + int rindex = candidates.lastIndexOf(r); + + if ((index != -1) && (index == rindex)) + return candidates[index]; + } + dot = path.lastIndexOf('.'); + if (dot != -1) + { + QRegExp r(QRegExp::escape(path.left(dot)) + "\\.[^\\.]+$"); + + int index = candidates.indexOf(r); + int rindex = candidates.lastIndexOf(r); + + if ((index != -1) && (index == rindex)) + return candidates[index]; + } + + return path; +} |
