2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-10-26 16:25:51 +00:00

refactor: refactor sound csv loading code

This commit is contained in:
Jan
2024-10-06 16:34:01 +02:00
parent a3b9d2693c
commit d814fe7b95
7 changed files with 1045 additions and 564 deletions

View File

@@ -1,14 +1,86 @@
#include "CsvStream.h"
#include <cstdlib>
#include <sstream>
constexpr char CSV_SEPARATOR = ',';
CsvCell::CsvCell(std::string value)
: m_value(std::move(value))
{
}
bool CsvCell::AsFloat(float& out) const
{
const char* startPtr = m_value.c_str();
char* endPtr;
out = std::strtof(startPtr, &endPtr);
if (endPtr == startPtr)
return false;
for (const auto* c = endPtr; *c; c++)
{
if (!isspace(*c))
return false;
}
return true;
}
bool CsvCell::AsInt32(int32_t& out) const
{
const char* startPtr = m_value.c_str();
char* endPtr;
out = std::strtol(startPtr, &endPtr, 0);
if (endPtr == startPtr)
return false;
for (const auto* c = endPtr; *c; c++)
{
if (!isspace(*c))
return false;
}
return true;
}
bool CsvCell::AsUInt32(uint32_t& out) const
{
const char* startPtr = m_value.c_str();
char* endPtr;
out = std::strtoul(startPtr, &endPtr, 0);
if (endPtr == startPtr)
return false;
for (const auto* c = endPtr; *c; c++)
{
if (!isspace(*c))
return false;
}
return true;
}
CsvInputStream::CsvInputStream(std::istream& stream)
: m_stream(stream)
{
}
bool CsvInputStream::NextRow(std::vector<CsvCell>& out) const
{
if (!out.empty())
out.clear();
return EmitNextRow(
[&out](std::string value)
{
out.emplace_back(std::move(value));
});
}
bool CsvInputStream::NextRow(std::vector<std::string>& out) const
{
if (!out.empty())