ASL
Loading...
Searching...
No Matches
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:

color = white
An Array is a contiguous and resizable array of any type of elements.
Definition Array.h:77

Can be read and written with:

IniFile config("settings.ini");
int num_retries = config["network/num_retries"];
config.set("main/color", "black");
A utility to read and write configuration files in INI format.
Definition IniFile.h:57

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 <asl/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.
 
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.
 
bool has (const String &name) const
 Returns true if the file contains a key named name.
 
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

◆ 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)


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