Mercurial > hg > svcore
changeset 1009:e369dd281cf2
Provide reader from QIODevice
author | Chris Cannam |
---|---|
date | Fri, 14 Nov 2014 17:23:27 +0000 |
parents | d9e0e59a1581 |
children | 36f79bc5c3d7 |
files | data/fileio/CSVFileReader.cpp data/fileio/CSVFileReader.h data/fileio/CSVFormat.h |
diffstat | 3 files changed, 46 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/data/fileio/CSVFileReader.cpp Fri Nov 14 13:51:33 2014 +0000 +++ b/data/fileio/CSVFileReader.cpp Fri Nov 14 17:23:27 2014 +0000 @@ -37,42 +37,54 @@ CSVFileReader::CSVFileReader(QString path, CSVFormat format, int mainModelSampleRate) : m_format(format), - m_file(0), + m_device(0), + m_ownDevice(true), m_warnings(0), m_mainModelSampleRate(mainModelSampleRate) { - m_file = new QFile(path); + QFile *file = new QFile(path); bool good = false; - if (!m_file->exists()) { + if (!file->exists()) { m_error = QFile::tr("File \"%1\" does not exist").arg(path); - } else if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text)) { + } else if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) { m_error = QFile::tr("Failed to open file \"%1\"").arg(path); } else { good = true; } - if (!good) { - delete m_file; - m_file = 0; + if (good) { + m_device = file; + } else { + delete file; } } +CSVFileReader::CSVFileReader(QIODevice *device, CSVFormat format, + int mainModelSampleRate) : + m_format(format), + m_device(device), + m_ownDevice(false), + m_warnings(0), + m_mainModelSampleRate(mainModelSampleRate) +{ +} + CSVFileReader::~CSVFileReader() { - SVDEBUG << "CSVFileReader::~CSVFileReader: file is " << m_file << endl; + SVDEBUG << "CSVFileReader::~CSVFileReader: device is " << m_device << endl; - if (m_file) { - SVDEBUG << "CSVFileReader::CSVFileReader: Closing file" << endl; - m_file->close(); + if (m_device && m_ownDevice) { + SVDEBUG << "CSVFileReader::CSVFileReader: Closing device" << endl; + m_device->close(); + delete m_device; } - delete m_file; } bool CSVFileReader::isOK() const { - return (m_file != 0); + return (m_device != 0); } QString @@ -136,7 +148,7 @@ Model * CSVFileReader::load() const { - if (!m_file) return 0; + if (!m_device) return 0; CSVFormat::ModelType modelType = m_format.getModelType(); CSVFormat::TimingType timingType = m_format.getTimingType(); @@ -168,8 +180,7 @@ EditableDenseThreeDimensionalModel *model3 = 0; Model *model = 0; - QTextStream in(m_file); - in.seek(0); + QTextStream in(m_device); unsigned int warnings = 0, warnLimit = 10; unsigned int lineno = 0; @@ -215,7 +226,7 @@ for (int li = 0; li < lines.size(); ++li) { QString line = lines[li]; - + if (line.startsWith("#")) continue; QStringList list = StringBits::split(line, separator, allowQuoting);
--- a/data/fileio/CSVFileReader.h Fri Nov 14 13:51:33 2014 +0000 +++ b/data/fileio/CSVFileReader.h Fri Nov 14 17:23:27 2014 +0000 @@ -28,16 +28,31 @@ class CSVFileReader : public DataFileReader { public: + /** + * Construct a CSVFileReader to read the CSV file at the given + * path, with the given format. + */ CSVFileReader(QString path, CSVFormat format, int mainModelSampleRate); + + /** + * Construct a CSVFileReader to read from the given + * QIODevice. Caller retains ownership of the QIODevice: the + * CSVFileReader will not close or delete it and it must outlive + * the CSVFileReader. + */ + CSVFileReader(QIODevice *device, CSVFormat format, int mainModelSampleRate); + virtual ~CSVFileReader(); virtual bool isOK() const; virtual QString getError() const; + virtual Model *load() const; protected: CSVFormat m_format; - QFile *m_file; + QIODevice *m_device; + bool m_ownDevice; QString m_error; mutable int m_warnings; int m_mainModelSampleRate;
--- a/data/fileio/CSVFormat.h Fri Nov 14 13:51:33 2014 +0000 +++ b/data/fileio/CSVFormat.h Fri Nov 14 17:23:27 2014 +0000 @@ -100,8 +100,8 @@ void setTimingType(TimingType t) { m_timingType = t; } void setTimeUnits(TimeUnits t) { m_timeUnits = t; } void setSeparator(QChar s) { m_separator = s; } - void setSampleRate(int r) { m_sampleRate = r; } - void setWindowSize(int s) { m_windowSize = s; } + void setSampleRate(int r) { m_sampleRate = r; } + void setWindowSize(int s) { m_windowSize = s; } void setColumnCount(int c) { m_columnCount = c; } void setAllowQuoting(bool q) { m_allowQuoting = q; }