fix main page code formatting

This commit is contained in:
Mark Gillard 2023-10-13 22:12:18 +03:00
parent 30172438ce
commit f1a38d23b7
2 changed files with 107 additions and 111 deletions

View File

@ -10,7 +10,7 @@ trim_trailing_whitespace = true
charset = utf-8 charset = utf-8
max_line_length = 120 max_line_length = 120
[*.{gitattributes,yaml,yml,vcxproj,vcxproj.filters,sln,rc,clang-format,toml,py,cmake}] [*.{gitattributes,yaml,yml,vcxproj,vcxproj.filters,sln,rc,clang-format,toml,py,cmake,md,markdown}]
indent_style = space indent_style = space
[{Doxyfile,Doxyfile-mcss,CMakeLists.txt}] [{Doxyfile,Doxyfile-mcss,CMakeLists.txt}]

View File

@ -40,20 +40,19 @@ Call toml::parse_file() and work with the toml::table you get back, or handle an
int main(int argc, char\*\* argv) int main(int argc, char\*\* argv)
{ {
toml::table tbl; toml::table tbl;
try try
{ {
tbl = toml::parse_file(argv[1]); tbl = toml::parse_file(argv[1]);
std::cout << tbl << "\n"; std::cout << tbl << "\n";
} }
catch (const toml::parse_error& err) catch (const toml::parse_error& err)
{ {
std::cerr << "Parsing failed:\n" << err << "\n"; std::cerr << "Parsing failed:\n" << err << "\n";
return 1; return 1;
} }
return 0; return 0;
} }
@endcpp @endcpp
@ -80,34 +79,33 @@ using namespace std::string_view_literals;
int main() int main()
{ {
static constexpr std::string_view some_toml = R"( static constexpr std::string_view some_toml = R"(
[library] [library]
name = "toml++" name = "toml++"
authors = ["Mark Gillard <mark.gillard@outlook.com.au>"] authors = ["Mark Gillard <mark.gillard@outlook.com.au>"]
cpp = 17 cpp = 17
)"sv; )"sv;
try try
{ {
// parse directly from a string view:
{
toml::table tbl = toml::parse(some_toml);
std::cout << tbl << "\n";
}
// parse directly from a string view: // parse from a string stream:
{ {
toml::table tbl = toml::parse(some_toml); std::stringstream ss{ std::string{ some_toml } };
std::cout << tbl << "\n"; toml::table tbl = toml::parse(ss);
} std::cout << tbl << "\n";
}
// parse from a string stream: }
{ catch (const toml::parse_error& err)
std::stringstream ss{ std::string{ some_toml } }; {
toml::table tbl = toml::parse(ss); std::cerr << "Parsing failed:\n" << err << "\n";
std::cout << tbl << "\n"; return 1;
} }
}
catch (const toml::parse_error& err)
{
std::cerr << "Parsing failed:\n" << err << "\n";
return 1;
}
return 0; return 0;
@ -148,16 +146,16 @@ the parsing functions return a toml::parse_result instead of a toml::table:
int main() int main()
{ {
toml::parse_result result = toml::parse_file("configuration.toml"); toml::parse_result result = toml::parse_file("configuration.toml");
if (!result)
{ if (!result)
std::cerr << "Parsing failed:\n" << result.error() << "\n"; {
return 1; std::cerr << "Parsing failed:\n" << result.error() << "\n";
} return 1;
}
do_stuff_with_your_config(std::move(result).table()); // 'steal' the table from the result do_stuff_with_your_config(std::move(result).table()); // 'steal' the table from the result
return 0; return 0;
} }
@endcpp @endcpp
@ -179,15 +177,15 @@ and description() members:
toml::table tbl; toml::table tbl;
try try
{ {
tbl = toml::parse_file("configuration.toml"); tbl = toml::parse_file("configuration.toml");
} }
catch (const toml::parse_error& err) catch (const toml::parse_error& err)
{ {
std::cerr std::cerr
<< "Error parsing file '" << \*err.source().path << "Error parsing file '" << \*err.source().path
<< "':\n" << err.description() << "':\n" << err.description()
<< "\n (" << err.source().begin << ")\n"; << "\n (" << err.source().begin << ")\n";
return 1; return 1;
} }
@endcpp @endcpp
@ -214,18 +212,17 @@ using namespace std::string_view_literals;
int main() int main()
{ {
static constexpr auto source = R"( static constexpr auto source = R"(
str = "hello world" str = "hello world"
numbers = [ 1, 2, 3, "four", 5.0 ] numbers = [ 1, 2, 3, "four", 5.0 ]
vegetables = [ "tomato", "onion", "mushroom", "lettuce" ] vegetables = [ "tomato", "onion", "mushroom", "lettuce" ]
minerals = [ "quartz", "iron", "copper", "diamond" ] minerals = [ "quartz", "iron", "copper", "diamond" ]
[animals]
cats = [ "tiger", "lion", "puma" ]
birds = [ "macaw", "pigeon", "canary" ]
fish = [ "salmon", "trout", "carp" ]
[animals]
cats = [ "tiger", "lion", "puma" ]
birds = [ "macaw", "pigeon", "canary" ]
fish = [ "salmon", "trout", "carp" ]
)"sv; )"sv;
toml::table tbl = toml::parse(source); toml::table tbl = toml::parse(source);
@ -249,20 +246,20 @@ fish = [ "salmon", "trout", "carp" ]
// get the underlying array object to do some more advanced stuff // get the underlying array object to do some more advanced stuff
if (toml::array* arr = numbers.as_array()) if (toml::array* arr = numbers.as_array())
{ {
// visitation with for_each() helps deal with heterogeneous data // visitation with for_each() helps deal with heterogeneous data
arr->for_each([](auto&& el) arr->for_each([](auto&& el)
{ {
if constexpr (toml::is_number<decltype(el)>) if constexpr (toml::is_number<decltype(el)>)
(*el)++; (*el)++;
else if constexpr (toml::is_string<decltype(el)>) else if constexpr (toml::is_string<decltype(el)>)
el = "five"sv; el = "five"sv;
}); });
// arrays are very similar to std::vector // arrays are very similar to std::vector
arr->push_back(7); arr->push_back(7);
arr->emplace_back<toml::array>(8, 9); arr->emplace_back<toml::array>(8, 9);
std::cout << "numbers: " << numbers << "\n"; std::cout << "numbers: " << numbers << "\n";
} }
// node-views can be chained to quickly query deeper // node-views can be chained to quickly query deeper
std::cout << "cats: " << tbl["animals"]["cats"] << "\n"; std::cout << "cats: " << tbl["animals"]["cats"] << "\n";
@ -318,18 +315,18 @@ but via a toml::json_formatter and toml::yaml_formatter.
int main() int main()
{ {
auto tbl = toml::table{ auto tbl = toml::table{
{ "lib", "toml++" }, { "lib", "toml++" },
{ "cpp", toml::array{ 17, 20, "and beyond" } }, { "cpp", toml::array{ 17, 20, "and beyond" } },
{ "toml", toml::array{ "1.0.0", "and beyond" } }, { "toml", toml::array{ "1.0.0", "and beyond" } },
{ "repo", "https://github.com/marzer/tomlplusplus/" }, { "repo", "https://github.com/marzer/tomlplusplus/" },
{ "author", toml::table{ { "author", toml::table{
{ "name", "Mark Gillard" }, { "name", "Mark Gillard" },
{ "github", "https://github.com/marzer" }, { "github", "https://github.com/marzer" },
{ "twitter", "https://twitter.com/marzer8789" } { "twitter", "https://twitter.com/marzer8789" }
} }
}, },
}; };
// serializing as TOML // serializing as TOML
std::cout << "###### TOML ######" << "\n\n"; std::cout << "###### TOML ######" << "\n\n";
@ -365,22 +362,22 @@ twitter = 'https://twitter.com/marzer8789'
###### JSON ###### JSON
{ {
"author" : { "author" : {
"github" : "https://github.com/marzer", "github" : "https://github.com/marzer",
"name" : "Mark Gillard", "name" : "Mark Gillard",
"twitter" : "https://twitter.com/marzer8789" "twitter" : "https://twitter.com/marzer8789"
}, },
"cpp" : [ "cpp" : [
17, 17,
20, 20,
"and beyond" "and beyond"
], ],
"lib" : "toml++", "lib" : "toml++",
"repo" : "https://github.com/marzer/tomlplusplus/", "repo" : "https://github.com/marzer/tomlplusplus/",
"toml" : [ "toml" : [
"1.0.0", "1.0.0",
"and beyond" "and beyond"
] ]
} }
###### YAML ###### YAML
@ -390,16 +387,15 @@ github: 'https://github.com/marzer'
name: 'Mark Gillard' name: 'Mark Gillard'
twitter: 'https://twitter.com/marzer8789' twitter: 'https://twitter.com/marzer8789'
cpp: cpp:
- 17 - 17
- 20 - 20
- 'and beyond' - 'and beyond'
lib: 'toml++' lib: 'toml++'
repo: 'https://github.com/marzer/tomlplusplus/' repo: 'https://github.com/marzer/tomlplusplus/'
toml: toml:
- '1.0.0' - '1.0.0'
- 'and beyond' - 'and beyond'
@endout @endout
@see @see
@ -479,7 +475,7 @@ Add `tomlplusplus/3.4.0` to your conanfile.
Add `tomlpp` to your `package.json5`, e.g.: Add `tomlpp` to your `package.json5`, e.g.:
@json @json
depends: [ depends: [
'tomlpp^3.4.0', 'tomlpp^3.4.0',
] ]
@endjson @endjson
@ -510,7 +506,7 @@ You can also add it as a subproject directly.
@json @json
{ {
"marzer/tomlplusplus": { } "marzer/tomlplusplus": { }
} }
@endjson @endjson
@ -529,9 +525,9 @@ vcpkg install tomlplusplus
@cmake @cmake
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
tomlplusplus tomlplusplus
GIT_REPOSITORY https://github.com/marzer/tomlplusplus.git GIT_REPOSITORY https://github.com/marzer/tomlplusplus.git
GIT_TAG v3.4.0 GIT_TAG v3.4.0
) )
FetchContent_MakeAvailable(tomlplusplus) FetchContent_MakeAvailable(tomlplusplus)
@endcmake @endcmake