]> vaikene.ee Git - evaf/blob - src/libs/Common/inifile_p.h
b7826270f25239bc64c0013f0949d7c031ee54cc
[evaf] / src / libs / Common / inifile_p.h
1 /**
2 * @file Common/inifile_p.h
3 * @brief Internal implementation of the 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_P_H
21 #define __COMMON_INIFILE_P_H
22
23 #include <QSharedData>
24 #include <QExplicitlySharedDataPointer>
25 #include <QHash>
26 #include <QString>
27 #include <QVariant>
28 #include <QDateTime>
29 #include <QFile>
30
31
32 namespace eVaf {
33 namespace Common {
34 namespace Internal {
35
36 /**
37 * INI file value in the internal cache
38 */
39 class IniFileValue : public QSharedData
40 {
41 public:
42
43 IniFileValue(quint64 pos)
44 : QSharedData()
45 , filePos(pos)
46 , thisOsOnly(false)
47 {}
48
49 /**
50 * File position
51 *
52 * Offset of the parameter in the INI file. By seeking the file to this offset value,
53 * the next character read or written will be the beginning of the key name.
54 */
55 quint64 filePos;
56
57 /**
58 * Key name of the parameter
59 */
60 QString name;
61
62 /**
63 * Value from the INI file
64 */
65 QString paramValue;
66
67 /**
68 * Flag indicating that this value is valid on this OS only
69 */
70 bool thisOsOnly;
71 };
72
73 /**
74 * INI file section in the internal cache
75 */
76 class IniFileSection : public QSharedData
77 {
78 public:
79
80 IniFileSection(quint64 pos)
81 : QSharedData()
82 , filePos(pos)
83 {}
84
85 /**
86 * File position
87 *
88 * Offset of the section in the INI file. By seeking the file to this offset value,
89 * the next character read or written will be the first character of the section.
90 */
91 quint64 filePos;
92
93 /**
94 * Name of the section
95 */
96 QString name;
97
98 /**
99 * List of all the known parameter values in this section
100 *
101 * The key to the hash table is the name of the key for the parameters value.
102 */
103 QHash<QString, QExplicitlySharedDataPointer<IniFileValue> > values;
104
105 };
106
107 /**
108 * Internal implementation of the IniFile class.
109 */
110 class IniFileImpl
111 {
112 public:
113
114 IniFileImpl(QString const & fileName, QIODevice::OpenMode mode);
115
116 ~IniFileImpl();
117
118 QVariant getValue(QString const & paramName, QVariant const & defaultValue);
119
120 bool setValue(QString const & paramName, QVariant const & value);
121
122 inline bool isValid() const { return mValid; }
123
124 inline QString const & fileName() const { return mFileName; }
125
126 inline QString const & errorString() const { return mErrorString; }
127
128
129 private: // Members
130
131 /// Flag indicating that the INI file object is valid and can be used to access the INI file
132 bool mValid;
133
134 /// Name of the INI file
135 QString mFileName;
136
137 /// INI file opening mode
138 QIODevice::OpenMode mMode;
139
140 /// Last human-readable error message if an operation with the INI file failed
141 QString mErrorString;
142
143 /**
144 * Internal cache of sections and parameter values.
145 *
146 * The key to the hash table is the name of the section.
147 */
148 QHash<QString, QExplicitlySharedDataPointer<IniFileSection> > mCache;
149
150 /// When was the INI file modified.
151 QDateTime mLastModified;
152
153
154 private: /// Methods
155
156 /**
157 * Updates items in the internal cache after changes to the INI file
158 * @param pos File offset from where the cache should be updated
159 * @param diff Difference between old and new file offsets
160 *
161 * When a parameter value is modified or a new value inserted, the length of the INI file changes.
162 * Sections and parameters that come after the modified parameter value are shifted and their file
163 * positions changed. This method updates items in the internal cache after changes to the INI file.
164 */
165 void updateCache(quint64 pos, qint64 diff);
166
167 /**
168 * Looks for a section in the INI file
169 * @param file The file object
170 * @param sectionName Name of the section
171 * @return The section object or an invalid object if not found
172 *
173 * This method reads the INI file and looks for the section with the given name. If found, returns the section
174 * object and seeks the file to the first character after the section name.
175 *
176 * If the section is not found, returns an invalid section object and seeks the file to the end of the file.
177 *
178 * The file object is expected to be opened if the mValid flag is true. If the mValid flag is false, looks
179 * only in the cache.
180 */
181 QExplicitlySharedDataPointer<IniFileSection> getSection(QFile & file, QString const & sectionName);
182
183 /**
184 * Looks for a parameter in the INI file
185 * @param file The file object
186 * @param section The section object
187 * @param paramName Name of the parameter
188 * @return The value object or an invalid object if not found
189 *
190 * This method reads the INI file and looks for the parameter with the given name. If found, returns the value
191 * object and seeks the file to the beginning of the line containing the parameter.
192 *
193 * If not found, returns an invalid value object and seeks the file to the end of the section.
194 *
195 * The file object is expected to be opened if the mValid flag is true. If the mValid flag is false, looks
196 * only in the cache.
197 */
198 QExplicitlySharedDataPointer<IniFileValue> getParameter(QFile & file, IniFileSection & section, QString const & paramName);
199
200 };
201
202
203 } // namespace eVaf::Common::Internal
204 } // namespace eVaf::Common
205 } // namespace eVaf
206
207 #endif // inifile_p.h