annotate data/fileio/CSVFileWriter.cpp @ 1440:04caefd35391 streaming-csv-writer

Add failing test for non zero selection start
author Lucas Thompson <dev@lucas.im>
date Tue, 17 Apr 2018 10:03:50 +0100
parents 09c2ba31a711
children 3c3eedc2fad8
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@1436 88 bool completed = false;
dev@1436 89
dev@1436 90 for (const auto& bounds : selection->getSelections()) {
dev@1438 91 completed = CSVStreamWriter::writeInChunks(
dev@1436 92 out,
dev@1436 93 *m_model,
dev@1436 94 bounds,
dev@1436 95 m_reporter,
dev@1436 96 m_delimiter,
dev@1436 97 m_options
dev@1436 98 );
dev@1436 99 if (!completed) {
dev@1436 100 break;
dev@1436 101 }
Chris@839 102 }
Chris@839 103
Chris@839 104 file.close();
dev@1436 105 if (completed) {
dev@1436 106 temp.moveToTarget();
dev@1436 107 }
Chris@839 108
Chris@839 109 } catch (FileOperationFailed &f) {
Chris@839 110 m_error = f.what();
dev@1436 111 } catch (const std::exception &e) { // ProgressReporter could throw
dev@1436 112 m_error = e.what();
Chris@839 113 }
Chris@839 114 }