comparison data/fileio/CSVFileWriter.cpp @ 1527:710e6250a401 zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:14 +0100
parents 6e9615bde1f9
children c546429d4c2f
comparison
equal deleted inserted replaced
1324:d4a28d1479a8 1527:710e6250a401
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "CSVFileWriter.h" 16 #include "CSVFileWriter.h"
17 #include "CSVStreamWriter.h"
17 18
18 #include "model/Model.h" 19 #include "model/Model.h"
19 #include "model/SparseOneDimensionalModel.h" 20 #include "model/SparseOneDimensionalModel.h"
20 #include "model/SparseTimeValueModel.h" 21 #include "model/SparseTimeValueModel.h"
21 #include "model/NoteModel.h" 22 #include "model/NoteModel.h"
25 #include "base/Exceptions.h" 26 #include "base/Exceptions.h"
26 #include "base/Selection.h" 27 #include "base/Selection.h"
27 28
28 #include <QFile> 29 #include <QFile>
29 #include <QTextStream> 30 #include <QTextStream>
31 #include <exception>
30 32
31 CSVFileWriter::CSVFileWriter(QString path, 33 CSVFileWriter::CSVFileWriter(QString path,
32 Model *model, 34 Model *model,
33 QString delimiter, 35 QString delimiter,
34 DataExportOptions options) : 36 DataExportOptions options) :
57 } 59 }
58 60
59 void 61 void
60 CSVFileWriter::write() 62 CSVFileWriter::write()
61 { 63 {
62 try { 64 Selection all {
63 TempWriteFile temp(m_path); 65 m_model->getStartFrame(),
64 66 m_model->getEndFrame()
65 QFile file(temp.getTemporaryFilename()); 67 };
66 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { 68 MultiSelection selections;
67 m_error = tr("Failed to open file %1 for writing") 69 selections.addSelection(all);
68 .arg(temp.getTemporaryFilename()); 70 writeSelection(selections);
69 return;
70 }
71
72 QTextStream out(&file);
73 out << m_model->toDelimitedDataStringWithOptions
74 (m_delimiter, m_options);
75
76 file.close();
77 temp.moveToTarget();
78
79 } catch (FileOperationFailed &f) {
80 m_error = f.what();
81 }
82 } 71 }
83 72
84 void 73 void
85 CSVFileWriter::writeSelection(MultiSelection *selection) 74 CSVFileWriter::writeSelection(MultiSelection selection)
86 { 75 {
87 try { 76 try {
88 TempWriteFile temp(m_path); 77 TempWriteFile temp(m_path);
89 78
90 QFile file(temp.getTemporaryFilename()); 79 QFile file(temp.getTemporaryFilename());
94 return; 83 return;
95 } 84 }
96 85
97 QTextStream out(&file); 86 QTextStream out(&file);
98 87
99 for (MultiSelection::SelectionList::iterator i = 88 sv_frame_t blockSize = 65536;
100 selection->getSelections().begin(); 89
101 i != selection->getSelections().end(); ++i) { 90 if (m_model->isSparse()) {
102 91 // Write the whole in one go, as re-seeking for each block
103 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame()); 92 // may be very costly otherwise
104 out << m_model->toDelimitedDataStringSubsetWithOptions 93 sv_frame_t startFrame, endFrame;
105 (m_delimiter, m_options, f0, f1); 94 selection.getExtents(startFrame, endFrame);
95 blockSize = endFrame - startFrame;
106 } 96 }
97
98 bool completed = CSVStreamWriter::writeInChunks(
99 out,
100 *m_model,
101 selection,
102 m_reporter,
103 m_delimiter,
104 m_options,
105 blockSize
106 );
107 107
108 file.close(); 108 file.close();
109 temp.moveToTarget(); 109 if (completed) {
110 temp.moveToTarget();
111 }
110 112
111 } catch (FileOperationFailed &f) { 113 } catch (FileOperationFailed &f) {
112 m_error = f.what(); 114 m_error = f.what();
115 } catch (const std::exception &e) { // ProgressReporter could throw
116 m_error = e.what();
113 } 117 }
114 } 118 }
115
116
117