开源软件名称:skystrife/cpptoml开源软件地址:https://github.com/skystrife/cpptoml开源编程语言:C++ 96.7%开源软件介绍:cpptomlA header-only library for parsing TOML configuration files. Targets: TOML v0.5.0 as of August 2018. This includes support for the new DateTime format, inline tables, multi-line basic and raw strings, digit separators, hexadecimal integers, octal integers, binary integers, and float special values. Alternatives:
Build StatusTest ResultsFrom the toml-test suite:
We also currently maintain (but hopefully not indefinitely!) a fork of the toml-test suite that adds tests for features and clarifications that have been added to the TOML spec more recently than toml-test has been updated. We pass every test there.
CompilationRequires a well conforming C++11 compiler. On OSX this means clang++ with libc++ and libc++abi (the default clang installed with XCode's command line tools is sufficient). On Linux, you should be able to use g++ >= 4.8.x, or clang++ with libc++ and libc++abi (if your package manager supplies this; most don't). Compiling the examples can be done with cmake:
Example UsageTo parse a configuration file from a file, you can do the following: auto config = cpptoml::parse_file("config.toml");
Obtaining Basic ValuesYou can find basic values like so: auto val = config->get_as<int64_t>("my-int");
// val is a cpptoml::option<int64_t>
if (val)
{
// *val is the integer value for the key "my-int"
}
else
{
// "my-int" either did not exist or was not an integer
} To simplify things, you can specify default a default value using the
auto baz = config->get_as<double>("baz").value_or(0.5);
// baz is now the double value for key "baz", if it exists, or 0.5 otherwise cpptoml has extended support for dates and times beyond the TOML v0.4.0 spec. Specifically, it supports
Here are the fields of the date/time objects in cpptoml:
There are convenience functions Nested TablesIf you want to look up things in nested tables, there are two ways of doing this. Suppose you have the following structure: [first-table]
key1 = 0.1
key2 = 1284
[first-table.inner]
key3 = "hello world" Here's an idiomatic way of obtaining all three keys' values: auto config = cpptoml::parse_file("config.toml");
auto key1 = config->get_qualified_as<double>("first-table.key1");
auto key2 = config->get_qualified_as<int>("first-table.key2");
auto key3 = config->get_qualified_as<std::string>("first-table.inner.key3"); (Note that, because the TOML spec allows for "." to occur in a table name, you won't always be able to do this for any nested key, but in practice you should be fine.) A slightly more verbose way of getting them would be to first obtain the individual tables, and then query those individual tables for their keys like so: auto config = cpptoml::parse_file("config.toml");
auto first = config->get_table("first-table");
auto key1 = first->get_as<double>("key1");
auto key2 = first->get_as<int>("key2");
auto inner = first->get_table("inner");
auto key3 = inner->get_as<std::string>("key3"); The function auto inner2 = config->get_table_qualified("first-table.inner"); Arrays of ValuesSuppose you had a configuration file like the following: arr = [1, 2, 3, 4, 5]
mixed-arr = [[1, 2, 3, 4, 5], ["hello", "world"], [0.1, 1.1, 2.1]] To obtain an array of values, you can do the following: auto config = cpptoml::parse_file("config.toml");
auto vals = config->get_array_of<int64_t>("arr");
// vals is a cpptoml::option<std::vector<int64_t>>
for (const auto& val : *vals)
{
// val is an int64_t
}
For nested arrays, it looks like the following: auto nested = config->get_array_of<cpptoml::array>("mixed-arr");
auto ints = (*nested)[0]->get_array_of<int64_t>();
// ints is a cpptoml::option<std::vector<int64_t>>
auto strings = (*nested)[1]->get_array_of<std::string>();
auto doubles = (*nested)[2]->get_array_of<double>(); There is also a Arrays of TablesSuppose you had a configuration file like the following: [[table-array]]
key1 = "hello"
[[table-array]]
key1 = "can you hear me" Arrays of tables are represented as a separate type in auto config = cpptoml::parse_file("config.toml");
auto tarr = config->get_table_array("table-array");
for (const auto& table : *tarr)
{
// *table is a cpptoml::table
auto key1 = table->get_as<std::string>("key1");
} More ExamplesYou can look at the files files
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论