From: Enar Väikene Date: Thu, 29 Sep 2011 13:32:09 +0000 (+0300) Subject: Started working on the IniFile class. X-Git-Url: https://vaikene.ee/gitweb/gitweb.cgi?p=evaf;a=commitdiff_plain;h=09e5758f2e8557be2f23ed7a8a51ca4ef855e9ab Started working on the IniFile class. --- diff --git a/src/libs/Common/inifile.h b/src/libs/Common/inifile.h new file mode 100644 index 0000000..59f6fa0 --- /dev/null +++ b/src/libs/Common/inifile.h @@ -0,0 +1,139 @@ +/** + * @file Common/inifile.h + * @brief Class for reading and writing parameter values in INI files. + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#ifndef __COMMON_INIFILE_H +#define __COMMON_INIFILE_H + +#include "libcommon.h" + +namespace eVaf { +namespace Common { +namespace Internal { + class IniFileImpl; +} // namespace eVaf::Common::Internal + +/** + * 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. + * + * 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: + * + * @li Bool - '0', 'false', 'off', 'no' are equal to false and '1', 'true', 'on', 'yes' are equal to true; + * @li Char - a single character or an ASCII code as '\0NNN' or '\0xNN'; + * @li Date - date string in the ISO 8601 format YYYY-MM-DD; + * @li DateTime - date and time string in the ISO 8601 format YYY-MM-DDTHH:MM:SSTZD; + * @li Double - the decimal point is always '.' regardless of the locale; + * @li Int - only base 10 (decimal) is allowed; + * @li Time - 24h time string in the format HH:MM:SS + * @li UInt - base 16 (hex) if the string is prefixed with '0x'; base 8 if the string starts with '0'; otherwise + * the value is expected to be base 10 (decimal). + */ +class COMMON_EXPORT IniFile +{ +public: + + /** + * Creates the INI file object + * @param fileName Name of the INI file + * @param mode File opening mode + */ + IniFile(QString const & fileName, QIODevice::OpenMode mode = QIODevice::ReadWrite); + + /// Dtor + ~IniFile(); + + /** + * 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. + * + * If the object is not valid, then: + * @li Writing to the INI file always fails; the internal cache is still updated and reading the parameter returns + * the new value; + * @li Reading from the INI file returns the cached value or the default value if no values with this name are written. + */ + bool isValid() const; + + /** + * Returns the last error message. + * + * The errorString() method returns a human-readable error message if an operation with the INI file failed. + */ + QString errorString() const; + + /** + * Reads a value from the INI file. + * @param paramName Name of the parameter + * @param defaultValue Default value returned if the parameter value cannot be read + * @return The value from the INI file or an invalid QVariant if failed + * + * 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: + * @li If the value in the INI file is 'abc' and the default value an integer value, then validation + * fails and the method returns the default value; + * @li If the default value is a boolean value, then the method accepts '1', 'true', 'on' and 'yes' as valid values. + */ + QVariant getValue(QString const & paramName, QVariant const & defaultValue = QVariant::Invalid); + + /** + * Writes a value to the INI file. + * @param paramName Name of the parameter + * @param value The value of the value + * @return True if succeeded and false if not + * + * 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. + * + * Writing to an invalid INI file always fails, but the value is still stored into the internal cache. Readin the same parameter + * value returns the new value even if it was actually not stored into the INI file. + */ + bool setValue(QString const & paramName, QVariant const & value); + + +private: + + /// Pointer to the internal implementation of the class + Internal::IniFileImpl * d; + +}; + +} // namespace eVaf::Common +} // namespace eVaf + +#endif // INIFILE_H