Use new helper functions ParseAsArray and ParseAsPairs of InfoStringToStructConverterBase to parse notetracksoundmap and hidetags

This commit is contained in:
Jan
2021-03-26 12:12:32 +01:00
parent aaf350d088
commit 3520a9bd2c
3 changed files with 140 additions and 127 deletions

View File

@ -11,6 +11,95 @@ InfoStringToStructConverterBase::InfoStringToStructConverterBase(const InfoStrin
{
}
bool InfoStringToStructConverterBase::ParseAsArray(const std::string& value, std::vector<std::string>& valueArray)
{
auto startPos = 0u;
for (auto ci = 0u; ci < value.size(); ci++)
{
const auto c = value[ci];
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
{
valueArray.emplace_back(value, startPos, ci - startPos);
startPos = ++ci + 1;
}
else if(c == '\n')
{
valueArray.emplace_back(value, startPos, ci - startPos);
startPos = ci + 1;
}
}
if(startPos < value.size())
{
valueArray.emplace_back(value, startPos, value.size() - startPos);
}
return true;
}
bool InfoStringToStructConverterBase::ParseAsPairs(const std::string& value, std::vector<std::pair<std::string, std::string>>& valueArray) const
{
std::string key;
auto isKey = true;
for (auto ci = 0u; ci < value.size(); ci++)
{
auto c = value[ci];
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
c = value[++ci];
if (c == '\n' && !isKey)
{
std::cout << "Expected value but got new line" << std::endl;
return false;
}
if (isspace(c))
continue;
int separator;
const auto startPos = ci;
while (true)
{
ci++;
if (ci >= value.size())
{
separator = EOF;
break;
}
c = value[ci];
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
c = value[++ci];
if (isspace(c))
{
separator = static_cast<int>(static_cast<unsigned char>(c));
break;
}
}
if (isKey)
{
if (separator == '\n' || separator == EOF)
{
std::cout << "Expected value but got new line" << std::endl;
return false;
}
key = std::string(value, startPos, ci - startPos);
}
else
{
auto parsedValue = std::string(value, startPos, ci - startPos);
valueArray.emplace_back(std::make_pair(std::move(key), std::move(parsedValue)));
key = std::string();
}
isKey = !isKey;
}
return true;
}
bool InfoStringToStructConverterBase::ConvertString(const std::string& value, const size_t offset)
{
*reinterpret_cast<const char**>(reinterpret_cast<uintptr_t>(m_structure) + offset) = m_memory->Dup(value.c_str());

View File

@ -2,6 +2,7 @@
#include <string>
#include <unordered_set>
#include <utility>
#include "Utils/ClassUtils.h"
#include "InfoString/InfoString.h"
@ -18,6 +19,9 @@ protected:
std::unordered_set<XAssetInfoGeneric*> m_dependencies;
MemoryManager* m_memory;
void* m_structure;
static bool ParseAsArray(const std::string& value, std::vector<std::string>& valueArray);
bool ParseAsPairs(const std::string& value, std::vector<std::pair<std::string, std::string>>& valueArray) const;
bool ConvertString(const std::string& value, size_t offset);
bool ConvertStringBuffer(const std::string& value, size_t offset, size_t bufferSize);