v0.9.17
This is a tiny, header only C++ library for manipulating INI files.
It conforms to the following format:
- section and key names are case insensitive by default
- whitespace around sections, keys and values is ignored
- empty section and key names are allowed
- keys that do not belong to a section are ignored
- comments are lines where the first non-whitespace character is a semicolon (
;
) - trailing comments are allowed on section lines, but not key/value lines
- every entry exists on a single line and multiline is not supported
;comment
[section]
key= value
Files are read on demand in one go, after which the data is kept in memory and is ready to be manipulated. Files are closed after read or write operations. This utility supports lazy writing, which only writes changes and updates and preserves custom formatting and comments. A lazy write invoked by awrite()
call will read the output file, find which changes have been made, and update the file accordingly. If you only need to generate files, usegenerate()
instead.
Section and key order is preserved on read and write operations. Iterating through data will take the same order as the original file or the order in which keys were added to the structure.
This library operates with thestd::string
type to hold values and relies on your host environment for encoding. It should play nicely with UTF-8 but your mileage may vary.
This is a header-only library. To install it, just copy everything in/src/
into your own project's source code folder, or use a custom location and just make sure your compiler sees the additional include directory. Then include the file somewhere in your code:
#include"mini/ini.h"
You're good to go!
Start with an INI file namedmyfile.ini
:
;amounts of fruits
[fruits]
apples=20
oranges=30
Our code:
//first, create a file instance
mINI::INIFilefile("myfile.ini");
//next, create a structure that will hold data
mINI::INIStructure ini;
//now we can read the file
file.read(ini);
//read a value
std::string& amountOfApples = ini["fruits"]["apples"];
//update a value
ini["fruits"]["oranges"] ="50";
//add a new entry
ini["fruits"]["bananas"] ="100";
//write updates to file
file.write(ini);
After running the code, our INI file now looks like this:
;amounts of fruits
[fruits]
apples=20
oranges=50
bananas=100
//create a file instance
mINI::INIFilefile("myfile.ini");
//create a data structure
mINI::INIStructure ini;
//populate the structure
ini["things"]["chairs"] ="20";
ini["things"]["balloons"] ="100";
//generate an INI file (overwrites any previous file)
file.generate(ini);
TheINIFile
class holds the filename and exposes functions for reading, writing and generating INI files. It does not keep the file open but merely provides an abstraction you can use to access physical files.
To create a file instance:
mINI::INIFilefile("myfile.ini");
You will also need a structure you can operate on:
mINI::INIStructure ini;
To read from a file:
boolreadSuccess = file.read(ini);
To write back to a file while preserving comments and custom formatting:
boolwriteSuccess = file.write(ini);
You can set the second parameter towrite()
totrue
if you want the file to be written with pretty-print. Pretty-print adds spaces between key-value pairs and blank lines between sections in the output file:
boolwriteSuccess = file.write(ini,true);
Awrite()
call will attempt to preserve any custom formatting the original INI file uses and will only use pretty-print for creation of new keys and sections.
To generate a file:
file.generate(ini);
Note thatgenerate()
will overwrite any custom formatting and comments from the original file!
You can use pretty-print withgenerate()
as well:
file.generate(ini,true);
Example output for a generated INI filewithoutpretty-print:
[section1]
key1=value1
key2=value2
[section2]
key1=value1
Example output for a generated INI filewithpretty-print:
[section1]
key1= value1
key2= value2
[section2]
key1= value1
There are two ways to read data from the INI structure. You can either use the[]
operator or theget()
function:
//read value - if key or section don't exist, they will be created
//returns reference to real value
std::string& value = ini["section"]["key"];
//read value safely - if key or section don't exist they will NOT be created
//returns a copy
std::string value = ini.get("section").get("key");
The difference between[]
andget()
operations is that[]
returns a reference torealdata (that you may modify) and creates a new item automatically if one does not already exist, whereasget()
returns acopyof the data and doesn't create new items in the structure. Usehas()
before doing any operations with[]
if you wish to avoid altering the structure.
You may combine usage of[]
andget()
.
Section and key names are case insensitive and are stripped of leading and trailing whitespace.ini[ "section" ]
is the same asini[ "SECTION" ]
is the same asini[ "sEcTiOn" ]
and so on, and same for keys. Generated files always use lower case for section and key names. Writing to an existing file will preserve letter cases of the original file whenever those keys or sections already exists.
To set or update a value:
ini["section"]["key"] ="value";
Note that when writing to a file, values will be stripped of leading and trailing whitespace. For example, the following value will be converted to just"c"
when reading back from a file:ini[ "a" ][ "b" ] = "c";
You can set multiple values at once by usingset()
:
ini["section"].set({
{"key1","value1"},
{"key2","value2"}
});
To create an empty section, simply do:
ini["section"];
Similarly, to create an empty key:
ini["section"]["key"];
To remove a single key from a section:
boolremoveSuccess = ini["section"].remove("key");
To remove a section:
boolremoveSuccess = ini.remove("section");
To remove all keys from a section:
ini["section"].clear();
To remove all data in structure:
ini.clear();
To check if a section is present:
boolhasSection = ini.has("section");
To check if a key within a section is present:
boolhasKey = ini["section"].has("key");
To get the number of keys in a section:
size_tn_keys = ini["section"].size();
To get the number of sections in the structure:
size_tn_sections = ini.size();
Keep in mind that[]
will always create a new item if the item does not already exist! You can usehas()
to check if an item exists before performing further operations. Remember thatget()
will return a copy of data, so you shouldnotbe doing removes or updates to data with it!
Usage of the[]
operator shouldn't be a problem in most real-world cases where you're doing lookups on known keys and you may not care if empty keys or sections get created. However - if you have a situation where you do not want new items to be added to the structure, either useget()
to retreive items, or if you don't want to be working with copies of data, usehas()
before using the[]
operator if you want to be on the safe side.
Short example that demonstrates safe manipulation of data:
if(ini.has("section"))
{
//we have section, we can access it safely without creating a new one
auto& collection = ini["section"];
if(collection.has("key"))
{
//we have key, we can access it safely without creating a new one
auto& value = collection["key"];
}
}
You can traverse the structure in order of insertion. The following example loops through the structure and displays results in a familiar format:
for(autoconst& it: ini)
{
autoconst& section = it.first;
autoconst& collection = it.second;
std::cout <<"["<< section <<"]"<< std::endl;
for(autoconst& it2: collection)
{
autoconst& key = it2.first;
autoconst& value = it2.second;
std::cout << key <<"="<< value << std::endl;
}
}
it.first
is alwaysstd::string
type.
it.second
is an object which is either amINI::INIMap
type on the first level orstd::string
type on the second level.
The API only exposes aconst_iterator
,so you can't use iterators to manipulate data directly. You can however access the structure as normal while iterating:
//change all values in the structure to "banana"
for(autoconst& it: ini)
{
autoconst& section = it.first;
autoconst& collection = it.second;
for(autoconst& it2: collection)
{
autoconst& key = it2.first;
ini[section][key] ="banana";//O(1) because hashmaps
}
}
If you wish to make the library not ignore letter case, add the directive#define MINI_CASE_SENSITIVE
beforeincluding the library:
#defineMINI_CASE_SENSITIVE
#include"mini/ini.h"
This will affect reading and writing from files and access to the structure.
- lest- testing framework
Copyright © 2018 Danijel Durakovic
Licensed under the terms of theMIT license