annotate data/fileio/PlaylistFileReader.cpp @ 362:cc4eb32efc6c

* Further model lifecycle fixes
author Chris Cannam
date Thu, 24 Jan 2008 11:03:59 +0000
parents c022976d18e8
children d030801113b6
rev   line source
Chris@297 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@297 2
Chris@297 3 /*
Chris@297 4 Sonic Visualiser
Chris@297 5 An audio file viewer and annotation editor.
Chris@297 6 Centre for Digital Music, Queen Mary, University of London.
Chris@297 7 This file copyright 2007 QMUL.
Chris@297 8
Chris@297 9 This program is free software; you can redistribute it and/or
Chris@297 10 modify it under the terms of the GNU General Public License as
Chris@297 11 published by the Free Software Foundation; either version 2 of the
Chris@297 12 License, or (at your option) any later version. See the file
Chris@297 13 COPYING included with this distribution for more information.
Chris@297 14 */
Chris@297 15
Chris@297 16 #include "PlaylistFileReader.h"
Chris@297 17
Chris@297 18 #include <QFile>
Chris@297 19 #include <QTextStream>
Chris@297 20 #include <QStringList>
Chris@297 21
Chris@297 22 PlaylistFileReader::PlaylistFileReader(QString path)
Chris@297 23 {
Chris@297 24 m_file = new QFile(path);
Chris@297 25 bool good = false;
Chris@297 26
Chris@297 27 if (!m_file->exists()) {
Chris@297 28 m_error = QFile::tr("File \"%1\" does not exist").arg(path);
Chris@297 29 } else if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@297 30 m_error = QFile::tr("Failed to open file \"%1\"").arg(path);
Chris@297 31 } else {
Chris@297 32 good = true;
Chris@297 33 }
Chris@297 34
Chris@297 35 if (!good) {
Chris@297 36 delete m_file;
Chris@297 37 m_file = 0;
Chris@297 38 }
Chris@297 39 }
Chris@297 40
Chris@297 41 PlaylistFileReader::~PlaylistFileReader()
Chris@297 42 {
Chris@297 43 if (m_file) m_file->close();
Chris@297 44 delete m_file;
Chris@297 45 }
Chris@297 46
Chris@297 47 bool
Chris@297 48 PlaylistFileReader::isOK() const
Chris@297 49 {
Chris@297 50 return (m_file != 0);
Chris@297 51 }
Chris@297 52
Chris@297 53 QString
Chris@297 54 PlaylistFileReader::getError() const
Chris@297 55 {
Chris@297 56 return m_error;
Chris@297 57 }
Chris@297 58
Chris@297 59 PlaylistFileReader::Playlist
Chris@297 60 PlaylistFileReader::load() const
Chris@297 61 {
Chris@297 62 if (!m_file) return Playlist();
Chris@297 63
Chris@297 64 QTextStream in(m_file);
Chris@297 65 in.seek(0);
Chris@297 66
Chris@297 67 Playlist playlist;
Chris@297 68
Chris@297 69 while (!in.atEnd()) {
Chris@297 70
Chris@297 71 // cope with old-style Mac line endings (c.f. CSVFileReader)
Chris@297 72 // as well as DOS/Unix style
Chris@297 73
Chris@297 74 QString chunk = in.readLine();
Chris@297 75 QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
Chris@297 76
Chris@297 77 for (size_t li = 0; li < lines.size(); ++li) {
Chris@297 78
Chris@297 79 QString line = lines[li];
Chris@297 80
Chris@297 81 if (line.startsWith("#")) continue;
Chris@297 82
Chris@297 83 playlist.push_back(line);
Chris@297 84 }
Chris@297 85 }
Chris@297 86
Chris@297 87 return playlist;
Chris@297 88 }
Chris@297 89
Chris@297 90 void
Chris@297 91 PlaylistFileReader::getSupportedExtensions(std::set<QString> &extensions)
Chris@297 92 {
Chris@297 93 extensions.insert("m3u");
Chris@297 94 }