203 lines
5.6 KiB
C
203 lines
5.6 KiB
C
|
#pragma once
|
||
|
|
||
|
#include <Orange/Core/Variant.h>
|
||
|
#include <Orange/Core/Parse.h>
|
||
|
#include <Orange/Core/Span.h>
|
||
|
#include <Orange/Graphics/Color.h>
|
||
|
|
||
|
#include <unordered_map>
|
||
|
#include <cstdint>
|
||
|
#include <string>
|
||
|
#include <cstring>
|
||
|
#include <memory>
|
||
|
|
||
|
namespace orange::kv
|
||
|
{
|
||
|
namespace Types
|
||
|
{
|
||
|
enum KVVariantType
|
||
|
{
|
||
|
None,
|
||
|
String,
|
||
|
Int,
|
||
|
Float,
|
||
|
Ptr,
|
||
|
Color,
|
||
|
Uint64,
|
||
|
KeyValues,
|
||
|
|
||
|
Count,
|
||
|
};
|
||
|
}
|
||
|
using KeyValuesType = Types::KVVariantType;
|
||
|
|
||
|
class KeyValues;
|
||
|
|
||
|
class KeyValuesVariant
|
||
|
{
|
||
|
public:
|
||
|
using KeyValuesChild = std::unique_ptr<KeyValues>;
|
||
|
|
||
|
KeyValuesVariant()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
template <typename T>
|
||
|
KeyValuesVariant(const T& arg)
|
||
|
{
|
||
|
Set(arg);
|
||
|
}
|
||
|
|
||
|
template <typename T>
|
||
|
KeyValuesVariant(T&& arg)
|
||
|
{
|
||
|
Set(Move(arg));
|
||
|
}
|
||
|
|
||
|
~KeyValuesVariant()
|
||
|
{
|
||
|
Clear();
|
||
|
}
|
||
|
|
||
|
void Clear()
|
||
|
{
|
||
|
switch (m_type)
|
||
|
{
|
||
|
case Types::String:
|
||
|
m_data.Destruct<std::string>();
|
||
|
break;
|
||
|
case Types::KeyValues:
|
||
|
m_data.Destruct<KeyValuesChild>();
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
m_type = Types::None;
|
||
|
m_data.Clear();
|
||
|
}
|
||
|
|
||
|
std::string GetString() const
|
||
|
{
|
||
|
switch (m_type)
|
||
|
{
|
||
|
case Types::String:
|
||
|
return m_data.Get<std::string>();
|
||
|
default:
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Set(const std::string& val) { Clear(); m_type = Types::String; m_data.Construct<std::string>(val); }
|
||
|
void Set(StringView val) { Clear(); m_type = Types::String; m_data.Construct<std::string>(val.data, val.data + val.size); }
|
||
|
void Set(int32_t val) { Clear(); m_type = Types::Int; m_data.Get<int64_t>() = val; }
|
||
|
void Set(int64_t val) { Clear(); m_type = Types::Int; m_data.Get<int64_t>() = val; }
|
||
|
void Set(float val) { Clear(); m_type = Types::Float; m_data.Get<double>() = val; }
|
||
|
void Set(double val) { Clear(); m_type = Types::Float; m_data.Get<double>() = val; }
|
||
|
void Set(void* val) { Clear(); m_type = Types::Ptr; m_data.Get<void*>() = val; }
|
||
|
void Set(RGBAColor32 val) { Clear(); m_type = Types::Color; m_data.Construct<RGBAColor32>(val); }
|
||
|
void Set(uint32_t val) { Clear(); m_type = Types::Uint64; m_data.Get<uint64_t>() = val; }
|
||
|
void Set(uint64_t val) { Clear(); m_type = Types::Uint64; m_data.Get<uint64_t>() = val; }
|
||
|
void Set(KeyValuesChild val) { Clear(); m_type = Types::KeyValues; m_data.Get<KeyValuesChild>() = Move(val); }
|
||
|
private:
|
||
|
KeyValuesType m_type = Types::None;
|
||
|
|
||
|
Variant<
|
||
|
std::string,
|
||
|
int64_t,
|
||
|
double,
|
||
|
void*,
|
||
|
uint64_t,
|
||
|
RGBAColor32,
|
||
|
KeyValuesChild> m_data;
|
||
|
};
|
||
|
|
||
|
class KeyValues
|
||
|
{
|
||
|
public:
|
||
|
static std::unique_ptr<KeyValues> ParseFromUTF8(StringView buffer)
|
||
|
{
|
||
|
const char *start = buffer.begin();
|
||
|
const char *end = buffer.end();
|
||
|
return ParseChild(start, end);
|
||
|
}
|
||
|
private:
|
||
|
static std::unique_ptr<KeyValues> ParseChild(const char*& start, const char* end)
|
||
|
{
|
||
|
StringView range = StringView(start, end);
|
||
|
|
||
|
if (start == end)
|
||
|
return nullptr;
|
||
|
|
||
|
stream::ConsumeSpace(start, end);
|
||
|
|
||
|
if (*start != '{')
|
||
|
return nullptr;
|
||
|
|
||
|
auto kv = std::unique_ptr<KeyValues>();
|
||
|
|
||
|
bool inString = false;
|
||
|
|
||
|
bool fillingInValue = false;
|
||
|
std::string key;
|
||
|
std::string value;
|
||
|
|
||
|
while (start != end)
|
||
|
{
|
||
|
start++;
|
||
|
if (stream::IsStringToken(range, start))
|
||
|
inString = !inString;
|
||
|
|
||
|
if (!inString)
|
||
|
{
|
||
|
if (*start == '\r')
|
||
|
continue;
|
||
|
|
||
|
if (*start == '{')
|
||
|
{
|
||
|
auto child = ParseChild(start, end);
|
||
|
kv->m_children.emplace(key, Move(child));
|
||
|
|
||
|
fillingInValue = false;
|
||
|
key.clear();
|
||
|
value.clear();
|
||
|
}
|
||
|
|
||
|
if (*start == '}')
|
||
|
{
|
||
|
start++;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (*start == ' ' || *start == '\t' || *start == '\n')
|
||
|
{
|
||
|
if (!fillingInValue)
|
||
|
{
|
||
|
fillingInValue = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
kv->m_children.emplace(key, value);
|
||
|
|
||
|
fillingInValue = false;
|
||
|
key.clear();
|
||
|
value.clear();
|
||
|
}
|
||
|
|
||
|
stream::ConsumeSpace(start, end);
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (fillingInValue)
|
||
|
value += *start;
|
||
|
else
|
||
|
key += *start;
|
||
|
}
|
||
|
|
||
|
return kv;
|
||
|
}
|
||
|
|
||
|
std::unordered_multimap<std::string, KeyValuesVariant> m_children;
|
||
|
};
|
||
|
}
|