comparison data/fileio/CSVFileReader.cpp @ 1009:e369dd281cf2

Provide reader from QIODevice
author Chris Cannam
date Fri, 14 Nov 2014 17:23:27 +0000
parents dc1695b90a58
children 920699b6989d
comparison
equal deleted inserted replaced
1008:d9e0e59a1581 1009:e369dd281cf2
35 #include <map> 35 #include <map>
36 36
37 CSVFileReader::CSVFileReader(QString path, CSVFormat format, 37 CSVFileReader::CSVFileReader(QString path, CSVFormat format,
38 int mainModelSampleRate) : 38 int mainModelSampleRate) :
39 m_format(format), 39 m_format(format),
40 m_file(0), 40 m_device(0),
41 m_ownDevice(true),
41 m_warnings(0), 42 m_warnings(0),
42 m_mainModelSampleRate(mainModelSampleRate) 43 m_mainModelSampleRate(mainModelSampleRate)
43 { 44 {
44 m_file = new QFile(path); 45 QFile *file = new QFile(path);
45 bool good = false; 46 bool good = false;
46 47
47 if (!m_file->exists()) { 48 if (!file->exists()) {
48 m_error = QFile::tr("File \"%1\" does not exist").arg(path); 49 m_error = QFile::tr("File \"%1\" does not exist").arg(path);
49 } else if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text)) { 50 } else if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
50 m_error = QFile::tr("Failed to open file \"%1\"").arg(path); 51 m_error = QFile::tr("Failed to open file \"%1\"").arg(path);
51 } else { 52 } else {
52 good = true; 53 good = true;
53 } 54 }
54 55
55 if (!good) { 56 if (good) {
56 delete m_file; 57 m_device = file;
57 m_file = 0; 58 } else {
58 } 59 delete file;
60 }
61 }
62
63 CSVFileReader::CSVFileReader(QIODevice *device, CSVFormat format,
64 int mainModelSampleRate) :
65 m_format(format),
66 m_device(device),
67 m_ownDevice(false),
68 m_warnings(0),
69 m_mainModelSampleRate(mainModelSampleRate)
70 {
59 } 71 }
60 72
61 CSVFileReader::~CSVFileReader() 73 CSVFileReader::~CSVFileReader()
62 { 74 {
63 SVDEBUG << "CSVFileReader::~CSVFileReader: file is " << m_file << endl; 75 SVDEBUG << "CSVFileReader::~CSVFileReader: device is " << m_device << endl;
64 76
65 if (m_file) { 77 if (m_device && m_ownDevice) {
66 SVDEBUG << "CSVFileReader::CSVFileReader: Closing file" << endl; 78 SVDEBUG << "CSVFileReader::CSVFileReader: Closing device" << endl;
67 m_file->close(); 79 m_device->close();
68 } 80 delete m_device;
69 delete m_file; 81 }
70 } 82 }
71 83
72 bool 84 bool
73 CSVFileReader::isOK() const 85 CSVFileReader::isOK() const
74 { 86 {
75 return (m_file != 0); 87 return (m_device != 0);
76 } 88 }
77 89
78 QString 90 QString
79 CSVFileReader::getError() const 91 CSVFileReader::getError() const
80 { 92 {
134 } 146 }
135 147
136 Model * 148 Model *
137 CSVFileReader::load() const 149 CSVFileReader::load() const
138 { 150 {
139 if (!m_file) return 0; 151 if (!m_device) return 0;
140 152
141 CSVFormat::ModelType modelType = m_format.getModelType(); 153 CSVFormat::ModelType modelType = m_format.getModelType();
142 CSVFormat::TimingType timingType = m_format.getTimingType(); 154 CSVFormat::TimingType timingType = m_format.getTimingType();
143 CSVFormat::TimeUnits timeUnits = m_format.getTimeUnits(); 155 CSVFormat::TimeUnits timeUnits = m_format.getTimeUnits();
144 int sampleRate = m_format.getSampleRate(); 156 int sampleRate = m_format.getSampleRate();
166 RegionModel *model2a = 0; 178 RegionModel *model2a = 0;
167 NoteModel *model2b = 0; 179 NoteModel *model2b = 0;
168 EditableDenseThreeDimensionalModel *model3 = 0; 180 EditableDenseThreeDimensionalModel *model3 = 0;
169 Model *model = 0; 181 Model *model = 0;
170 182
171 QTextStream in(m_file); 183 QTextStream in(m_device);
172 in.seek(0);
173 184
174 unsigned int warnings = 0, warnLimit = 10; 185 unsigned int warnings = 0, warnLimit = 10;
175 unsigned int lineno = 0; 186 unsigned int lineno = 0;
176 187
177 float min = 0.0, max = 0.0; 188 float min = 0.0, max = 0.0;
213 QStringList lines = chunk.split('\r', QString::SkipEmptyParts); 224 QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
214 225
215 for (int li = 0; li < lines.size(); ++li) { 226 for (int li = 0; li < lines.size(); ++li) {
216 227
217 QString line = lines[li]; 228 QString line = lines[li];
218 229
219 if (line.startsWith("#")) continue; 230 if (line.startsWith("#")) continue;
220 231
221 QStringList list = StringBits::split(line, separator, allowQuoting); 232 QStringList list = StringBits::split(line, separator, allowQuoting);
222 if (!model) { 233 if (!model) {
223 234