Chris@297: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@297: Chris@297: /* Chris@297: Sonic Visualiser Chris@297: An audio file viewer and annotation editor. Chris@297: Centre for Digital Music, Queen Mary, University of London. Chris@297: This file copyright 2007 QMUL. Chris@297: Chris@297: This program is free software; you can redistribute it and/or Chris@297: modify it under the terms of the GNU General Public License as Chris@297: published by the Free Software Foundation; either version 2 of the Chris@297: License, or (at your option) any later version. See the file Chris@297: COPYING included with this distribution for more information. Chris@297: */ Chris@297: Chris@297: #include "PlaylistFileReader.h" Chris@297: Chris@297: #include Chris@297: #include Chris@297: #include Chris@297: Chris@297: PlaylistFileReader::PlaylistFileReader(QString path) Chris@297: { Chris@297: m_file = new QFile(path); Chris@297: bool good = false; Chris@297: Chris@297: if (!m_file->exists()) { Chris@297: m_error = QFile::tr("File \"%1\" does not exist").arg(path); Chris@297: } else if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text)) { Chris@297: m_error = QFile::tr("Failed to open file \"%1\"").arg(path); Chris@297: } else { Chris@297: good = true; Chris@297: } Chris@297: Chris@297: if (!good) { Chris@297: delete m_file; Chris@297: m_file = 0; Chris@297: } Chris@297: } Chris@297: Chris@297: PlaylistFileReader::~PlaylistFileReader() Chris@297: { Chris@297: if (m_file) m_file->close(); Chris@297: delete m_file; Chris@297: } Chris@297: Chris@297: bool Chris@297: PlaylistFileReader::isOK() const Chris@297: { Chris@297: return (m_file != 0); Chris@297: } Chris@297: Chris@297: QString Chris@297: PlaylistFileReader::getError() const Chris@297: { Chris@297: return m_error; Chris@297: } Chris@297: Chris@297: PlaylistFileReader::Playlist Chris@297: PlaylistFileReader::load() const Chris@297: { Chris@297: if (!m_file) return Playlist(); Chris@297: Chris@297: QTextStream in(m_file); Chris@297: in.seek(0); Chris@297: Chris@297: Playlist playlist; Chris@297: Chris@297: while (!in.atEnd()) { Chris@297: Chris@297: // cope with old-style Mac line endings (c.f. CSVFileReader) Chris@297: // as well as DOS/Unix style Chris@297: Chris@297: QString chunk = in.readLine(); Chris@297: QStringList lines = chunk.split('\r', QString::SkipEmptyParts); Chris@297: Chris@297: for (size_t li = 0; li < lines.size(); ++li) { Chris@297: Chris@297: QString line = lines[li]; Chris@297: Chris@297: if (line.startsWith("#")) continue; Chris@297: Chris@297: playlist.push_back(line); Chris@297: } Chris@297: } Chris@297: Chris@297: return playlist; Chris@297: } Chris@297: Chris@297: void Chris@297: PlaylistFileReader::getSupportedExtensions(std::set &extensions) Chris@297: { Chris@297: extensions.insert("m3u"); Chris@297: }