annotate data/fileio/CSVStreamWriter.h @ 1434:0684c6698e3f streaming-csv-writer

Added utility function for splitting a model selection into chunks and writing to a stream.
author Lucas Thompson <dev@lucas.im>
date Tue, 17 Apr 2018 10:03:49 +0100
parents b5283878cca2
children 09c2ba31a711
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@1430 7 This file copyright 2017 Lucas Thompson.
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@1430 16 #ifndef _CSV_STREAM_WRITER_H_
dev@1430 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@1434 27 namespace CSV
dev@1430 28 {
dev@1434 29 using Completed = bool;
dev@1430 30
dev@1434 31 template <class OutStream>
dev@1434 32 auto writeToStreamInChunks(
dev@1434 33 OutStream& oss,
dev@1434 34 const Model& model,
dev@1434 35 const Selection& extents,
dev@1434 36 ProgressReporter* reporter = nullptr,
dev@1434 37 QString delimiter = ",",
dev@1434 38 DataExportOptions options = DataExportDefaults,
dev@1434 39 const sv_frame_t blockSize = 16384
dev@1434 40 ) -> Completed
dev@1434 41 {
dev@1434 42 if (blockSize <= 0) return false;
dev@1434 43 sv_frame_t readPtr = extents.isEmpty() ?
dev@1434 44 model.getStartFrame() : extents.getStartFrame();
dev@1434 45 sv_frame_t endFrame = extents.isEmpty() ?
dev@1434 46 model.getEndFrame() : extents.getEndFrame();
dev@1434 47 int previousPercentagePoint = 0;
dev@1430 48
dev@1434 49 const auto wasCancelled = [&reporter]() {
dev@1434 50 return reporter && reporter->wasCancelled();
dev@1434 51 };
dev@1430 52
dev@1434 53 while (readPtr < endFrame) {
dev@1434 54 if (wasCancelled()) return false;
dev@1434 55
dev@1434 56 const auto start = readPtr;
dev@1434 57 const auto end = std::min(start + blockSize, endFrame);
dev@1434 58
dev@1434 59 oss << model.toDelimitedDataStringSubsetWithOptions(
dev@1434 60 delimiter,
dev@1434 61 options,
dev@1434 62 start,
dev@1434 63 end
dev@1434 64 ) << (end < endFrame ? "\n" : "");
dev@1434 65
dev@1434 66 const auto currentPercentage = 100 * end / endFrame;
dev@1434 67 const bool hasIncreased = currentPercentage > previousPercentagePoint;
dev@1434 68
dev@1434 69 if (hasIncreased) {
dev@1434 70 if (reporter) reporter->setProgress(currentPercentage);
dev@1434 71 previousPercentagePoint = currentPercentage;
dev@1434 72 }
dev@1434 73 readPtr = end;
dev@1434 74 }
dev@1434 75 return !wasCancelled(); // setProgress could process event loop
dev@1434 76 }
dev@1434 77
dev@1434 78 template <class OutStream>
dev@1434 79 auto writeToStreamInChunks(
dev@1434 80 OutStream& oss,
dev@1434 81 const Model& model,
dev@1434 82 ProgressReporter* reporter = nullptr,
dev@1434 83 QString delimiter = ",",
dev@1434 84 DataExportOptions options = DataExportDefaults,
dev@1434 85 const sv_frame_t blockSize = 16384
dev@1434 86 ) -> Completed
dev@1434 87 {
dev@1434 88 const Selection empty;
dev@1434 89 return CSV::writeToStreamInChunks(
dev@1434 90 oss,
dev@1434 91 model,
dev@1434 92 empty,
dev@1434 93 reporter,
dev@1434 94 delimiter,
dev@1434 95 options,
dev@1434 96 blockSize
dev@1434 97 );
dev@1434 98 }
dev@1434 99 } // namespace
dev@1434 100 #endif