OSDN Git Service

Added support for the Visual Leak Detector. This effects "Debug" builds only.
[lamexp/LameXP.git] / src / PlaylistImporter.cpp
index 9b58405..1065225 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // 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
@@ -29,6 +29,7 @@
 #include <QDate>
 #include <QTime>
 #include <QDebug>
+#include <QTextCodec>
 
 //Un-escape XML characters
 static const struct
@@ -85,6 +86,13 @@ bool PlaylistImporter::importPlaylist(QStringList &fileList, const QString &play
                return false;
        }
 
+       //Skip very large files (parsing could take very long)
+       if(data.size() >= 10485760i64)
+       {
+               qWarning("File is very big. Probably not a Playlist. Rejecting...");
+               return false;
+       }
+
        //Parse playlist depending on type
        switch(playlistType)
        {
@@ -131,46 +139,87 @@ PlaylistImporter::playlist_t PlaylistImporter::isPlaylist(const QString &fileNam
 
 bool PlaylistImporter::parsePlaylist_m3u(QFile &data, QStringList &fileList, const QDir &baseDir, const QDir &rootDir)
 {
-       QByteArray line = data.readLine();
+       const QTextCodec *codec = QTextCodec::codecForName("System");
        const bool preferUTF8 = data.fileName().endsWith(".m3u8", Qt::CaseInsensitive);
+       bool foundAtLeastOneFile = false;
+       
+       data.reset();
 
-       while(line.size() > 0)
+       while(!data.atEnd())
        {
                QString filePath[3];
+               QByteArray line = data.readLine();
 
                if(preferUTF8)
                {
                        filePath[0] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
-                       filePath[1] = QString(QDir::fromNativeSeparators(QString::fromLocal8Bit(line.constData(), line.size()).trimmed()));
+                       filePath[1] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
                        filePath[2] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
                }
                else
                {
-                       filePath[0] = QString(QDir::fromNativeSeparators(QString::fromLocal8Bit(line.constData(), line.size()).trimmed()));
+                       filePath[0] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
                        filePath[1] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
                        filePath[2] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
                }
 
                for(size_t i = 0; i < 3; i++)
                {
-                       if(!(filePath[i].startsWith("#") || filePath[i].contains(QChar(QChar::ReplacementCharacter))))
+                       if(!(filePath[i].isEmpty() || filePath[i].startsWith("#") || filePath[i].contains(QChar(QChar::ReplacementCharacter))))
                        {
                                QFileInfo filename(filePath[i]);
                                filename.setCaching(false);
                                fixFilePath(filename, baseDir, rootDir);
 
-                               if(filename.exists())
+                               if(filename.exists() && filename.isFile())
                                {
+                                       qDebug("Found: \"%s\"", filePath[i].toUtf8().constData());
                                        if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
                                        {
                                                fileList << filename.canonicalFilePath();
                                        }
+                                       foundAtLeastOneFile = true;
                                        break;
                                }
                        }
                }
+       }
+
+       //If we did not find any files yet, try UTF-16 now
+       if(!foundAtLeastOneFile)
+       {
+               const char* codecs[2] = {"UTF-16LE", "UTF-16BE"};
+
+               for(size_t i = 0; i < 2; i++)
+               {
+                       QTextStream stream(&data);
+                       stream.setAutoDetectUnicode(false);
+                       stream.setCodec(codecs[i]);
+                       stream.seek(0i64);
+
+                       while(!stream.atEnd())
+                       {
+                               QString filePath = stream.readLine().trimmed();
 
-               line = data.readLine();
+                               if(!(filePath.isEmpty() || filePath.startsWith("#") || filePath.contains(QChar(QChar::ReplacementCharacter))))
+                               {
+                                       QFileInfo filename(filePath);
+                                       filename.setCaching(false);
+                                       fixFilePath(filename, baseDir, rootDir);
+
+                                       if(filename.exists() && filename.isFile())
+                                       {
+                                               if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
+                                               {
+                                                       fileList << filename.canonicalFilePath();
+                                               }
+                                               foundAtLeastOneFile = true;
+                                       }
+                               }
+                       }
+
+                       if(foundAtLeastOneFile) break;
+               }
        }
 
        return true;
@@ -179,16 +228,20 @@ bool PlaylistImporter::parsePlaylist_m3u(QFile &data, QStringList &fileList, con
 bool PlaylistImporter::parsePlaylist_pls(QFile &data, QStringList &fileList, const QDir &baseDir, const QDir &rootDir)
 {
        QRegExp plsEntry("File(\\d+)=(.+)", Qt::CaseInsensitive);
-       QByteArray line = data.readLine();
-       
-       while(line.size() > 0)
+       const QTextCodec *codec = QTextCodec::codecForName("System");
+       bool foundAtLeastOneFile = false;
+
+       data.reset();
+
+       while(!data.atEnd())
        {
                QString filePath[3];
+               QByteArray line = data.readLine();
 
-               filePath[0] = QString(QDir::fromNativeSeparators(QString::fromLocal8Bit(line.constData(), line.size()).trimmed()));
-               filePath[2] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
-               filePath[1] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
-
+               filePath[0] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
+               filePath[1] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
+               filePath[2] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
+               
                for(size_t i = 0; i < 3; i++)
                {
                        if(!filePath[i].contains(QChar(QChar::ReplacementCharacter)))
@@ -199,19 +252,58 @@ bool PlaylistImporter::parsePlaylist_pls(QFile &data, QStringList &fileList, con
                                        filename.setCaching(false);
                                        fixFilePath(filename, baseDir, rootDir);
 
-                                       if(filename.exists())
+                                       if(filename.exists() && filename.isFile())
                                        {
                                                if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
                                                {
                                                        fileList << filename.canonicalFilePath();
                                                }
+                                               foundAtLeastOneFile = true;
                                                break;
                                        }
                                }
                        }
                }
+       }
+
+       //If we did not find any files yet, try UTF-16 now
+       if(!foundAtLeastOneFile)
+       {
+               const char* codecs[2] = {"UTF-16LE", "UTF-16BE"};
 
-               line = data.readLine();
+               for(size_t i = 0; i < 2; i++)
+               {
+                       QTextStream stream(&data);
+                       stream.setAutoDetectUnicode(false);
+                       stream.setCodec(codecs[i]);
+                       stream.seek(0i64);
+
+                       while(!stream.atEnd())
+                       {
+                               QString filePath = stream.readLine().trimmed();
+
+                               if(!filePath.contains(QChar(QChar::ReplacementCharacter)))
+                               {
+                                       if(plsEntry.indexIn(filePath) >= 0)
+                                       {
+                                               QFileInfo filename(QDir::fromNativeSeparators(plsEntry.cap(2)).trimmed());
+                                               filename.setCaching(false);
+                                               fixFilePath(filename, baseDir, rootDir);
+
+                                               if(filename.exists() && filename.isFile())
+                                               {
+                                                       if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
+                                                       {
+                                                               fileList << filename.canonicalFilePath();
+                                                       }
+                                                       foundAtLeastOneFile = true;
+                                               }
+                                       }
+                               }
+                       }
+
+                       if(foundAtLeastOneFile) break;
+               }
        }
 
        return true;
@@ -243,7 +335,7 @@ bool PlaylistImporter::parsePlaylist_wpl(QFile &data, QStringList &fileList, con
                filename.setCaching(false);
                fixFilePath(filename, baseDir, rootDir);
 
-               if(filename.exists())
+               if(filename.exists() && filename.isFile())
                {
                        if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
                        {