|
ini-cpp
|
Yet another .ini parser for modern c++ (made for cpp17). It is a header only implementation, easy to install and simple to use.
The config.ini's content is something looks like:
To learn more, please refer to test folder, it covered ALL utilities.
All APIs live in namespace inih. All errors are reported by throwing std::runtime_error.
INIReader| API | Description |
|---|---|
INIReader(const std::string& filename) | Parse the given ini file; throws on missing file, syntax error (with line number) or duplicate keys |
INIReader(FILE* file) | Same, but reads from an already-opened FILE* (the caller keeps ownership) |
Sections() | All section names, as std::set<std::string> |
Keys(section) | All key names in the given section, as std::set<std::string> |
Get(section) | The whole section, as std::unordered_map<std::string, std::string> |
Get<T = std::string>(section, name) | The value parsed as T; throws if section/key is missing or the value cannot be parsed |
Get<T>(section, name, default_value) | Same, but returns default_value instead of throwing |
GetVector<T = std::string>(section, name) | The whitespace-separated value parsed as std::vector<T> |
GetVector<T>(section, name, default_vector) | Same, but returns default_vector instead of throwing |
InsertEntry(section, name, value) | Add a new entry (scalar or std::vector); throws if the key already exists |
UpdateEntry(section, name, value) | Overwrite an existing entry (scalar or std::vector); throws if the key does not exist |
T can be std::string, bool (accepts 1/0/true/false/yes/no/on/off, case-insensitive), any integer or floating-point type, or any custom type readable from a stream with operator>>.
INIWriter| API | Description |
|---|---|
INIWriter::write(filepath, reader, overwrite = false) | Dump an INIReader's content to a file; unless overwrite is true, throws if the file already exists |
Simply copy the header file ini/ini.h to your project, then done.