CSVFileWriter.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "CSVFileWriter.h"
17 #include "CSVStreamWriter.h"
18 
19 #include "model/Model.h"
22 #include "model/NoteModel.h"
23 #include "model/TextModel.h"
24 
25 #include "base/TempWriteFile.h"
26 #include "base/Exceptions.h"
27 #include "base/Selection.h"
28 
29 #include <QFile>
30 #include <QTextStream>
31 #include <exception>
32 
34  Model *model,
35  QString delimiter,
36  DataExportOptions options) :
37  m_path(path),
38  m_model(model),
39  m_error(""),
40  m_delimiter(delimiter),
41  m_options(options)
42 {
43 }
44 
46 {
47 }
48 
49 bool
51 {
52  return m_error == "";
53 }
54 
55 QString
57 {
58  return m_error;
59 }
60 
61 void
63 {
64  Selection all {
67  };
68  MultiSelection selections;
69  selections.addSelection(all);
70  writeSelection(selections);
71 }
72 
73 void
75 {
76  try {
77  TempWriteFile temp(m_path);
78 
79  QFile file(temp.getTemporaryFilename());
80  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
81  m_error = tr("Failed to open file %1 for writing")
82  .arg(temp.getTemporaryFilename());
83  return;
84  }
85 
86  QTextStream out(&file);
87 
92  << endl;
93  }
94 
95  sv_frame_t blockSize = 65536;
96 
97  if (m_model->isSparse()) {
98  // Write the whole in one go, as re-seeking for each block
99  // may be very costly otherwise
100  sv_frame_t startFrame, endFrame;
101  selection.getExtents(startFrame, endFrame);
102  blockSize = endFrame - startFrame;
103  }
104 
105  bool completed = CSVStreamWriter::writeInChunks(
106  out,
107  *m_model,
108  selection,
109  m_reporter,
110  m_delimiter,
111  m_options,
112  blockSize
113  );
114 
115  file.close();
116  if (completed) {
117  temp.moveToTarget();
118  }
119 
120  } catch (FileOperationFailed &f) {
121  m_error = f.what();
122  } catch (const std::exception &e) { // ProgressReporter could throw
123  m_error = e.what();
124  }
125 }
virtual QString getError() const
const char * what() const override
Definition: Exceptions.cpp:89
Model * m_model
Definition: CSVFileWriter.h:57
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
QString m_path
Definition: CSVFileWriter.h:56
A selection object simply represents a range in time, via start and end frame.
Definition: Selection.h:40
QString m_delimiter
Definition: CSVFileWriter.h:59
DataExportOptions m_options
Definition: CSVFileWriter.h:60
CSVFileWriter(QString path, Model *model, QString delimiter=",", DataExportOptions options=DataExportDefaults)
sv_frame_t getEndFrame() const
Return the audio frame at the end of the model, i.e.
Definition: Model.h:87
Write a header row before any data rows.
virtual QVector< QString > getStringExportHeaders(DataExportOptions options) const =0
Return a label for each column that would be written by toStringExportRows.
void addSelection(const Selection &selection)
Definition: Selection.cpp:124
QString m_error
Definition: CSVFileWriter.h:58
virtual void write()
virtual ~CSVFileWriter()
ProgressReporter * m_reporter
Definition: CSVFileWriter.h:61
virtual bool isOK() const
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:51
A class that manages the creation of a temporary file with a given prefix and the renaming of that fi...
Definition: TempWriteFile.h:26
virtual bool isSparse() const
Return true if this is a sparse model.
Definition: Model.h:149
void moveToTarget()
Rename the temporary file to the target filename.
virtual sv_frame_t getStartFrame() const =0
Return the first audio frame spanned by the model.
virtual void writeSelection(MultiSelection selection)
void getExtents(sv_frame_t &startFrame, sv_frame_t &endFrame) const
Definition: Selection.cpp:177
QString getTemporaryFilename()
Return the name of the temporary file.
static QString joinDelimited(QVector< QString > row, QString delimiter)
Join a vector of strings into a single string, with the delimiter as the joining string.
Definition: StringBits.cpp:172
bool writeInChunks(OutStream &oss, const Model &model, const MultiSelection &regions, ProgressReporter *reporter=nullptr, QString delimiter=",", DataExportOptions options=DataExportDefaults, const sv_frame_t blockSize=16384)
int DataExportOptions