Mercurial > hg > svcore
comparison data/fileio/CSVFileWriter.cpp @ 1453:4b496a258782
Merge from branch streaming-csv-writer
author | Chris Cannam |
---|---|
date | Tue, 17 Apr 2018 10:52:06 +0100 |
parents | 6e9615bde1f9 |
children | c546429d4c2f |
comparison
equal
deleted
inserted
replaced
1429:48e9f538e6e9 | 1453:4b496a258782 |
---|---|
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()) { |
91 // Write the whole in one go, as re-seeking for each block | |
92 // may be very costly otherwise | |
93 sv_frame_t startFrame, endFrame; | |
94 selection.getExtents(startFrame, endFrame); | |
95 blockSize = endFrame - startFrame; | |
96 } | |
102 | 97 |
103 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame()); | 98 bool completed = CSVStreamWriter::writeInChunks( |
104 out << m_model->toDelimitedDataStringSubsetWithOptions | 99 out, |
105 (m_delimiter, m_options, f0, f1); | 100 *m_model, |
106 } | 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 |