svcore  1.9
DataFileReaderFactory.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "DataFileReaderFactory.h"
17 #include "MIDIFileReader.h"
18 #include "CSVFileReader.h"
19 
20 #include "model/Model.h"
21 
22 #include <QString>
23 
24 QString
26 {
27  return "*.svl *.csv *.lab *.mid *.txt";
28 }
29 
32  bool csv,
34  CSVFormat format,
35  int mainModelSampleRate)
36 {
37  QString err;
38 
39  DataFileReader *reader = 0;
40 
41  if (!csv) {
42  reader = new MIDIFileReader(path, acquirer, mainModelSampleRate);
43  if (reader->isOK()) return reader;
44  if (reader->getError() != "") err = reader->getError();
45  delete reader;
46  }
47 
48  if (csv) {
49  reader = new CSVFileReader(path, format, mainModelSampleRate);
50  if (reader->isOK()) return reader;
51  if (reader->getError() != "") err = reader->getError();
52  delete reader;
53  }
54 
55  return 0;
56 }
57 
61  int mainModelSampleRate)
62 {
64  (path, false, acquirer, CSVFormat(), mainModelSampleRate);
65  if (reader) return reader;
66 
67  reader = createReader
68  (path, true, acquirer, CSVFormat(path), mainModelSampleRate);
69  if (reader) return reader;
70 
71  return 0;
72 }
73 
74 Model *
77  int mainModelSampleRate)
78 {
79  DataFileReader *reader = createReader(path,
80  acquirer,
81  mainModelSampleRate);
82  if (!reader) return NULL;
83 
84  try {
85  Model *model = reader->load();
86  delete reader;
87  return model;
88  } catch (Exception) {
89  delete reader;
90  throw;
91  }
92 }
93 
94 Model *
97  int mainModelSampleRate)
98 {
99  DataFileReader *reader = createReader(path, false,
100  acquirer,
101  CSVFormat(),
102  mainModelSampleRate);
103  if (!reader) return NULL;
104 
105  try {
106  Model *model = reader->load();
107  delete reader;
108  return model;
109  } catch (Exception) {
110  delete reader;
111  throw;
112  }
113 }
114 
115 Model *
117  int mainModelSampleRate)
118 {
119  DataFileReader *reader = createReader(path, true, 0, format,
120  mainModelSampleRate);
121  if (!reader) return NULL;
122 
123  try {
124  Model *model = reader->load();
125  delete reader;
126  return model;
127  } catch (Exception) {
128  delete reader;
129  throw;
130  }
131 }
132 
static QString getKnownExtensions()
Return the file extensions that we have data file readers for, in a format suitable for use with QFil...
static Model * loadCSV(QString path, CSVFormat format, int mainModelSampleRate)
Read the given path using the CSV reader with the given format.
virtual bool isOK() const =0
Return true if the file appears to be of the correct type.
virtual Model * load() const =0
Read the file and return the corresponding data model.
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:35
virtual QString getError() const
static Model * load(QString path, MIDIFileImportPreferenceAcquirer *acquirer, int mainModelSampleRate)
Read the given path, if a suitable reader is available.
static Model * loadNonCSV(QString path, MIDIFileImportPreferenceAcquirer *acquirer, int mainModelSampleRate)
Read the given path, if a suitable reader is available.
static DataFileReader * createReader(QString path, MIDIFileImportPreferenceAcquirer *, int mainModelSampleRate)
Return a data file reader initialised to the file at the given path, or NULL if no suitable reader fo...