# HG changeset patch # User Lucas Thompson # Date 1523955829 -3600 # Node ID d61d6c33f14d8bafe61815fa95d06a2479f1b31c # Parent 365876627fb0d0328d43e5910c1fbc7add9af1e5 Remove some duplication and make use of utility function for writing in chunks to avoid memory growth. diff -r 365876627fb0 -r d61d6c33f14d data/fileio/CSVFileWriter.cpp --- a/data/fileio/CSVFileWriter.cpp Tue Apr 17 10:03:49 2018 +0100 +++ b/data/fileio/CSVFileWriter.cpp Tue Apr 17 10:03:49 2018 +0100 @@ -14,6 +14,7 @@ */ #include "CSVFileWriter.h" +#include "CSVStreamWriter.h" #include "model/Model.h" #include "model/SparseOneDimensionalModel.h" @@ -27,6 +28,7 @@ #include #include +#include CSVFileWriter::CSVFileWriter(QString path, Model *model, @@ -59,26 +61,13 @@ void CSVFileWriter::write() { - try { - TempWriteFile temp(m_path); - - QFile file(temp.getTemporaryFilename()); - if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { - m_error = tr("Failed to open file %1 for writing") - .arg(temp.getTemporaryFilename()); - return; - } - - QTextStream out(&file); - out << m_model->toDelimitedDataStringWithOptions - (m_delimiter, m_options); - - file.close(); - temp.moveToTarget(); - - } catch (FileOperationFailed &f) { - m_error = f.what(); - } + Selection all { + m_model->getStartFrame(), + m_model->getEndFrame() + }; + MultiSelection selections; + selections.addSelection(all); + writeSelection(&selections); } void @@ -96,22 +85,30 @@ QTextStream out(&file); - for (MultiSelection::SelectionList::iterator i = - selection->getSelections().begin(); - i != selection->getSelections().end(); ++i) { - - sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame()); - out << m_model->toDelimitedDataStringSubsetWithOptions - (m_delimiter, m_options, f0, f1); + bool completed = false; + + for (const auto& bounds : selection->getSelections()) { + completed = CSV::writeToStreamInChunks( + out, + *m_model, + bounds, + m_reporter, + m_delimiter, + m_options + ); + if (!completed) { + break; + } } file.close(); - temp.moveToTarget(); + if (completed) { + temp.moveToTarget(); + } } catch (FileOperationFailed &f) { m_error = f.what(); + } catch (const std::exception &e) { // ProgressReporter could throw + m_error = e.what(); } } - - -