PlaylistFileReader.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2007 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "PlaylistFileReader.h"
00017 
00018 #include <QFile>
00019 #include <QTextStream>
00020 #include <QStringList>
00021 
00022 PlaylistFileReader::PlaylistFileReader(QString path)
00023 {
00024     m_file = new QFile(path);
00025     bool good = false;
00026 
00027     if (!m_file->exists()) {
00028         m_error = QFile::tr("File \"%1\" does not exist").arg(path);
00029     } else if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text)) {
00030         m_error = QFile::tr("Failed to open file \"%1\"").arg(path);
00031     } else {
00032         good = true;
00033     }
00034 
00035     if (!good) {
00036         delete m_file;
00037         m_file = 0;
00038     }
00039 }
00040 
00041 PlaylistFileReader::~PlaylistFileReader()
00042 {
00043     if (m_file) m_file->close();
00044     delete m_file;
00045 }
00046 
00047 bool
00048 PlaylistFileReader::isOK() const
00049 {
00050     return (m_file != 0);
00051 }
00052 
00053 QString
00054 PlaylistFileReader::getError() const
00055 {
00056     return m_error;
00057 }
00058 
00059 PlaylistFileReader::Playlist
00060 PlaylistFileReader::load() const
00061 {
00062     if (!m_file) return Playlist();
00063 
00064     QTextStream in(m_file);
00065     in.seek(0);
00066 
00067     Playlist playlist;
00068 
00069     while (!in.atEnd()) {
00070 
00071         // cope with old-style Mac line endings (c.f. CSVFileReader)
00072         // as well as DOS/Unix style
00073 
00074         QString chunk = in.readLine();
00075         QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
00076         
00077         for (size_t li = 0; li < lines.size(); ++li) {
00078 
00079             QString line = lines[li];
00080 
00081             if (line.startsWith("#")) continue;
00082 
00083             playlist.push_back(line);
00084         }
00085     }
00086 
00087     return playlist;
00088 }
00089 
00090 void
00091 PlaylistFileReader::getSupportedExtensions(std::set<QString> &extensions)
00092 {
00093     extensions.insert("m3u");
00094 }

Generated on Wed Feb 20 15:45:27 2008 for SonicVisualiser by  doxygen 1.5.1