Skip to content

1.1 Config: Schema Parser

DK edited this page Mar 6, 2024 · 7 revisions

Custom formatted string

Schema parser allows you to parse custom formatted strings into c++ data structure.

Given a struct CustomData:

struct CustomData
{
    std::uint64_t form;
    std::string   name;
    std::string   payload;
    bool          excluded;
};

And expect schema string with delimiter |:

"0x12345|Random|None|true"

Special Values

spaces: all whitespaces for data type except std::string will be trimmed before parsing.

" 1000 " == "1000 " == "1000" 

bool: true/false is case insensitive, 1/0 is also accepted.

"True" == "true" == "1" == " true     "

hex: to guarantee a hex number conversion, regardless of prefix 0x in string segment, use dku::numbers::hex in place of std::uint64_t.

dku::numbers::hex formID;    // special hex number holder
formID = "0x14";             // 0x14
formID = "100";              // 0x100
formID = 100;                // 0x64
std::uint64_t num = formID;  // implicit conversion to numbers
fmt::format("{} {}", formID, num);   // formats to "0x64 100"

One liner

For the sake of ease of use, DKUtil::Config provides static function to parse strings as you go:

auto data = dku::Config::ParseSchemaString<CustomData>("0x12345|Random|None|true", "|");
INFO("{} {} {} {}", data.form, data.name, data.payload, data.excluded);

Proxy file

To parse entire schema file, that consists lines of schema strings:

auto schema = SCHEMA_PROXY("Schema_SC.txt");
schema.Load();
auto& parser = schema.get_parser();

std::vector<CustomData> data;
while (true) {
    auto entry = parser.ParseNextLine<CustomData>("|");
    if (!entry.has_value()) {
        break;
    }

    data.push_back(entry.value());
}

You can also utilize dku::Config::GetAllFiles<recursive>(...) to collect files at runtime.

Tuple/Struct conversion

All schema functions support user defined type and varadic arguments:

auto [a,b,c,d] = dku::Config::ParseSchemaString<int, std::string, std::string, bool>("0x12345|Random|None|true", "|");