annotate data/fileio/CSVFileWriter.cpp @ 1762:110f853c3149 by-id

Deleted-model checks, & debug output
author Chris Cannam
date Mon, 15 Jul 2019 14:12:37 +0100
parents 6e9615bde1f9
children c546429d4c2f
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);
cannam@1452 70 writeSelection(selections);
Chris@148 71 }
Chris@148 72
Chris@839 73 void
cannam@1452 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
cannam@1452 88 sv_frame_t blockSize = 65536;
cannam@1452 89
cannam@1452 90 if (m_model->isSparse()) {
cannam@1452 91 // Write the whole in one go, as re-seeking for each block
cannam@1452 92 // may be very costly otherwise
cannam@1452 93 sv_frame_t startFrame, endFrame;
cannam@1452 94 selection.getExtents(startFrame, endFrame);
cannam@1452 95 blockSize = endFrame - startFrame;
cannam@1452 96 }
cannam@1452 97
dev@1445 98 bool completed = CSVStreamWriter::writeInChunks(
dev@1445 99 out,
dev@1445 100 *m_model,
cannam@1452 101 selection,
dev@1445 102 m_reporter,
dev@1445 103 m_delimiter,
cannam@1452 104 m_options,
cannam@1452 105 blockSize
dev@1442 106 );
Chris@839 107
Chris@839 108 file.close();
dev@1436 109 if (completed) {
dev@1436 110 temp.moveToTarget();
dev@1436 111 }
Chris@839 112
Chris@839 113 } catch (FileOperationFailed &f) {
Chris@839 114 m_error = f.what();
dev@1436 115 } catch (const std::exception &e) { // ProgressReporter could throw
dev@1436 116 m_error = e.what();
Chris@839 117 }
Chris@839 118 }