]> vaikene.ee Git - evaf/blob - src/libs/Common/inifile.h
Started working on the IniFile class.
[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 namespace eVaf {
26 namespace Common {
27 namespace Internal {
28 class IniFileImpl;
29 } // namespace eVaf::Common::Internal
30
31 /**
32 * Class for reading and writing parameter values in INI files.
33 *
34 * The IniFile class provides access to parameter values in a standard INI file. Every parameter value is
35 * identified by a section/key name pair. Key names can be prefixed with 'windows:' or 'linux:' if the
36 * parameter value is specific for the given platform. This allows entering platform-specific parameter
37 * values to the INI file. INI files can contain comment lines that start with ';' or '#'. Comments are
38 * preserved when new values are written to the INI file.
39 *
40 * Values in INI files are stored as strings and the IniFile class expects them to use specific formats if
41 * the returned value is supposed to be of a different data type. The following list shows the expected format
42 * of different data types:
43 *
44 * @li Bool - '0', 'false', 'off', 'no' are equal to false and '1', 'true', 'on', 'yes' are equal to true;
45 * @li Char - a single character or an ASCII code as '\0NNN' or '\0xNN';
46 * @li Date - date string in the ISO 8601 format YYYY-MM-DD;
47 * @li DateTime - date and time string in the ISO 8601 format YYY-MM-DDTHH:MM:SSTZD;
48 * @li Double - the decimal point is always '.' regardless of the locale;
49 * @li Int - only base 10 (decimal) is allowed;
50 * @li Time - 24h time string in the format HH:MM:SS
51 * @li UInt - base 16 (hex) if the string is prefixed with '0x'; base 8 if the string starts with '0'; otherwise
52 * the value is expected to be base 10 (decimal).
53 */
54 class COMMON_EXPORT IniFile
55 {
56 public:
57
58 /**
59 * Creates the INI file object
60 * @param fileName Name of the INI file
61 * @param mode File opening mode
62 */
63 IniFile(QString const & fileName, QIODevice::OpenMode mode = QIODevice::ReadWrite);
64
65 /// Dtor
66 ~IniFile();
67
68 /**
69 * Returns true if the object is valid
70 *
71 * The isValid() method returns true if the INI file is can be used. Use this
72 * method after creating the object to verify that opening the INI file in the specified
73 * mode succeeded.
74 *
75 * If the object is not valid, then:
76 * @li Writing to the INI file always fails; the internal cache is still updated and reading the parameter returns
77 * the new value;
78 * @li Reading from the INI file returns the cached value or the default value if no values with this name are written.
79 */
80 bool isValid() const;
81
82 /**
83 * Returns the last error message.
84 *
85 * The errorString() method returns a human-readable error message if an operation with the INI file failed.
86 */
87 QString errorString() const;
88
89 /**
90 * Reads a value from the INI file.
91 * @param paramName Name of the parameter
92 * @param defaultValue Default value returned if the parameter value cannot be read
93 * @return The value from the INI file or an invalid QVariant if failed
94 *
95 * The getValue() method reads a parameter value from the INI file. Parameters are identified by a section and key name pair
96 * in the format '<section>/<key&gt'. For example, if the section is called 'general' and the key is called 'log_level',
97 * then the name of the parameter should be given as 'general/log_level'. Parameter names are case insensitive.
98 *
99 * If the parameter cannot be read because it is not found in the INI file or the INI file object is invalid, returns the
100 * default value. If reading the parameter fails by any other reasons, returns an invalid QVariant value (QVariant::Invalid).
101 *
102 * If the default value is of a valid and known QVariant type, then its type is used to validate and optionally convert
103 * the string read from the INI file. For example:
104 * @li If the value in the INI file is 'abc' and the default value an integer value, then validation
105 * fails and the method returns the default value;
106 * @li If the default value is a boolean value, then the method accepts '1', 'true', 'on' and 'yes' as valid values.
107 */
108 QVariant getValue(QString const & paramName, QVariant const & defaultValue = QVariant::Invalid);
109
110 /**
111 * Writes a value to the INI file.
112 * @param paramName Name of the parameter
113 * @param value The value of the value
114 * @return True if succeeded and false if not
115 *
116 * The setValue() method writes a parameter value to the INI file. Parameters are identified by a section and key name pair
117 * in the format '<section>/<key>'. For example, if the section is called 'general' and the key is called 'log_level',
118 * the the name of the parameter should be given as 'general/log_level'. Parameter names are case insensitive.
119 *
120 * The method returns true if the parameter value was written into the INI file and false if not. Use the errorString() method
121 * to get a human-readable error string if writing to the INI file fails.
122 *
123 * Writing to an invalid INI file always fails, but the value is still stored into the internal cache. Readin the same parameter
124 * value returns the new value even if it was actually not stored into the INI file.
125 */
126 bool setValue(QString const & paramName, QVariant const & value);
127
128
129 private:
130
131 /// Pointer to the internal implementation of the class
132 Internal::IniFileImpl * d;
133
134 };
135
136 } // namespace eVaf::Common
137 } // namespace eVaf
138
139 #endif // INIFILE_H