From 4696011d9ddb0bd2d00034c5f3a03acd22456f48 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 20 Jan 2024 16:30:59 +0100 Subject: [PATCH] chore: extend CsvStream to be able to emit const char* allocated with a MemoryManager --- src/ObjCommon/Csv/CsvStream.cpp | 25 +++++++++++++++++++++++-- src/ObjCommon/Csv/CsvStream.h | 22 +++++++++++++++------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/ObjCommon/Csv/CsvStream.cpp b/src/ObjCommon/Csv/CsvStream.cpp index 9acc1c76..cd01eed6 100644 --- a/src/ObjCommon/Csv/CsvStream.cpp +++ b/src/ObjCommon/Csv/CsvStream.cpp @@ -14,6 +14,27 @@ bool CsvInputStream::NextRow(std::vector& out) const if (!out.empty()) out.clear(); + return EmitNextRow( + [&out](std::string value) + { + out.emplace_back(std::move(value)); + }); +} + +bool CsvInputStream::NextRow(std::vector& out, MemoryManager& memory) const +{ + if (!out.empty()) + out.clear(); + + return EmitNextRow( + [&out, &memory](const std::string& value) + { + out.emplace_back(memory.Dup(value.c_str())); + }); +} + +bool CsvInputStream::EmitNextRow(const std::function& cb) const +{ auto c = m_stream.get(); const auto isEof = c == EOF; std::ostringstream col; @@ -21,7 +42,7 @@ bool CsvInputStream::NextRow(std::vector& out) const { if (c == CSV_SEPARATOR) { - out.emplace_back(col.str()); + cb(col.str()); col.clear(); col.str(std::string()); } @@ -46,7 +67,7 @@ bool CsvInputStream::NextRow(std::vector& out) const if (!isEof) { - out.emplace_back(col.str()); + cb(col.str()); } return !isEof; diff --git a/src/ObjCommon/Csv/CsvStream.h b/src/ObjCommon/Csv/CsvStream.h index 29bfb440..2524ec41 100644 --- a/src/ObjCommon/Csv/CsvStream.h +++ b/src/ObjCommon/Csv/CsvStream.h @@ -1,28 +1,36 @@ #pragma once +#include "Utils/MemoryManager.h" + +#include #include #include #include class CsvInputStream { - std::istream& m_stream; - public: explicit CsvInputStream(std::istream& stream); bool NextRow(std::vector& out) const; + bool NextRow(std::vector& out, MemoryManager& memory) const; + +private: + bool EmitNextRow(const std::function& cb) const; + + std::istream& m_stream; }; class CsvOutputStream { - std::ostream& m_stream; - unsigned m_column_count; - unsigned m_current_column; - bool m_first_row; - public: explicit CsvOutputStream(std::ostream& stream); void WriteColumn(const std::string& value); void NextRow(); + +private: + std::ostream& m_stream; + unsigned m_column_count; + unsigned m_current_column; + bool m_first_row; };