UTF8 - Simple Library for Internationalization
Loading...
Searching...
No Matches
utf8::IniFile Class Reference

Operations on INI files. More...

#include <ini.h>

Public Member Functions

 IniFile (const std::string &name)
 Constructor based on an existing or new INI file.
 
 IniFile ()
 Default constructor uses a temporary file.
 
 IniFile (const IniFile &p)
 Copy constructor.
 
 ~IniFile ()
 Destructor.
 
IniFileoperator= (const IniFile &)
 Assignment operator.
 
const std::string & File () const
 Return file name associated with this object.
 
void File (const std::string &filename)
 Set the file name associated with this object.
 
size_t GetString (char *value, size_t len, const std::string &key, const std::string &section, const std::string &defval=std::string()) const
 Get a string key.
 
std::string GetString (const std::string &key, const std::string &section, const std::string &defval=std::string()) const
 Return a string key.
 
int GetInt (const std::string &key, const std::string &section, int defval=0) const
 Return an integer key.
 
double GetDouble (const std::string &key, const std::string &section, double defval=0.) const
 Return a floating point value.
 
bool GetBool (const std::string &key, const std::string &section, bool defval=false) const
 Return a boolean key.
 
bool HasKey (const std::string &key, const std::string &section) const
 Check for key existence.
 
bool PutString (const std::string &key, const std::string &value, const std::string &section)
 Write a string key.
 
bool PutInt (const std::string &key, long value, const std::string &section)
 Write an integer key.
 
bool PutBool (const std::string &key, bool value, const std::string &section)
 Write a boolean key.
 
bool PutDouble (const std::string &key, double value, const std::string &section, int dec=2)
 Write a floating point value key.
 
bool DeleteKey (const std::string &key, const std::string &section)
 Delete a key.
 
bool DeleteSection (const std::string &section)
 Delete an entire section.
 
bool CopySection (const IniFile &from_file, const std::string &from_sect, const std::string &to_sect=std::string())
 Copy all keys from one section to another.
 
bool HasSection (const std::string &section)
 Return true if profile contains a non empty section with the given name.
 
int GetKeys (char *buffer, size_t sz, const std::string &section)
 Retrieve names of all keys in a section.
 
size_t GetKeys (std::deque< std::string > &keys, const std::string &section)
 Retrieve names of all keys in a section.
 
size_t GetSections (char *sects, size_t sz)
 Return the names of all sections in the INI file.
 
size_t GetSections (std::deque< std::string > &sections)
 Return the names of all sections in the INI file.
 

Detailed Description

Operations on INI files.

Provides a handy object oriented encapsulation of functions needed to manipulate INI (profile) files.

Constructor & Destructor Documentation

◆ IniFile() [1/2]

utf8::IniFile::IniFile ( const std::string &  name)
explicit

Constructor based on an existing or new INI file.

Constructor.

◆ IniFile() [2/2]

utf8::IniFile::IniFile ( )

Default constructor uses a temporary file.

Creates a temporary file as filename.

◆ ~IniFile()

utf8::IniFile::~IniFile ( )

Destructor.

Destructor. If this was a temporary file it is deleted now.

Member Function Documentation

◆ CopySection()

bool utf8::IniFile::CopySection ( const IniFile from_file,
const std::string &  from_sect,
const std::string &  to_sect = std::string() 
)

Copy all keys from one section to another.

Copy a whole section form another INI file.

Parameters
from_filesource INI file
from_sectsource section
to_sectdestination section
Returns
true if successful, false otherwise

If to_sec is empty the destination section is the same as the source section.

The previous content of destination section is erased.

Precondition
from_sect cannot be empty

◆ DeleteKey()

bool utf8::IniFile::DeleteKey ( const std::string &  key,
const std::string &  section 
)

Delete a key.

Parameters
keykey name
sectionsection name
Returns
true if successful, false otherwise

◆ DeleteSection()

bool utf8::IniFile::DeleteSection ( const std::string &  section)

Delete an entire section.

Parameters
sectionsection name

◆ File()

void utf8::IniFile::File ( const std::string &  fname)

Set the file name associated with this object.

Changes the file associated with this object.

If previous one was a temporary file, it is deleted now (loosing all settings in the process).

Parameters
fnameNew file name. If empty it creates a temporary file.

◆ GetBool()

