mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-05-10 06:24:57 +00:00
chore: extend CsvStream to be able to emit const char* allocated with a MemoryManager
This commit is contained in:
parent
09f3313d76
commit
4696011d9d
@ -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;
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user