Skip to content

LordOfTrident/cxxini

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License Issues GitHub pull requests


A simple single-header C++11 INI reading and writing library.

Clone with

$ git clone --recurse-submodules https://github.com/lordoftrident/cxxini

Table of contents

Documentation

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.

Constructing

You can construct a record either by using initializer lists, index operator or string literals.

Initializer-lists

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({
	// ...
});

Index operator

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";

String literals

ini::record rec("foo = bar\n"
                "\n"
                "[food-ratings]\n"
                "salmon       = 10\n"
                "fish n chips = 8\n"
                "pizza        = 9\n");

Writing

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

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

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.

Quickstart

Either copy the header file cxxini.hh into your project, or you can use git submodules:

$ git submodule add https://github.com/LordOfTrident/cxxini

Bugs

If you find any bugs, please create an issue and report them.

About

A C++ INI reading and writing library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published