bool utf8::IniFile::GetBool ( const std::string &  key,
const std::string &  section,
bool  defval = false 
) const

Return a boolean key.

True values can be specified by any of "on", "yes", "true" or "1".

Anything else is considered as FALSE. Strings are case-insensitive.

Parameters
keykey name
sectionsection name
defvaldefault value if key is missing

◆ GetDouble()

double utf8::IniFile::GetDouble ( const std::string &  key,
const std::string &  section,
double  defval = 0. 
) const

Return a floating point value.

Parameters
keykey name
sectionsection name
defvaldefault value if key is missing

◆ GetInt()

int utf8::IniFile::GetInt ( const std::string &  key,
const std::string &  section,
int  defval = 0 
) const

Return an integer key.

Parameters
keykey name
sectionsection name
defvaldefault value if key is missing

◆ GetKeys() [1/2]

int utf8::IniFile::GetKeys ( char *  keys,
size_t  sz,
const std::string &  section 
)

Retrieve names of all keys in a section.

Key names are returned as null-terminated strings followed by one final null.

Parameters
keysbuffer for returned keys
szsize of buffer
sectionsection name
Returns
number of keys in section

◆ GetKeys() [2/2]

size_t utf8::IniFile::GetKeys ( std::deque< std::string > &  keys,
const std::string &  section 
)

Retrieve names of all keys in a section.

Key names are returned as a deque array.

Parameters
keyscontainer for returned keys
sectionsection name
Returns
number of keys in section

◆ GetSections() [1/2]

size_t utf8::IniFile::GetSections ( char *  sects,
size_t  sz 
)

Return the names of all sections in the INI file.

Section names are returned as null-terminated strings followed by one final null.

Parameters
sectsbuffer for returned keys
szsize of buffer
Returns
number of sections found
Precondition
sects !=NULL

The number of sections returned is the total number of sections in the INI file even if the buffer is too small to hold sections' names.

◆ GetSections() [2/2]

size_t utf8::IniFile::GetSections ( std::deque< std::string > &  sects)

Return the names of all sections in the INI file.

Section names are returned as a deque of strings.

Parameters
sectsdeque of sections
Returns
number of sections found

◆ GetString() [1/2]

size_t utf8::IniFile::GetString ( char *  value,
size_t  len,
const std::string &  key,
const std::string &  section,
const std::string &  defval = std::string() 
) const

Get a string key.

Parameters
valuebuffer for returned string
lenlength of buffer
keykey name
sectionsection name
defvaldefault value
Returns
length of returned string

◆ GetString() [2/2]

std::string utf8::IniFile::GetString ( const std::string &  key,
const std::string &  section,
const std::string &  defval = std::string () 
) const

Return a string key.

Parameters
keykey name
sectionsection name
defvaldefault value
Returns
key value

◆ HasKey()

bool utf8::IniFile::HasKey ( const std::string &  key,
const std::string &  section 
) const

Check for key existence.

Return true if specified key exists in the INI file.

◆ HasSection()

bool utf8::IniFile::HasSection ( const std::string &  section)

Return true if profile contains a non empty section with the given name.

Parameters
sectionsection name

◆ operator=()

IniFile & utf8::IniFile::operator= ( const IniFile p)

Assignment operator.

Assignment operator performs a file copy of the passed object.

◆ PutBool()

bool utf8::IniFile::PutBool ( const std::string &  key,
bool  value,
const std::string &  section 
)

Write a boolean key.

Boolean values are encoded as "On" "Off" strings.

Parameters
keykey name
sectionsection name
valuekey value
Returns
true if successful, false otherwise

◆ PutDouble()

bool utf8::IniFile::PutDouble ( const std::string &  key,
double  value,
const std::string &  section,
int  dec = 2 
)

Write a floating point value key.

Parameters
keykey name
valuekey value
sectionsection name
decnumber of decimals
Returns
true if successful, false otherwise

◆ PutInt()

bool utf8::IniFile::PutInt ( const std::string &  key,
long  value,
const std::string &  section 
)

Write an integer key.

Parameters
keykey name
valuekey value
sectionsection name
Returns
true if successful, false otherwise

◆ PutString()

bool utf8::IniFile::PutString ( const std::string &  key,
const std::string &  value,
const std::string &  section 
)

Write a string key.

Parameters
valuekey value
keykey name
sectionsection name
Returns
true if successful, false otherwise

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