reverb and duck loading. cleaned up asset loading with custom csv parser

This commit is contained in:
Alex
2024-01-24 22:01:47 -05:00
parent a93cb6f05d
commit acd9fa27fc
4 changed files with 349 additions and 182 deletions

View File

@ -0,0 +1,65 @@
#include "Csv/ParsedCsv.h"
ParsedCsvRow::ParsedCsvRow(std::unordered_map<std::string, size_t>& headers, std::vector<std::string>& row)
: headers(headers),
values(row)
{
}
const std::string& ParsedCsvRow::GetValue(const std::string& header, bool required) const
{
if (this->headers.find(header) == this->headers.end())
{
if (required)
std::cerr << "ERROR: Required column \"" << header << "\" was not found";
else
std::cerr << "WARNING: Expected column \"" << header << "\" was not found";
return nullptr;
}
auto& value = this->values.at(this->headers[header]);
if (required && value.empty())
{
std::cerr << "ERROR: Required column \"" << header << "\" does not have a value";
return nullptr;
}
return value;
}
ParsedCsv::ParsedCsv(const CsvInputStream& inputStream, bool hasHeaders)
{
std::vector<std::vector<std::string>> csvLines;
std::vector<std::string> currentLine;
while (inputStream.NextRow(currentLine))
{
csvLines.emplace_back(std::move(currentLine));
currentLine = std::vector<std::string>();
}
if (hasHeaders)
{
auto& headersRow = csvLines[0];
for (auto i = 0u; i < headersRow.size(); i++)
{
this->headers[headersRow[i]] = i;
}
}
for (auto i = hasHeaders ? 1u : 0u; i < csvLines.size(); i++)
{
auto& rowValues = csvLines[i];
this->rows.push_back(ParsedCsvRow(this->headers, rowValues));
}
}
size_t ParsedCsv::Size() const
{
return this->rows.size();
}
ParsedCsvRow ParsedCsv::operator[](size_t index) const
{
return this->rows.at(index);
}

View File

@ -0,0 +1,41 @@
#pragma once
#include <Csv/CsvStream.h>
#include <sstream>
#include <unordered_map>
class ParsedCsvRow
{
std::unordered_map<std::string, size_t>& headers;
std::vector<std::string> values;
public:
explicit ParsedCsvRow(std::unordered_map<std::string, size_t>& headers, std::vector<std::string>& row);
const std::string& GetValue(const std::string& header, bool required = false) const;
template<typename T> T GetValueAs(const std::string& header, bool required = false) const
{
const auto& value = this->GetValue(header, required);
if (!value.empty())
{
std::istringstream ss(value);
T out{};
ss >> out;
return out;
}
return {};
}
};
class ParsedCsv
{
std::unordered_map<std::string, size_t> headers;
std::vector<ParsedCsvRow> rows;
public:
explicit ParsedCsv(const CsvInputStream& inputStream, bool hasHeaders = true);
size_t Size() const;
ParsedCsvRow operator[](size_t index) const;
};