annotate data/fileio/DataFileReaderFactory.cpp @ 1833:21c792334c2e sensible-delimited-data-strings

Rewrite all the DelimitedDataString stuff so as to return vectors of individual cell strings rather than having the classes add the delimiters themselves. Rename accordingly to names based on StringExport. Take advantage of this in the CSV writer code so as to properly quote cells that contain delimiter characters.
author Chris Cannam
date Fri, 03 Apr 2020 17:11:05 +0100
parents 70e172e6cc59
children
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@148 7 This file copyright 2006 Chris Cannam.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #include "DataFileReaderFactory.h"
Chris@148 17 #include "MIDIFileReader.h"
Chris@148 18 #include "CSVFileReader.h"
Chris@148 19
Chris@150 20 #include "model/Model.h"
Chris@148 21
Chris@148 22 #include <QString>
Chris@148 23
Chris@148 24 QString
Chris@148 25 DataFileReaderFactory::getKnownExtensions()
Chris@148 26 {
Chris@148 27 return "*.svl *.csv *.lab *.mid *.txt";
Chris@148 28 }
Chris@148 29
Chris@148 30 DataFileReader *
Chris@392 31 DataFileReaderFactory::createReader(QString path,
Chris@392 32 bool csv,
Chris@392 33 MIDIFileImportPreferenceAcquirer *acquirer,
Chris@392 34 CSVFormat format,
Chris@1491 35 sv_samplerate_t mainModelSampleRate,
Chris@1491 36 ProgressReporter *reporter)
Chris@148 37 {
Chris@148 38 QString err;
Chris@148 39
Chris@1582 40 DataFileReader *reader = nullptr;
Chris@148 41
Chris@392 42 if (!csv) {
Chris@1491 43 reader = new MIDIFileReader(path,
Chris@1491 44 acquirer,
Chris@1491 45 mainModelSampleRate,
Chris@1491 46 reporter);
Chris@392 47 if (reader->isOK()) return reader;
Chris@392 48 if (reader->getError() != "") err = reader->getError();
Chris@392 49 delete reader;
Chris@392 50 }
Chris@148 51
Chris@392 52 if (csv) {
Chris@1491 53 reader = new CSVFileReader(path,
Chris@1491 54 format,
Chris@1491 55 mainModelSampleRate,
Chris@1491 56 reporter);
Chris@392 57 if (reader->isOK()) return reader;
Chris@392 58 if (reader->getError() != "") err = reader->getError();
Chris@392 59 delete reader;
Chris@392 60 }
Chris@392 61
Chris@1582 62 return nullptr;
Chris@392 63 }
Chris@392 64
Chris@392 65 DataFileReader *
Chris@392 66 DataFileReaderFactory::createReader(QString path,
Chris@392 67 MIDIFileImportPreferenceAcquirer *acquirer,
Chris@1491 68 sv_samplerate_t mainModelSampleRate,
Chris@1491 69 ProgressReporter *reporter)
Chris@392 70 {
Chris@392 71 DataFileReader *reader = createReader
Chris@1491 72 (path, false, acquirer, CSVFormat(), mainModelSampleRate, reporter);
Chris@392 73 if (reader) return reader;
Chris@392 74
Chris@392 75 reader = createReader
Chris@1491 76 (path, true, acquirer, CSVFormat(path), mainModelSampleRate, reporter);
Chris@392 77 if (reader) return reader;
Chris@148 78
Chris@1582 79 return nullptr;
Chris@148 80 }
Chris@148 81
Chris@148 82 Model *
Chris@392 83 DataFileReaderFactory::load(QString path,
Chris@392 84 MIDIFileImportPreferenceAcquirer *acquirer,
Chris@1491 85 sv_samplerate_t mainModelSampleRate,
Chris@1491 86 ProgressReporter *reporter)
Chris@148 87 {
Chris@392 88 DataFileReader *reader = createReader(path,
Chris@392 89 acquirer,
Chris@1491 90 mainModelSampleRate,
Chris@1491 91 reporter);
Chris@1582 92 if (!reader) return nullptr;
Chris@148 93
Chris@308 94 try {
Chris@308 95 Model *model = reader->load();
Chris@308 96 delete reader;
Chris@308 97 return model;
Chris@308 98 } catch (Exception) {
Chris@308 99 delete reader;
Chris@308 100 throw;
Chris@308 101 }
Chris@148 102 }
Chris@148 103
Chris@392 104 Model *
Chris@392 105 DataFileReaderFactory::loadNonCSV(QString path,
Chris@392 106 MIDIFileImportPreferenceAcquirer *acquirer,
Chris@1491 107 sv_samplerate_t mainModelSampleRate,
Chris@1491 108 ProgressReporter *reporter)
Chris@392 109 {
Chris@392 110 DataFileReader *reader = createReader(path, false,
Chris@392 111 acquirer,
Chris@392 112 CSVFormat(),
Chris@1491 113 mainModelSampleRate,
Chris@1491 114 reporter);
Chris@1582 115 if (!reader) return nullptr;
Chris@392 116
Chris@392 117 try {
Chris@392 118 Model *model = reader->load();
Chris@392 119 delete reader;
Chris@392 120 return model;
Chris@392 121 } catch (Exception) {
Chris@392 122 delete reader;
Chris@392 123 throw;
Chris@392 124 }
Chris@392 125 }
Chris@392 126
Chris@392 127 Model *
Chris@392 128 DataFileReaderFactory::loadCSV(QString path, CSVFormat format,
Chris@1491 129 sv_samplerate_t mainModelSampleRate,
Chris@1491 130 ProgressReporter *reporter)
Chris@392 131 {
Chris@1582 132 DataFileReader *reader = createReader(path, true, nullptr, format,
Chris@1491 133 mainModelSampleRate,
Chris@1491 134 reporter);
Chris@1582 135 if (!reader) return nullptr;
Chris@392 136
Chris@392 137 try {
Chris@392 138 Model *model = reader->load();
Chris@392 139 delete reader;
Chris@392 140 return model;
Chris@392 141 } catch (Exception) {
Chris@392 142 delete reader;
Chris@392 143 throw;
Chris@392 144 }
Chris@392 145 }
Chris@392 146