chore: extend CsvStream to be able to emit const char* allocated with a MemoryManager

This commit is contained in:
Jan 2024-01-20 16:30:59 +01:00
parent 09f3313d76
commit 4696011d9d
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
2 changed files with 38 additions and 9 deletions

View File

@ -14,6 +14,27 @@ bool CsvInputStream::NextRow(std::vector<std::string>& out) const
if (!out.empty())
out.clear();
return EmitNextRow(
[&out](std::string value)
{
out.emplace_back(std::move(value));
});
}
bool CsvInputStream::NextRow(std::vector<const char*>& 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<void(std::string)>& cb) const
{
auto c = m_stream.get();
const auto isEof = c == EOF;
std::ostringstream col;
@ -21,7 +42,7 @@ bool CsvInputStream::NextRow(std::vector<std::string>& 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<std::string>& out) const
if (!isEof)
{
out.emplace_back(col.str());
cb(col.str());
}
return !isEof;

View File

@ -1,28 +1,36 @@
#pragma once
#include "Utils/MemoryManager.h"
#include <functional>
#include <iostream>
#include <string>
#include <vector>
class CsvInputStream
{
std::istream& m_stream;
public:
explicit CsvInputStream(std::istream& stream);
bool NextRow(std::vector<std::string>& out) const;
bool NextRow(std::vector<const char*>& out, MemoryManager& memory) const;
private:
bool EmitNextRow(const std::function<void(std::string)>& 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;
};