Skip to content

mcmtroffaes/inipp

Repository files navigation

IniPP

Simple header-only C++ ini parser and generator.

Build Status

Features

  • Header-only.
  • Both parsing and generating.
  • Wide character support for native unicode on Windows.
  • Default section support (similar to Python's ConfigParser).
  • Interpolation support (i.e. variable substitution, similar to Python's ConfigParser).
  • Trailing comments support.
  • Simple design and implementation.
  • Permissive MIT license.

Example

#include<fstream>
#include"inipp.h"

intmain() {
inipp::Ini<char> ini;
std::ifstreamis("example.ini");
ini.parse(is);
std::cout <<"raw ini file:"<< std::endl;
ini.generate(std::cout);
ini.strip_trailing_comments();
ini.default_section(ini.sections["DEFAULT"]);
ini.interpolate();
std::cout <<"ini file after default section and interpolation:"<< std::endl;
ini.generate(std::cout);
intcompression_level = -1;
inipp::get_value(ini.sections["bitbucket.org"],"CompressionLevel",compression_level);
std::cout <<"bitbucket.org compression level:"<< compression_level << std::endl;
return0;
}

To include in a cmake project:

FetchContent_Declare(inipp GIT_REPOSITORY https://github /mcmtroffaes/inipp.git)
FetchContent_MakeAvailable(inipp)
target_link_libraries(MyTarget PRIVATE inipp::inipp)

Parsing algorithm

  • Thesectionis set to the empty string.

  • Everylineis read from the file and trimmed from whitespace.

    • Iflineis empty or starts with;then nothing happens.

    • Otherwise, iflinestarts with[thensectionis changed to the string between[and].Iflinedoes not end with]then an error is reported.

    • Otherwise, iflinecontains an=sign, then all characters before=are treated asvariableand all characters following=are treated asvalue.Both are trimmed. If the variable was already assigned earlier, an error is reported. Otherwise, the corresponding assigment is added to the section.

    • Otherwise, thelineis reported as an error.

Default section algorithm

Insert every variable from the default section into every other section, without overwriting existing variables.

Interpolation algorithm

  1. Locally within each section, every occurrence "${variable}" is replaced by "${section:variable}".
  2. Every occurrence of "${section:variable}" is replaced by its value.
  3. The previous step is repeated until no more replacements are possible, or until the recursion depth (by default, 10) is reached.