A simple single-header C++11 INI reading and writing library.
Clone with
$ git clone --recurse-submodules https://github.com/lordoftrident/cxxini
This library provides a single class ini::record
, which inherits from ini::sections
, which is
a std::map<std::string, std::map<std::string, std::string>>
, so it has all of its functionality.
You can construct a record either by using initializer lists, index operator or string literals.
With the constructor
ini::record rec({
{ini::default_section, { // ini::default_section is the unnamed section at the
{"foo", "bar"}, // top of an ini file and is the same as []
}},
{"food-ratings", {
{"salmon", "10"},
{"fish n chips", "8"},
{"pizza", "9"},
}},
});
Or you can use assignment
auto rec = ini::record({
// ...
});
ini::record rec;
rec[ini::default_section]["foo"] = "bar";
rec["food-ratings"]["salmon"] = "10";
rec["food-ratings"]["fish n chips"] = "8";
rec["food-ratings"]["pizza"] = "9";
ini::record rec("foo = bar\n"
"\n"
"[food-ratings]\n"
"salmon = 10\n"
"fish n chips = 8\n"
"pizza = 9\n");
For writing, you can use the stringify
method to return an std::string
ini::record rec;
rec[ini::default_section]["foo"] = "bar";
std::cout << rec.stringify() << std::endl;
or you can use <<
to insert it into a stream (this just calls stringify
under the hood)
ini::record rec;
rec[ini::default_section]["foo"] = "bar";
std::cout << rec << std::endl;
Reading from a string
ini::record rec;
rec.parse("foo = bar");
from a file
ini::record rec;
std::ifstream file("test.ini");
rec.parse(file);
The return value of parse
is 0 if there was no error, otherwise its the number of the line at
which the error occured (starting at 1)
you can also use the >>
operator to read from a file
ini::record rec;
std::ifstream file("test.ini");
file >> rec;
Examples can be found in the examples folder. To build them, first bootstrap the builder with
$ cc build.c -o build
(you can also use a C++ compiler for it)
and then build
$ ./build
The binaries will be outputted into bin.
Either copy the header file cxxini.hh into your project, or you can use git submodules:
$ git submodule add https://github.com/LordOfTrident/cxxini
If you find any bugs, please create an issue and report them.