annotate data/fileio/CSVFileWriter.cpp @ 678:948271d124ac

Make more (though still not entirely) robust in the face of running out of disc space
author Chris Cannam
date Thu, 14 Apr 2011 15:20:27 +0100
parents 341e4e1a6ed3
children 38bb7c8e415d
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@675 26
Chris@148 27 #include <QFile>
Chris@148 28 #include <QTextStream>
Chris@148 29
Chris@148 30 CSVFileWriter::CSVFileWriter(QString path, Model *model, QString delimiter) :
Chris@148 31 m_path(path),
Chris@148 32 m_model(model),
Chris@148 33 m_error(""),
Chris@148 34 m_delimiter(delimiter)
Chris@148 35 {
Chris@148 36 }
Chris@148 37
Chris@148 38 CSVFileWriter::~CSVFileWriter()
Chris@148 39 {
Chris@148 40 }
Chris@148 41
Chris@148 42 bool
Chris@148 43 CSVFileWriter::isOK() const
Chris@148 44 {
Chris@148 45 return m_error == "";
Chris@148 46 }
Chris@148 47
Chris@148 48 QString
Chris@148 49 CSVFileWriter::getError() const
Chris@148 50 {
Chris@148 51 return m_error;
Chris@148 52 }
Chris@148 53
Chris@148 54 void
Chris@148 55 CSVFileWriter::write()
Chris@148 56 {
Chris@675 57 try {
Chris@675 58 TempWriteFile temp(m_path);
Chris@675 59
Chris@675 60 QFile file(temp.getTemporaryFilename());
Chris@675 61 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Chris@675 62 m_error = tr("Failed to open file %1 for writing")
Chris@675 63 .arg(temp.getTemporaryFilename());
Chris@675 64 return;
Chris@675 65 }
Chris@675 66
Chris@675 67 QTextStream out(&file);
Chris@675 68 out << m_model->toDelimitedDataString(m_delimiter);
Chris@675 69
Chris@675 70 file.close();
Chris@675 71 temp.moveToTarget();
Chris@675 72
Chris@675 73 } catch (FileOperationFailed &f) {
Chris@675 74 m_error = f.what();
Chris@148 75 }
Chris@148 76 }
Chris@148 77
Chris@148 78