You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.3 KiB
Markdown

4 years ago
# ini-config
A single-header library that converts INI-formatted string literals to a key-value pair list at compile-time.
Requires C++20; tested on gcc 10.1 and clang trunk. Passes `-Wall -Wextra -pedantic`.
4 years ago
## Features
4 years ago
* Direct accesses to values are compile-time evaluated, allowing an INI config to be used for project/program configuration.
4 years ago
* Values can be accessed as strings, integers, or floating-point numbers.
4 years ago
* Run-time support includes key lookup, iteration through the key-value list, and key existance checking; all of which can be filtered by section.
4 years ago
[Try it on Godbolt.](https://godbolt.org/z/Ys1o9G)
4 years ago
### INI format notes
* Handles single-line `key=value` pairs (extra whitespace is okay)
4 years ago
* Supports sections
4 years ago
* Supports comments (start line with ';' or '#')
4 years ago
* Supports wide strings
4 years ago
* INI format is validated at compile-time, with future goal of clearly reporting syntax errors
4 years ago
## How to use
```cpp
#include "ini_config.hpp"
4 years ago
// Simply place the _ini suffix at the end of your config string:
constexpr auto config = R"(
4 years ago
someflag = true
[Cat]
color = gray
4 years ago
lives = 9
)"_ini;
4 years ago
// Or, go for a more functional look:
//constexpr auto config = make_ini_config<R"( ... )">;
4 years ago
auto KVPcount = config.size(); // = 3
for (auto kvp : config) {} // Iterate through all KVPs
// (or use begin()/end())
for (auto kvp : config.section("Cat")) {} // Iterate through all KVPs under [Cat] section
// (or use begin("Cat")/end("Cat"))
config.get("someflag"); // Searches entire config for "someflag", picks first match
// This call gets compile-time evaluated to "true"
config.get("Cat", "lives"); // Searches "Cat" section, compile-time evaluated to "9"
config.get<int>("Cat", "lives"); // Compile-time evaluated to 9
config.get("Dog", "lives"); // Does not exist, compile-time evaluated to ""
config.contains("Dog", "lives"); // Compile-time evaluated to false
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
4 years ago
```
See the header file for further documentation.