]> vaikene.ee Git - evaf/blob - src/libs/Common/inifile.h
ecf873768a37ff0b51413fcb5596ca347d334217
[evaf] / src / libs / Common / inifile.h
1 /**
2 * @file Common/inifile.h
3 * @brief Class for reading and writing parameter values in INI files.
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
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.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #ifndef __COMMON_INIFILE_H
21 #define __COMMON_INIFILE_H
22
23 #include "libcommon.h"
24
25 #include <QString>
26 #include <QVariant>
27 #include <QIODevice>
28
29 namespace eVaf {
30 namespace Common {
31 namespace Internal {
32 class IniFileImpl;
33 } // namespace eVaf::Common::Internal
34
35 /**
36 * Class for reading and writing parameter values in INI files.
37 *
38 * The IniFile class provides access to parameter values in a standard INI file.
39 * Every parameter value is identified by a section/key name pair. Key names can be prefixed with
40 * 'windows:' or 'linux:' if the parameter value is specific for the given platform.
41 * This allows entering platform-specific parameter values to the INI file.
42 *
43 * INI files can contain comment lines that start with ';' or '#'. Comments are
44 * preserved when new values are written to the INI file.
45 *
46 * This is a sample INI %file:
47 * @code
48 * [main]
49 * # The full name of this parameter is 'main/name'
50 * name = "My Application"
51 *
52 * # 4-byte binary version number 1.0.7.10 with the name 'main/version'
53 * version = &#x01;&#x00;&#x07;&#x0a;
54 *
55 * [extensions/about]
56 * # The full name of this parameter is 'extensions/about/module'
57 * module = libabout.so
58 * @endcode
59 *
60 * Values in INI files are stored as strings and the IniFile class expects them to use specific formats if
61 * the returned value is supposed to be of a different data type. The following list shows the expected format
62 * of different data types:
63 *
64 * @li Bool - '0', 'false', 'off', 'no' are equal to false and '1', 'true', 'on', 'yes' are equal to true;
65 * @li Char - a single character or an UTF-16 code as '\0NNNNNN' (oct) or '\0xNNNN' (hex);
66 * @li Date - date string in the ISO 8601 format YYYY-MM-DD;
67 * @li DateTime - date and time string in the ISO 8601 format YYY-MM-DDTHH:MM:SSTZD;
68 * @li Double - the decimal point is always '.' regardless of the locale;
69 * @li Int - only base 10 (decimal) is allowed;
70 * @li Time - 24h time string in the format HH:MM:SS
71 * @li UInt - base 16 (hex) if the string is prefixed with '0x'; base 8 if the string starts with '0'; otherwise
72 * the value is expected to be base 10 (decimal);
73 * @li ByteArray - non-printable bytes and special characters are encoded as numeric character or character entity references;
74 * @li String - non-printable and special characters are encoded as numeric character or character entity references.
75 *
76 * Strings and Byte array values can be enclosed in single or double quotes. The IniFile class does this automatically when
77 * saving String or Byte array values with leading or trailing spaces. Quotes are removed from the parameter value prior
78 * returning the value to the application. Use character entity references "&quot;" and "&apos;" if quotes should be part of
79 * the parameter value.
80 */
81 class COMMON_EXPORT IniFile
82 {
83 public:
84
85 /**
86 * Creates the INI file object
87 * @param fileName Name of the INI file
88 * @param mode File opening mode
89 */
90 IniFile(QString const & fileName, QIODevice::OpenMode mode = QIODevice::ReadWrite);
91
92 /// Dtor
93 ~IniFile();
94
95 /**
96 * Returns true if the object is valid
97 *
98 * The isValid() method returns true if the INI file is can be used. Use this
99 * method after creating the object to verify that opening the INI file in the specified
100 * mode succeeded.
101 */
102 bool isValid() const;
103
104 /**
105 * Returns the last error message.
106 *
107 * The errorString() method returns a human-readable error message if an operation with the INI file failed.
108 */
109 QString errorString() const;
110
111 /**
112 * Reads a value from the INI file.
113 * @param paramName Name of the parameter
114 * @param defaultValue Default value returned if the parameter value cannot be read
115 * @return The value from the INI file or an invalid QVariant if failed
116 *
117 * The getValue() method reads a parameter value from the INI file. Parameters are identified by a section and key name pair
118 * in the format '\<section\>/\<key\>'. For example, if the section is called 'general' and the key is called 'log_level',
119 * then the name of the parameter should be given as 'general/log_level'. Parameter names are case insensitive.
120 *
121 * If the parameter cannot be read because it is not found in the INI file or the INI file object is invalid, returns the
122 * default value. If reading the parameter fails by any other reasons, returns an invalid QVariant value (QVariant::Invalid).
123 *
124 * If the default value is of a valid and known QVariant type, then its type is used to validate and optionally convert
125 * the string read from the INI file. For example:
126 * @li If the value in the INI file is 'abc' and the default value an integer value, then validation
127 * fails and the method returns the default value;
128 * @li If the default value is a boolean value, then the method accepts '1', 'true', 'on' and 'yes' as valid values.
129 *
130 * @sa eVaf::Common::toVariant()
131 */
132 QVariant getValue(QByteArray const & paramName, QVariant const & defaultValue = QVariant::Invalid);
133
134 /**
135 * Writes a value to the INI file.
136 * @param paramName Name of the parameter
137 * @param value The value of the value
138 * @return True if succeeded and false if not
139 *
140 * The setValue() method writes a parameter value to the INI file. Parameters are identified by a section and key name pair
141 * in the format '\<section\>/\<key\>'. For example, if the section is called 'general' and the key is called 'log_level',
142 * the the name of the parameter should be given as 'general/log_level'. Parameter names are case insensitive.
143 *
144 * The method returns true if the parameter value was written into the INI file and false if not. Use the errorString() method
145 * to get a human-readable error string if writing to the INI file fails.
146 */
147 bool setValue(QByteArray const & paramName, QVariant const & value);
148
149
150 private:
151
152 /// Pointer to the internal implementation of the class
153 Internal::IniFileImpl * d;
154
155 };
156
157 } // namespace eVaf::Common
158 } // namespace eVaf
159
160 #endif // inifile.h