annotate data/fileio/CSVStreamWriter.h @ 1441:51493540a753 streaming-csv-writer

Fix progress reporting and allow for changing how progress is calculating (TODO multi-selection)
author Lucas Thompson <dev@lucas.im>
date Tue, 17 Apr 2018 10:03:50 +0100
parents 09c2ba31a711
children 4507be815384
rev   line source
dev@1430 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
dev@1430 2
dev@1430 3 /*
dev@1430 4 Sonic Visualiser
dev@1430 5 An audio file viewer and annotation editor.
dev@1430 6 Centre for Digital Music, Queen Mary, University of London.
dev@1438 7 This file copyright 2017 Queen Mary, University of London.
dev@1430 8
dev@1430 9 This program is free software; you can redistribute it and/or
dev@1430 10 modify it under the terms of the GNU General Public License as
dev@1430 11 published by the Free Software Foundation; either version 2 of the
dev@1430 12 License, or (at your option) any later version. See the file
dev@1430 13 COPYING included with this distribution for more information.
dev@1430 14 */
dev@1430 15
dev@1438 16 #ifndef CSV_STREAM_WRITER_H
dev@1438 17 #define CSV_STREAM_WRITER_H
dev@1430 18
dev@1434 19 #include "base/BaseTypes.h"
dev@1434 20 #include "base/Selection.h"
dev@1434 21 #include "base/ProgressReporter.h"
dev@1434 22 #include "base/DataExportOptions.h"
dev@1434 23 #include "data/model/Model.h"
dev@1434 24 #include <QString>
dev@1434 25 #include <algorithm>
dev@1434 26
dev@1441 27 namespace
dev@1441 28 {
dev@1441 29 const auto initProgressCalculator = [](sv_frame_t nFramesToWrite) {
dev@1441 30 return [nFramesToWrite](sv_frame_t nFramesWritten) {
dev@1441 31 return 100 * nFramesWritten / nFramesToWrite;
dev@1441 32 };
dev@1441 33 };
dev@1441 34 } // namespace
dev@1441 35
dev@1438 36 namespace CSVStreamWriter
dev@1430 37 {
dev@1430 38
dev@1441 39 template <
dev@1441 40 class OutStream,
dev@1441 41 class ProgressCalculatorInit = decltype(initProgressCalculator)
dev@1441 42 >
dev@1441 43 bool
dev@1441 44 writeInChunks(OutStream& oss,
dev@1441 45 const Model& model,
dev@1441 46 const Selection& extents,
dev@1441 47 ProgressReporter* reporter = nullptr,
dev@1441 48 QString delimiter = ",",
dev@1441 49 DataExportOptions options = DataExportDefaults,
dev@1441 50 const sv_frame_t blockSize = 16384,
dev@1441 51 const ProgressCalculatorInit& initCalc = initProgressCalculator)
dev@1434 52 {
dev@1434 53 if (blockSize <= 0) return false;
dev@1441 54 const auto startFrame = extents.isEmpty() ?
dev@1434 55 model.getStartFrame() : extents.getStartFrame();
dev@1441 56 const auto endFrame = extents.isEmpty() ?
dev@1434 57 model.getEndFrame() : extents.getEndFrame();
dev@1441 58 const auto hasValidExtents = startFrame >= 0 && endFrame > startFrame;
dev@1441 59 if (!hasValidExtents) return false;
dev@1441 60 const auto calculateProgress = initCalc(endFrame - startFrame);
dev@1441 61
dev@1441 62 auto readPtr = startFrame;
dev@1434 63 int previousPercentagePoint = 0;
dev@1430 64
dev@1434 65 const auto wasCancelled = [&reporter]() {
dev@1434 66 return reporter && reporter->wasCancelled();
dev@1434 67 };
dev@1430 68
dev@1434 69 while (readPtr < endFrame) {
dev@1434 70 if (wasCancelled()) return false;
dev@1434 71
dev@1434 72 const auto start = readPtr;
dev@1434 73 const auto end = std::min(start + blockSize, endFrame);
dev@1434 74
dev@1434 75 oss << model.toDelimitedDataStringSubsetWithOptions(
dev@1434 76 delimiter,
dev@1434 77 options,
dev@1434 78 start,
dev@1434 79 end
dev@1434 80 ) << (end < endFrame ? "\n" : "");
dev@1441 81 const auto nFramesWritten = end - startFrame;
dev@1441 82 const auto currentPercentage = calculateProgress(nFramesWritten);
dev@1434 83 const bool hasIncreased = currentPercentage > previousPercentagePoint;
dev@1434 84
dev@1434 85 if (hasIncreased) {
dev@1434 86 if (reporter) reporter->setProgress(currentPercentage);
dev@1434 87 previousPercentagePoint = currentPercentage;
dev@1434 88 }
dev@1434 89 readPtr = end;
dev@1434 90 }
dev@1434 91 return !wasCancelled(); // setProgress could process event loop
dev@1434 92 }
dev@1434 93
dev@1434 94 template <class OutStream>
dev@1441 95 bool
dev@1441 96 writeInChunks(OutStream& oss,
dev@1441 97 const Model& model,
dev@1441 98 ProgressReporter* reporter = nullptr,
dev@1441 99 QString delimiter = ",",
dev@1441 100 DataExportOptions options = DataExportDefaults,
dev@1441 101 const sv_frame_t blockSize = 16384)
dev@1434 102 {
dev@1434 103 const Selection empty;
dev@1438 104 return CSV::writeInChunks(
dev@1434 105 oss,
dev@1434 106 model,
dev@1434 107 empty,
dev@1434 108 reporter,
dev@1434 109 delimiter,
dev@1434 110 options,
dev@1434 111 blockSize
dev@1434 112 );
dev@1434 113 }
dev@1438 114 } // namespace CSVStreamWriter
dev@1434 115 #endif