Merge branch 'Laupetin:main' into main

This commit is contained in:
Alex
2024-01-26 12:15:26 -05:00
committed by GitHub
52 changed files with 1838 additions and 359 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;
};