Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@148: 
Chris@148: /*
Chris@148:     Sonic Visualiser
Chris@148:     An audio file viewer and annotation editor.
Chris@148:     Centre for Digital Music, Queen Mary, University of London.
Chris@148:     This file copyright 2006 Chris Cannam.
Chris@148:     
Chris@148:     This program is free software; you can redistribute it and/or
Chris@148:     modify it under the terms of the GNU General Public License as
Chris@148:     published by the Free Software Foundation; either version 2 of the
Chris@148:     License, or (at your option) any later version.  See the file
Chris@148:     COPYING included with this distribution for more information.
Chris@148: */
Chris@148: 
Chris@148: #include "DataFileReaderFactory.h"
Chris@148: #include "MIDIFileReader.h"
Chris@148: #include "CSVFileReader.h"
Chris@148: 
Chris@150: #include "model/Model.h"
Chris@148: 
Chris@148: #include <QString>
Chris@148: 
Chris@148: QString
Chris@148: DataFileReaderFactory::getKnownExtensions()
Chris@148: {
Chris@148:     return "*.svl *.csv *.lab *.mid *.txt";
Chris@148: }
Chris@148: 
Chris@148: DataFileReader *
Chris@392: DataFileReaderFactory::createReader(QString path,
Chris@392:                                     bool csv,
Chris@392:                                     MIDIFileImportPreferenceAcquirer *acquirer,
Chris@392:                                     CSVFormat format,
Chris@929:                                     int mainModelSampleRate)
Chris@148: {
Chris@148:     QString err;
Chris@148: 
Chris@148:     DataFileReader *reader = 0;
Chris@148: 
Chris@392:     if (!csv) {
Chris@392:         reader = new MIDIFileReader(path, acquirer, mainModelSampleRate);
Chris@392:         if (reader->isOK()) return reader;
Chris@392:         if (reader->getError() != "") err = reader->getError();
Chris@392:         delete reader;
Chris@392:     }
Chris@148: 
Chris@392:     if (csv) {
Chris@392:         reader = new CSVFileReader(path, format, mainModelSampleRate);
Chris@392:         if (reader->isOK()) return reader;
Chris@392:         if (reader->getError() != "") err = reader->getError();
Chris@392:         delete reader;
Chris@392:     }
Chris@392: 
Chris@392:     return 0;
Chris@392: }
Chris@392: 
Chris@392: DataFileReader *
Chris@392: DataFileReaderFactory::createReader(QString path,
Chris@392:                                     MIDIFileImportPreferenceAcquirer *acquirer,
Chris@929:                                     int mainModelSampleRate)
Chris@392: {
Chris@392:     DataFileReader *reader = createReader
Chris@392:         (path, false, acquirer, CSVFormat(), mainModelSampleRate);
Chris@392:     if (reader) return reader;
Chris@392: 
Chris@392:     reader = createReader
Chris@392:         (path, true, acquirer, CSVFormat(path), mainModelSampleRate);
Chris@392:     if (reader) return reader;
Chris@148: 
Chris@148:     return 0;
Chris@148: }
Chris@148: 
Chris@148: Model *
Chris@392: DataFileReaderFactory::load(QString path,
Chris@392:                             MIDIFileImportPreferenceAcquirer *acquirer,
Chris@929:                             int mainModelSampleRate)
Chris@148: {
Chris@392:     DataFileReader *reader = createReader(path,
Chris@392:                                           acquirer,
Chris@392:                                           mainModelSampleRate);
Chris@148:     if (!reader) return NULL;
Chris@148: 
Chris@308:     try {
Chris@308:         Model *model = reader->load();
Chris@308:         delete reader;
Chris@308:         return model;
Chris@308:     } catch (Exception) {
Chris@308:         delete reader;
Chris@308:         throw;
Chris@308:     }
Chris@148: }
Chris@148: 
Chris@392: Model *
Chris@392: DataFileReaderFactory::loadNonCSV(QString path,
Chris@392:                                   MIDIFileImportPreferenceAcquirer *acquirer,
Chris@929:                                   int mainModelSampleRate)
Chris@392: {
Chris@392:     DataFileReader *reader = createReader(path, false,
Chris@392:                                           acquirer,
Chris@392:                                           CSVFormat(),
Chris@392:                                           mainModelSampleRate);
Chris@392:     if (!reader) return NULL;
Chris@392: 
Chris@392:     try {
Chris@392:         Model *model = reader->load();
Chris@392:         delete reader;
Chris@392:         return model;
Chris@392:     } catch (Exception) {
Chris@392:         delete reader;
Chris@392:         throw;
Chris@392:     }
Chris@392: }
Chris@392: 
Chris@392: Model *
Chris@392: DataFileReaderFactory::loadCSV(QString path, CSVFormat format,
Chris@929:                                int mainModelSampleRate)
Chris@392: {
Chris@392:     DataFileReader *reader = createReader(path, true, 0, format,
Chris@392:                                           mainModelSampleRate);
Chris@392:     if (!reader) return NULL;
Chris@392: 
Chris@392:     try {
Chris@392:         Model *model = reader->load();
Chris@392:         delete reader;
Chris@392:         return model;
Chris@392:     } catch (Exception) {
Chris@392:         delete reader;
Chris@392:         throw;
Chris@392:     }
Chris@392: }
Chris@392: