]> vaikene.ee Git - evaf/blob - src/libs/Common/inifile.h
Mac OS changes and switched to c++11.
[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-2019 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 #include <QScopedPointer>
29
30 namespace eVaf {
31 namespace Common {
32 namespace Internal {
33 class IniFileImpl;
34 } // namespace eVaf::Common::Internal
35
36 /**
37 * Class for reading and writing parameter values in INI files.
38 *
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.
43 *
44 * INI files can contain comment lines that start with ';' or '#'. Comments are
45 * preserved when new values are written to the INI file.
46 *
47 * This is a sample INI %file:
48 * @code
49 * [main]
50 * # The full name of this parameter is 'main/name'
51 * name = "My Application"
52 *
53 * # 4-byte binary version number 1.0.7.10 with the name 'main/version'
54 * version = &#x01;&#x00;&#x07;&#x0a;
55 *
56 * [extensions/about]
57 * # The full name of this parameter is 'extensions/about/module'
58 * module = libabout.so
59 * @endcode
60 *
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:
64 *
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.
76 *
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 "&quot;" and "&apos;" if quotes should be part of
80 * the parameter value.
81 */
82 class COMMON_EXPORT IniFile
83 {
84 public:
85
86 /**
87 * Creates the INI file object
88 * @param fileName Name of the INI file
89 * @param mode File opening mode
90 */
91 IniFile(QString const & fileName, QIODevice::OpenMode mode = QIODevice::ReadWrite);
92
93 /// Dtor
94 ~IniFile();
95
96 /**
97 * Returns true if the object is valid
98 *
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
101 * mode succeeded.
102 */
103 bool isValid() const;
104
105 /**
106 * Returns the last error message.
107 *
108 * The errorString() method returns a human-readable error message if an operation with the INI file failed.
109 */
110 QString errorString() const;
111
112 /**
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
117 *
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.
121 *
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).
124 *
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.
130 *
131 * @sa eVaf::Common::toVariant()
132 */
133 QVariant getValue(QByteArray const & paramName, QVariant const & defaultValue = QVariant::Invalid);
134
135 /**
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
140 *
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.
144 *
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.
147 */
148 bool setValue(QByteArray const & paramName, QVariant const & value);
149
150
151 private:
152
153 /// Pointer to the internal implementation of the class
154 QScopedPointer<Internal::IniFileImpl> d;
155
156 };
157
158 } // namespace eVaf::Common
159 } // namespace eVaf
160
161 #endif // inifile.h