ASL
IniFile Class Reference

Detailed Description

A utility to read and write configuration files in INI format.

On destruction, if there was any variable added or modified the file is automatically saved. When reading a variable name it is considered a "section/name" pair.

Example:

[main]
color = white
[network]
num_retries = 3

Can be read and written with:

IniFile config("settings.ini");
int num_retries = config["network/num_retries"];
config.set("main/color", "black");
IniFile(const String &path, bool shouldwrite=true)
Opens an INI file from the given path.

The file in that example will be saved if the original color in section main was not "black".

The file can be cheked for correct reading and whether a variable exists:

IniFile config("settings.ini");
if(!config.ok()) {
printf("Error reading %s\n", *config.fileName());
return;
}
int num_retries = 5;
if(config.has("network/num_retries"))
num_retries = config["network/num_retries"];

That last part can also be written as:

int num_retries = config["network/num_retries"] | 5;

#include <IniFile.h>

Public Member Functions

 IniFile (const String &path, bool shouldwrite=true)
 Opens an INI file from the given path.
 
 ~IniFile ()
 Destroys the object and save the file if there were modifications.
 
bool ok () const
 Returns true if the file was read correctly.
 
const StringfileName () const
 Returns the file name of this IniFile.
 
Stringoperator[] (const String &name)
 Returns the value associated with the key name in the current section if the name is like section/name then the given section is used. More...
 
void set (const String &name, const String &value)
 Sets the value for a key like 'section/name'.
 
const String operator() (const String &name, const String &defaultVal) const
 Returns the value associated with the key name or defaultVal if it was not found.
 
void write (const String &fname="")
 Writes the file with its modifications; this is done automatically on destruction, just call this if you need the file written before the object is detroyed.
 
Sectionsection (const String &name)
 Sets the current section to 'name' (variables will be read from here by default) More...
 
bool has (const String &name) const
 Returns true if the file contains a key named name. More...
 
int arraysize (const String &name) const
 Returns the length of an "array" named name as written by the Qt library and enables reading its values. More...
 
String array (const String &field, int index) const
 Returns the value associated with field name at the array position index of the array last specified with arraysize(). More...
 
Array< StringsectionNames () const
 Returns the names of sections in the file.
 
Dic values () const
 Returns all keys and values as a map with keys as "section/key".
 
Dic values (const String &secname) const
 Returns a section's keys and values as a map.
 

Member Function Documentation

◆ array()

String array ( const String field,
int  index 
) const

Returns the value associated with field name at the array position index of the array last specified with arraysize().

Deprecated:
Use ini[name/index\field]

◆ arraysize()

int arraysize ( const String name) const

Returns the length of an "array" named name as written by the Qt library and enables reading its values.

Deprecated:
Use ini[name/size]

◆ has()

bool has ( const String name) const

Returns true if the file contains a key named name.

If the name is like section/name then function tests a variable name in section section.

◆ operator[]()

String& operator[] ( const String name)

Returns the value associated with the key name in the current section if the name is like section/name then the given section is used.

In the future this might return a const ref, so that modifications must be made with .set(k, v)

◆ section()

Section& section ( const String name)

Sets the current section to 'name' (variables will be read from here by default)

Deprecated:
Use full names like "section/key"

The documentation for this class was generated from the following file: