annotate data/fileio/CSVFileWriter.cpp @ 1450:a12fd0456f0c streaming-csv-writer

Merge from default branch
author Chris Cannam
date Tue, 17 Apr 2018 10:35:42 +0100
parents 4437209348ef 48e9f538e6e9
children 6e9615bde1f9
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 "CSVFileWriter.h"
dev@1436 17 #include "CSVStreamWriter.h"
Chris@148 18
Chris@150 19 #include "model/Model.h"
Chris@148 20 #include "model/SparseOneDimensionalModel.h"
Chris@148 21 #include "model/SparseTimeValueModel.h"
Chris@148 22 #include "model/NoteModel.h"
Chris@148 23 #include "model/TextModel.h"
Chris@148 24
Chris@675 25 #include "base/TempWriteFile.h"
Chris@675 26 #include "base/Exceptions.h"
Chris@839 27 #include "base/Selection.h"
Chris@675 28
Chris@148 29 #include <QFile>
Chris@148 30 #include <QTextStream>
dev@1436 31 #include <exception>
Chris@148 32
Chris@1063 33 CSVFileWriter::CSVFileWriter(QString path,
Chris@1063 34 Model *model,
Chris@1063 35 QString delimiter,
Chris@1063 36 DataExportOptions options) :
Chris@148 37 m_path(path),
Chris@148 38 m_model(model),
Chris@148 39 m_error(""),
Chris@1063 40 m_delimiter(delimiter),
Chris@1063 41 m_options(options)
Chris@148 42 {
Chris@148 43 }
Chris@148 44
Chris@148 45 CSVFileWriter::~CSVFileWriter()
Chris@148 46 {
Chris@148 47 }
Chris@148 48
Chris@148 49 bool
Chris@148 50 CSVFileWriter::isOK() const
Chris@148 51 {
Chris@148 52 return m_error == "";
Chris@148 53 }
Chris@148 54
Chris@148 55 QString
Chris@148 56 CSVFileWriter::getError() const
Chris@148 57 {
Chris@148 58 return m_error;
Chris@148 59 }
Chris@148 60
Chris@148 61 void
Chris@148 62 CSVFileWriter::write()
Chris@148 63 {
dev@1436 64 Selection all {
dev@1436 65 m_model->getStartFrame(),
dev@1436 66 m_model->getEndFrame()
dev@1436 67 };
dev@1436 68 MultiSelection selections;
dev@1436 69 selections.addSelection(all);
dev@1436 70 writeSelection(&selections);
Chris@148 71 }
Chris@148 72
Chris@839 73 void
Chris@839 74 CSVFileWriter::writeSelection(MultiSelection *selection)
Chris@839 75 {
Chris@839 76 try {
Chris@839 77 TempWriteFile temp(m_path);
Chris@148 78
Chris@839 79 QFile file(temp.getTemporaryFilename());
Chris@839 80 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Chris@839 81 m_error = tr("Failed to open file %1 for writing")
Chris@839 82 .arg(temp.getTemporaryFilename());
Chris@839 83 return;
Chris@839 84 }
Chris@839 85
Chris@839 86 QTextStream out(&file);
Chris@839 87
dev@1445 88 bool completed = CSVStreamWriter::writeInChunks(
dev@1445 89 out,
dev@1445 90 *m_model,
dev@1445 91 *selection,
dev@1445 92 m_reporter,
dev@1445 93 m_delimiter,
dev@1445 94 m_options
dev@1442 95 );
Chris@839 96
Chris@839 97 file.close();
dev@1436 98 if (completed) {
dev@1436 99 temp.moveToTarget();
dev@1436 100 }
Chris@839 101
Chris@839 102 } catch (FileOperationFailed &f) {
Chris@839 103 m_error = f.what();
dev@1436 104 } catch (const std::exception &e) { // ProgressReporter could throw
dev@1436 105 m_error = e.what();
Chris@839 106 }
Chris@839 107 }