Skip to content

1.1 Config: Schema Parser

DK edited this page Mar 3, 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"

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 supports user defined type or varadic arguments:

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