Update README.md

master
Clyne 4 years ago committed by GitHub
parent f6bc63096b
commit d4f62d9d2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,22 +2,28 @@
A single-header library that converts INI-formatted string literals to a key-value pair list at compile-time. A single-header library that converts INI-formatted string literals to a key-value pair list at compile-time.
## Features
* Direct accesses to values are compile-time evaluated, allowing an INI config to replace a list of macros or `constexpr` globals.
* Values can be accessed as strings, integers, or floating-point numbers.
* Run-time support: can iterate through the key-value list or check for a key's existance, both with optional filtering by section.
Requires C++20. Tested to work on gcc 10.1 and clang trunk. Passes `-Wall -Wextra -pedantic`. Requires C++20. Tested to work on gcc 10.1 and clang trunk. Passes `-Wall -Wextra -pedantic`.
[Try it on Godbolt.](https://godbolt.org/z/WTPzE3) [Try it on Godbolt.](https://godbolt.org/z/WTPzE3)
## Features ### INI format notes
* Comments and whitespace removed during compilation * Handles single-line `key=value` pairs (extra whitespace is okay)
* Handles single-line `key=value` pairs with very basic syntax error reporting
* Supports sections * Supports sections
* Supports comments (start line with ';' or '#', preceding whitespace is okay) * Supports comments (start line with ';' or '#')
* Supports wide strings * Supports wide strings
* INI format is validated at compile-time, with future goal of clearly reporting syntax errors
## How to use ## How to use
```cpp ```cpp
#include "ini_config.hpp" #include "ini_config.hpp"
auto config = R"( // Simply place the _ini suffix at the end of your config string:
constexpr auto config = R"(
someflag = true someflag = true
[Cat] [Cat]
@ -25,23 +31,20 @@ color = gray
lives = 9 lives = 9
)"_ini; )"_ini;
// Returns count of key-value pairs (KVPs), 3 in this case auto KVPcount = config.size(); // = 3
config.size(); for (auto kvp : config) {} // Iterate through all KVPs
// Iterate through all KVPs // (or use begin()/end())
for (auto kvp : config) {} for (auto kvp : config.section("Cat")) {} // Iterate through all KVPs under [Cat] section
// Iterate through all KVPs under [Cat] section // (or use begin("Cat")/end("Cat"))
for (auto kvp : config.section("Cat")) {} config.get("someflag"); // Searches entire config for "someflag", picks first match
// Access specific values // This call gets compile-time evaluated to "true"
config.get("someflag"); // Returns "true" config.get("Cat", "lives"); // Searches "Cat" section, compile-time evaluated to "9"
config.get("lives"); // Searches all KVPs, returns "9" config.get<int>("Cat", "lives"); // Compile-time evaluated to 9
config["lives"]; // Same as above config.get("Dog", "lives"); // Does not exist, compile-time evaluated to ""
config.get("Cat", "color"); // Only searches [Cat] section, Returns "gray" config.contains("Dog", "lives"); // Compile-time evaluated to false
config.get("Dog", "color"); // No match, returns ""
config.tryget(argv[2]); // Same interface and behavior as get(),
// use this when run-time evaluation is necessary
config.trycontains("color"); // Run-time evaluated to true
``` ```
See the header file for further documentation. See the header file for further documentation.
## How it works (brief)
1. `_ini` prefix constructs an `ini_config` object with the given string.
2. Syntax is validated, and a character array is sized to fit all section names, keys, and values.
3. The array is populated with values.
4. Compiled result is the minimized array, with efficient functions for iteration and access.

Loading…
Cancel
Save