]>
vaikene.ee Git - evaf/blob - inifile.h
6bcf86beb6f7d234f0959aeb4aede742e5c95351
2 * @file Common/inifile.h
3 * @brief Class for reading and writing parameter values in INI files.
6 * Copyright (c) 2011-2019 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
20 #ifndef __COMMON_INIFILE_H
21 #define __COMMON_INIFILE_H
23 #include "libcommon.h"
28 #include <QScopedPointer>
34 } // namespace eVaf::Common::Internal
37 * Class for reading and writing parameter values in INI files.
39 * The IniFile class provides access to parameter values in a standard INI file.
40 * Every parameter value is identified by a section/key name pair. Key names can be prefixed with
41 * 'windows:' or 'linux:' if the parameter value is specific for the given platform.
42 * This allows entering platform-specific parameter values to the INI file.
44 * INI files can contain comment lines that start with ';' or '#'. Comments are
45 * preserved when new values are written to the INI file.
47 * This is a sample INI %file:
50 * # The full name of this parameter is 'main/name'
51 * name = "My Application"
53 * # 4-byte binary version number 1.0.7.10 with the name 'main/version'
54 * version = �

57 * # The full name of this parameter is 'extensions/about/module'
58 * module = libabout.so
61 * Values in INI files are stored as strings and the IniFile class expects them to use specific formats if
62 * the returned value is supposed to be of a different data type. The following list shows the expected format
63 * of different data types:
65 * @li Bool - '0', 'false', 'off', 'no' are equal to false and '1', 'true', 'on', 'yes' are equal to true;
66 * @li Char - a single character or an UTF-16 code as '\0NNNNNN' (oct) or '\0xNNNN' (hex);
67 * @li Date - date string in the ISO 8601 format YYYY-MM-DD;
68 * @li DateTime - date and time string in the ISO 8601 format YYY-MM-DDTHH:MM:SSTZD;
69 * @li Double - the decimal point is always '.' regardless of the locale;
70 * @li Int - only base 10 (decimal) is allowed;
71 * @li Time - 24h time string in the format HH:MM:SS
72 * @li UInt - base 16 (hex) if the string is prefixed with '0x'; base 8 if the string starts with '0'; otherwise
73 * the value is expected to be base 10 (decimal);
74 * @li ByteArray - non-printable bytes and special characters are encoded as numeric character or character entity references;
75 * @li String - non-printable and special characters are encoded as numeric character or character entity references.
77 * Strings and Byte array values can be enclosed in single or double quotes. The IniFile class does this automatically when
78 * saving String or Byte array values with leading or trailing spaces. Quotes are removed from the parameter value prior
79 * returning the value to the application. Use character entity references """ and "'" if quotes should be part of
80 * the parameter value.
82 class COMMON_EXPORT IniFile
87 * Creates the INI file object
88 * @param fileName Name of the INI file
89 * @param mode File opening mode
91 IniFile(QString
const & fileName
, QIODevice::OpenMode mode
= QIODevice::ReadWrite
);
97 * Returns true if the object is valid
99 * The isValid() method returns true if the INI file is can be used. Use this
100 * method after creating the object to verify that opening the INI file in the specified
103 bool isValid() const;
106 * Returns the last error message.
108 * The errorString() method returns a human-readable error message if an operation with the INI file failed.
110 QString
errorString() const;
113 * Reads a value from the INI file.
114 * @param paramName Name of the parameter
115 * @param defaultValue Default value returned if the parameter value cannot be read
116 * @return The value from the INI file or an invalid QVariant if failed
118 * The getValue() method reads a parameter value from the INI file. Parameters are identified by a section and key name pair
119 * in the format '\<section\>/\<key\>'. For example, if the section is called 'general' and the key is called 'log_level',
120 * then the name of the parameter should be given as 'general/log_level'. Parameter names are case insensitive.
122 * If the parameter cannot be read because it is not found in the INI file or the INI file object is invalid, returns the
123 * default value. If reading the parameter fails by any other reasons, returns an invalid QVariant value (QVariant::Invalid).
125 * If the default value is of a valid and known QVariant type, then its type is used to validate and optionally convert
126 * the string read from the INI file. For example:
127 * @li If the value in the INI file is 'abc' and the default value an integer value, then validation
128 * fails and the method returns the default value;
129 * @li If the default value is a boolean value, then the method accepts '1', 'true', 'on' and 'yes' as valid values.
131 * @sa eVaf::Common::toVariant()
133 QVariant
getValue(QByteArray
const & paramName
, QVariant
const & defaultValue
= QVariant::Invalid
);
136 * Writes a value to the INI file.
137 * @param paramName Name of the parameter
138 * @param value The value of the value
139 * @return True if succeeded and false if not
141 * The setValue() method writes a parameter value to the INI file. Parameters are identified by a section and key name pair
142 * in the format '\<section\>/\<key\>'. For example, if the section is called 'general' and the key is called 'log_level',
143 * the the name of the parameter should be given as 'general/log_level'. Parameter names are case insensitive.
145 * The method returns true if the parameter value was written into the INI file and false if not. Use the errorString() method
146 * to get a human-readable error string if writing to the INI file fails.
148 bool setValue(QByteArray
const & paramName
, QVariant
const & value
);
153 /// Pointer to the internal implementation of the class
154 QScopedPointer
<Internal::IniFileImpl
> d
;
158 } // namespace eVaf::Common