annotate data/fileio/CSVStreamWriter.h @ 1440:04caefd35391 streaming-csv-writer

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