From b0e1a516a96ccbabfde231f453cfb0ef2325cc8b Mon Sep 17 00:00:00 2001 From: John Maguire Date: Mon, 13 Sep 2010 16:31:42 +0000 Subject: [PATCH] Fix playlist parsing for iTunes M3U Fixes issue #736 This is really Apple's fault for bizarrely using \r for line endings. --- src/playlistparsers/m3uparser.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/playlistparsers/m3uparser.cpp b/src/playlistparsers/m3uparser.cpp index f8b5a76b6..cf8975918 100644 --- a/src/playlistparsers/m3uparser.cpp +++ b/src/playlistparsers/m3uparser.cpp @@ -16,6 +16,7 @@ #include "m3uparser.h" +#include #include M3UParser::M3UParser(QObject* parent) @@ -29,14 +30,22 @@ SongList M3UParser::Load(QIODevice* device, const QDir& dir) const { M3UType type = STANDARD; Metadata current_metadata; - QString line = QString::fromUtf8(device->readLine()).trimmed(); + QString data = QString::fromUtf8(device->readAll()); + data.replace('\r', '\n'); + QByteArray bytes = data.toUtf8(); + QBuffer buffer(&bytes); + buffer.open(QIODevice::ReadOnly); + + QString line = QString::fromUtf8(buffer.readLine()).trimmed(); + qDebug() << line; if (line.startsWith("#EXTM3U")) { // This is in extended M3U format. type = EXTENDED; - line = QString::fromUtf8(device->readLine()).trimmed(); + line = QString::fromUtf8(buffer.readLine()).trimmed(); } forever { + qDebug() << line; if (line.startsWith('#')) { // Extended info or comment. if (type == EXTENDED && line.startsWith("#EXT")) { @@ -62,10 +71,10 @@ SongList M3UParser::Load(QIODevice* device, const QDir& dir) const { current_metadata.length = -1; } } - if (device->atEnd()) { + if (buffer.atEnd()) { break; } - line = QString::fromUtf8(device->readLine()).trimmed(); + line = QString::fromUtf8(buffer.readLine()).trimmed(); } return ret;