eVaf
|
Class for reading and writing parameter values in INI files. More...
#include <Common/inifile.h>
Public Member Functions | |
QString | errorString () const |
Returns the last error message. More... | |
QVariant | getValue (QByteArray const ¶mName, QVariant const &defaultValue=QVariant::Invalid) |
Reads a value from the INI file. More... | |
IniFile (QString const &fileName, QIODevice::OpenMode mode=QIODevice::ReadWrite) | |
Creates the INI file object. More... | |
bool | isValid () const |
Returns true if the object is valid. More... | |
bool | setValue (QByteArray const ¶mName, QVariant const &value) |
Writes a value to the INI file. More... | |
~IniFile () | |
Dtor. More... | |
Class for reading and writing parameter values in INI files.
The IniFile class provides access to parameter values in a standard INI file. Every parameter value is identified by a section/key name pair. Key names can be prefixed with 'windows:' or 'linux:' if the parameter value is specific for the given platform. This allows entering platform-specific parameter values to the INI file.
INI files can contain comment lines that start with ';' or '#'. Comments are preserved when new values are written to the INI file.
This is a sample INI file:
Values in INI files are stored as strings and the IniFile class expects them to use specific formats if the returned value is supposed to be of a different data type. The following list shows the expected format of different data types:
Strings and Byte array values can be enclosed in single or double quotes. The IniFile class does this automatically when saving String or Byte array values with leading or trailing spaces. Quotes are removed from the parameter value prior returning the value to the application. Use character entity references """ and "'" if quotes should be part of the parameter value.
eVaf::Common::IniFile::IniFile | ( | QString const & | fileName, |
QIODevice::OpenMode | mode = QIODevice::ReadWrite |
||
) |
Creates the INI file object.
fileName | Name of the INI file |
mode | File opening mode |
eVaf::Common::IniFile::~IniFile | ( | ) |
Dtor.
QString eVaf::Common::IniFile::errorString | ( | ) | const |
Returns the last error message.
The errorString() method returns a human-readable error message if an operation with the INI file failed.
QVariant eVaf::Common::IniFile::getValue | ( | QByteArray const & | paramName, |
QVariant const & | defaultValue = QVariant::Invalid |
||
) |
Reads a value from the INI file.
paramName | Name of the parameter |
defaultValue | Default value returned if the parameter value cannot be read |
The getValue() method reads a parameter value from the INI file. Parameters are identified by a section and key name pair in the format '<section>/<key>'. For example, if the section is called 'general' and the key is called 'log_level', then the name of the parameter should be given as 'general/log_level'. Parameter names are case insensitive.
If the parameter cannot be read because it is not found in the INI file or the INI file object is invalid, returns the default value. If reading the parameter fails by any other reasons, returns an invalid QVariant value (QVariant::Invalid).
If the default value is of a valid and known QVariant type, then its type is used to validate and optionally convert the string read from the INI file. For example:
bool eVaf::Common::IniFile::isValid | ( | ) | const |
Returns true if the object is valid.
The isValid() method returns true if the INI file is can be used. Use this method after creating the object to verify that opening the INI file in the specified mode succeeded.
bool eVaf::Common::IniFile::setValue | ( | QByteArray const & | paramName, |
QVariant const & | value | ||
) |
Writes a value to the INI file.
paramName | Name of the parameter |
value | The value of the value |
The setValue() method writes a parameter value to the INI file. Parameters are identified by a section and key name pair in the format '<section>/<key>'. For example, if the section is called 'general' and the key is called 'log_level', the the name of the parameter should be given as 'general/log_level'. Parameter names are case insensitive.
The method returns true if the parameter value was written into the INI file and false if not. Use the errorString() method to get a human-readable error string if writing to the INI file fails.