2
// C++ Implementation: parserm3u
4
// Description: module to parse m3u(plaintext) formated playlists
7
// Author: Ingo Kossyk <kossyki@cs.tu-berlin.de>, (C) 2004
9
// Copyright: See COPYING file that comes with this distribution
12
#include <QTextStream>
14
#include "parserm3u.h"
18
@author Ingo Kossyk (kossyki@cs.tu-berlin.de)
23
- parse ALL informations from the pls file if available ,
24
not only the filepath;
27
The M3U format is just a headerless plaintext format
28
where every line of text either represents
29
a file location or a comment. comments are being
30
preceeded by a '#'. This parser will try to parse all
31
file information from the given file and add the filepaths
32
to the locations ptrlist when the file is existing locally
33
or on a mounted harddrive.
36
ParserM3u::ParserM3u() : Parser()
40
ParserM3u::~ParserM3u()
46
QList<QString> ParserM3u::parse(QString sFilename)
48
QFile file(sFilename);
49
QString basepath = sFilename.section('/', 0, -2);
52
//qDebug() << "ParserM3u: Starting to parse.";
53
if (file.open(QIODevice::ReadOnly) && !isBinary(sFilename)) {
54
/* Unfortunately, QT 4.7 does not handle <CR> line breaks.
55
* This is important on OS X where iTunes, e.g., exports M3U playlists using <CR>
57
* Using QFile::readAll() we obtain the complete content of the playlist as a ByteArray.
58
* We replace any '\r' with '\n' if applicaple
59
* This ensures that playlists from iTunes on OS X can be parsed
61
QByteArray ba = file.readAll();
62
ba.replace('\r',"\n");
63
QTextStream textstream(ba.data());
65
while(!textstream.atEnd()) {
66
QString sLine = getFilepath(&textstream, basepath);
70
//qDebug) << ("ParserM3u: parsed: " << (sLine);
71
m_sLocations.append(sLine);
76
if(m_sLocations.count() != 0)
79
return QList<QString>(); // NULL pointer returned when no locations were found
84
return QList<QString>(); //if we get here something went wrong
88
QString ParserM3u::getFilepath(QTextStream *stream, QString basepath)
90
QString textline,filename = "";
92
textline = stream->readLine();
94
while(!textline.isEmpty()){
98
if(!textline.contains("#") && !textline.isEmpty()){
100
filename.remove("file://");
101
QByteArray strlocbytes = filename.toUtf8();
102
QUrl location = QUrl::fromEncoded(strlocbytes);
103
QString trackLocation = location.toLocalFile();
104
//qDebug() << trackLocation;
105
if(isFilepath(trackLocation)) {
106
return trackLocation;
108
// Try relative to m3u dir
109
QString rel = basepath + "/" + trackLocation;
110
if (isFilepath(rel)) {
113
// We couldn't match this to a real file so ignore it
116
textline = stream->readLine();
119
// Signal we reached the end