ini-cpp
Loading...
Searching...
No Matches
inih::INIReader Class Reference

Read an INI file into easy-to-access name/value pairs. More...

#include <ini.h>

Public Member Functions

 INIReader (const std::string &filename)
 Construct an INIReader object from a file name.
 
 INIReader (std::FILE *file)
 Construct an INIReader object from a file pointer.
 
int ParseError () const
 Return the result of the parse, i.e., 0 on success.
 
std::set< std::string > Sections () const
 Return the list of sections found in ini file.
 
std::set< std::string > Keys (const std::string &section) const
 Return the list of keys in the given section.
 
std::unordered_map< std::string, std::string > Get (const std::string &section) const
 Get the map representing the values in a section of the INI file.
 
template<typename T = std::string>
Get (const std::string &section, const std::string &name) const
 Return the value of the given key in the given section.
 
template<typename T >
Get (const std::string &section, const std::string &name, T &&default_v) const
 Return the value of the given key in the given section, return default if not found.
 
template<typename T = std::string>
std::vector< T > GetVector (const std::string &section, const std::string &name) const
 Return the value array of the given key in the given section.
 
template<typename T >
std::vector< T > GetVector (const std::string &section, const std::string &name, const std::vector< T > &default_v) const
 Return the value array of the given key in the given section, return default if not found.
 
template<typename T = std::string>
void InsertEntry (const std::string &section, const std::string &name, const T &v)
 Insert a key-value pair into the INI file.
 
template<typename T = std::string>
void InsertEntry (const std::string &section, const std::string &name, const std::vector< T > &vs)
 Insert a vector of values into the INI file.
 
template<typename T = std::string>
void UpdateEntry (const std::string &section, const std::string &name, const T &v)
 Update a key-value pair in the INI file.
 
template<typename T = std::string>
void UpdateEntry (const std::string &section, const std::string &name, const std::vector< T > &vs)
 Update a vector of values in the INI file.
 

Protected Member Functions

template<typename T >
Converter (const std::string &s) const
 Parse s as a T; throws std::runtime_error on failure.
 
bool BoolConverter (std::string s) const
 Parse a boolean token: 1/0/true/false/yes/no/on/off, case-insensitive; throws std::runtime_error on anything else.
 
template<typename T >
std::string V2String (const T &v) const
 Serialize a value with operator<<.
 
template<typename T >
std::string Vec2String (const std::vector< T > &v) const
 Serialize a vector as space-separated values.
 

Protected Attributes

int _error = 0
 Parse result: 0 on success, -1 on file open error, otherwise the number of the first faulty line.
 
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > _values
 Parsed content, as _values[section][name] = value.
 

Detailed Description

Read an INI file into easy-to-access name/value pairs.

Constructor & Destructor Documentation

◆ INIReader() [1/2]

inih::INIReader::INIReader ( const std::string & filename)
inline

Construct an INIReader object from a file name.

Parameters
filenameThe name of the INI file to parse
Exceptions
std::runtime_errorif there is an error parsing the INI file

◆ INIReader() [2/2]

inih::INIReader::INIReader ( std::FILE * file)
inline

Construct an INIReader object from a file pointer.

Parameters
fileA pointer to the INI file to parse
Exceptions
std::runtime_errorif there is an error parsing the INI file

Member Function Documentation

◆ Get() [1/3]

std::unordered_map< std::string, std::string > inih::INIReader::Get ( const std::string & section) const
inline

Get the map representing the values in a section of the INI file.

Parameters
sectionThe name of the section to retrieve
Returns
The map representing the values in the given section
Exceptions
std::runtime_errorif the section is not found

◆ Get() [2/3]

template<typename T = std::string>
T inih::INIReader::Get ( const std::string & section,
const std::string & name ) const
inline

Return the value of the given key in the given section.

Parameters
sectionThe section name
nameThe key name
Returns
The value of the given key in the given section
Exceptions
std::runtime_errorif the section/key is not found or the value cannot be parsed to type T

◆ Get() [3/3]

template<typename T >
T inih::INIReader::Get ( const std::string & section,
const std::string & name,
T && default_v ) const
inline

Return the value of the given key in the given section, return default if not found.

Parameters
sectionThe section name
nameThe key name
default_vThe default value
Returns
The value of the given key in the given section, return default if not found

◆ GetVector() [1/2]

template<typename T = std::string>
std::vector< T > inih::INIReader::GetVector ( const std::string & section,
const std::string & name ) const
inline

Return the value array of the given key in the given section.

Parameters
sectionThe section name
nameThe key name
Returns
The value array of the given key in the given section.

For example:

[section]
key = 1 2 3 4
const auto vs = ini.GetVector<int>("section", "key");
// vs = {1, 2, 3, 4}

◆ GetVector() [2/2]

template<typename T >
std::vector< T > inih::INIReader::GetVector ( const std::string & section,
const std::string & name,
const std::vector< T > & default_v ) const
inline

Return the value array of the given key in the given section, return default if not found.

Parameters
sectionThe section name
nameThe key name
default_vThe default value
Returns
The value array of the given key in the given section, return default if not found
See also
INIReader::GetVector

◆ InsertEntry() [1/2]

template<typename T = std::string>
void inih::INIReader::InsertEntry ( const std::string & section,
const std::string & name,
const std::vector< T > & vs )
inline

Insert a vector of values into the INI file.

Parameters
sectionThe section name
nameThe key name
vsThe vector of values to insert
Exceptions
std::runtime_errorif the key already exists in the section

◆ InsertEntry() [2/2]

template<typename T = std::string>
void inih::INIReader::InsertEntry ( const std::string & section,
const std::string & name,
const T & v )
inline

Insert a key-value pair into the INI file.

Parameters
sectionThe section name
nameThe key name
vThe value to insert
Exceptions
std::runtime_errorif the key already exists in the section

◆ Keys()

std::set< std::string > inih::INIReader::Keys ( const std::string & section) const
inline

Return the list of keys in the given section.

Parameters
sectionThe section name
Returns
The list of keys in the given section

◆ ParseError()

int inih::INIReader::ParseError ( ) const
inline

Return the result of the parse, i.e., 0 on success.

Exceptions
std::runtime_erroron file open or parse error

◆ Sections()

std::set< std::string > inih::INIReader::Sections ( ) const
inline

Return the list of sections found in ini file.

Returns
The list of sections found in ini file

◆ UpdateEntry() [1/2]

template<typename T = std::string>
void inih::INIReader::UpdateEntry ( const std::string & section,
const std::string & name,
const std::vector< T > & vs )
inline

Update a vector of values in the INI file.

Parameters
sectionThe section name
nameThe key name
vsThe new vector of values to set
Exceptions
std::runtime_errorif the key does not exist in the section

◆ UpdateEntry() [2/2]

template<typename T = std::string>
void inih::INIReader::UpdateEntry ( const std::string & section,
const std::string & name,
const T & v )
inline

Update a key-value pair in the INI file.

Parameters
sectionThe section name
nameThe key name
vThe new value to set
Exceptions
std::runtime_errorif the key does not exist in the section

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