2 * @file Common/inifile_p.h
3 * @brief Internal implementation of the class for reading and writing parameter values in INI files.
6 * Copyright (c) 2011 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_P_H
21 #define __COMMON_INIFILE_P_H
23 #include <QSharedData>
24 #include <QExplicitlySharedDataPointer>
37 * INI file value in the internal cache
39 class IniFileValue
: public QSharedData
43 IniFileValue(quint64 pos
)
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.
58 * Key name of the parameter
63 * Value from the INI file
68 * Flag indicating that this value is valid on this OS only
74 * INI file section in the internal cache
76 class IniFileSection
: public QSharedData
80 IniFileSection(quint64 pos
)
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.
99 * List of all the known parameter values in this section
101 * The key to the hash table is the name of the key for the parameters value.
103 QHash
<QString
, QExplicitlySharedDataPointer
<IniFileValue
> > values
;
108 * Internal implementation of the IniFile class.
114 IniFileImpl(QString
const & fileName
, QIODevice::OpenMode mode
);
118 QVariant
getValue(QString
const & paramName
, QVariant
const & defaultValue
);
120 bool setValue(QString
const & paramName
, QVariant
const & value
);
122 inline bool isValid() const { return mValid
; }
124 inline QString
const & fileName() const { return mFileName
; }
126 inline QString
const & errorString() const { return mErrorString
; }
131 /// Flag indicating that the INI file object is valid and can be used to access the INI file
134 /// Name of the INI file
137 /// INI file opening mode
138 QIODevice::OpenMode mMode
;
140 /// Last human-readable error message if an operation with the INI file failed
141 QString mErrorString
;
144 * Internal cache of sections and parameter values.
146 * The key to the hash table is the name of the section.
148 QHash
<QString
, QExplicitlySharedDataPointer
<IniFileSection
> > mCache
;
150 /// When was the INI file modified.
151 QDateTime mLastModified
;
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
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.
165 void updateCache(quint64 pos
, qint64 diff
);
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
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.
176 * If the section is not found, returns an invalid section object and seeks the file to the end of the file.
178 * The file object is expected to be opened if the mValid flag is true. If the mValid flag is false, looks
181 QExplicitlySharedDataPointer
<IniFileSection
> getSection(QFile
& file
, QString
const & sectionName
);
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
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.
193 * If not found, returns an invalid value object and seeks the file to the end of the section.
195 * The file object is expected to be opened if the mValid flag is true. If the mValid flag is false, looks
198 QExplicitlySharedDataPointer
<IniFileValue
> getParameter(QFile
& file
, IniFileSection
& section
, QString
const & paramName
);
203 } // namespace eVaf::Common::Internal
204 } // namespace eVaf::Common
207 #endif // inifile_p.h