annotate data/fileio/CSVFileWriter.cpp @ 1364:b812df0351d9 3.0-integration

Fix (I think) crash on exit on Windows having loaded an mp3
author Chris Cannam
date Thu, 12 Jan 2017 17:29:59 +0000
parents 815f82508f96
children 48e9f538e6e9 d61d6c33f14d
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"
Chris@148 17
Chris@150 18 #include "model/Model.h"
Chris@148 19 #include "model/SparseOneDimensionalModel.h"
Chris@148 20 #include "model/SparseTimeValueModel.h"
Chris@148 21 #include "model/NoteModel.h"
Chris@148 22 #include "model/TextModel.h"
Chris@148 23
Chris@675 24 #include "base/TempWriteFile.h"
Chris@675 25 #include "base/Exceptions.h"
Chris@839 26 #include "base/Selection.h"
Chris@675 27
Chris@148 28 #include <QFile>
Chris@148 29 #include <QTextStream>
Chris@148 30
Chris@1063 31 CSVFileWriter::CSVFileWriter(QString path,
Chris@1063 32 Model *model,
Chris@1063 33 QString delimiter,
Chris@1063 34 DataExportOptions options) :
Chris@148 35 m_path(path),
Chris@148 36 m_model(model),
Chris@148 37 m_error(""),
Chris@1063 38 m_delimiter(delimiter),
Chris@1063 39 m_options(options)
Chris@148 40 {
Chris@148 41 }
Chris@148 42
Chris@148 43 CSVFileWriter::~CSVFileWriter()
Chris@148 44 {
Chris@148 45 }
Chris@148 46
Chris@148 47 bool
Chris@148 48 CSVFileWriter::isOK() const
Chris@148 49 {
Chris@148 50 return m_error == "";
Chris@148 51 }
Chris@148 52
Chris@148 53 QString
Chris@148 54 CSVFileWriter::getError() const
Chris@148 55 {
Chris@148 56 return m_error;
Chris@148 57 }
Chris@148 58
Chris@148 59 void
Chris@148 60 CSVFileWriter::write()
Chris@148 61 {
Chris@675 62 try {
Chris@675 63 TempWriteFile temp(m_path);
Chris@675 64
Chris@675 65 QFile file(temp.getTemporaryFilename());
Chris@675 66 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Chris@675 67 m_error = tr("Failed to open file %1 for writing")
Chris@675 68 .arg(temp.getTemporaryFilename());
Chris@675 69 return;
Chris@675 70 }
Chris@1127 71
Chris@675 72 QTextStream out(&file);
Chris@1063 73 out << m_model->toDelimitedDataStringWithOptions
Chris@1063 74 (m_delimiter, m_options);
Chris@675 75
Chris@675 76 file.close();
Chris@675 77 temp.moveToTarget();
Chris@675 78
Chris@675 79 } catch (FileOperationFailed &f) {
Chris@675 80 m_error = f.what();
Chris@148 81 }
Chris@148 82 }
Chris@148 83
Chris@839 84 void
Chris@839 85 CSVFileWriter::writeSelection(MultiSelection *selection)
Chris@839 86 {
Chris@839 87 try {
Chris@839 88 TempWriteFile temp(m_path);
Chris@148 89
Chris@839 90 QFile file(temp.getTemporaryFilename());
Chris@839 91 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Chris@839 92 m_error = tr("Failed to open file %1 for writing")
Chris@839 93 .arg(temp.getTemporaryFilename());
Chris@839 94 return;
Chris@839 95 }
Chris@839 96
Chris@839 97 QTextStream out(&file);
Chris@839 98
Chris@839 99 for (MultiSelection::SelectionList::iterator i =
Chris@839 100 selection->getSelections().begin();
Chris@839 101 i != selection->getSelections().end(); ++i) {
Chris@839 102
Chris@1038 103 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame());
Chris@1063 104 out << m_model->toDelimitedDataStringSubsetWithOptions
Chris@1063 105 (m_delimiter, m_options, f0, f1);
Chris@839 106 }
Chris@839 107
Chris@839 108 file.close();
Chris@839 109 temp.moveToTarget();
Chris@839 110
Chris@839 111 } catch (FileOperationFailed &f) {
Chris@839 112 m_error = f.what();
Chris@839 113 }
Chris@839 114 }
Chris@839 115
Chris@839 116
Chris@839 117