This class implements a rather simple foundation for reading and writing INI-Files.
The design goal was to preserve any user formatting of the INI-file as much as possible. Thus, if the INI-file is written without any modification since it was read from a file, the resulting file should quite exactly in respect comments, values and even whitespaces. Exceptions are:
When read, a linked list of INI-file sections is created and within each section a list of variables is created. The linked lists allow to write sections and their variables in the same order they have been read. In addition to the lists, a hashtable is created which allows finding variables quickly. To find a section, the section list is iterated.
After a file has been read (or also on a blank instance of this class), sections and variables can be manipulated (insert, delete, modify). It is also possible to read one or more files in a sequence and write a merged INI-file back.
This class does not perform any interpretation of the variable values. Neither escape sequences are converted, nor array values parsed or anything. Instead, the "raw" value of each variable, including the equal sign '='
after the variable name is stored. It is up to other types to parse and interpret variable values and to convert a programm's variable values to strings which can be stored in INI-files (ASCII or UTF8 files).
Some remarks on the functionality and supported format:
"//"
, a sharp sign '#'
or a semicolon ';'
are comment lines.'
[' and '
]'.'='
.'\'
.'\'
.Writeback Attribution
This is a non-standard feature of this class. Anywhere in the file, a line with the term writeback
may appear. This flags the next section or variable to be written back by a software. This way, changes can easily be taken over into the INI-file in the right syntax that a software expects. In the case of using class Configuration and associated type IniFileFeeder (the intended case!), the following use-cases can be achieved:
The term "writeback" is a resourced using token "INIWB"
and thus can be localized (translated to a different language) as appropriate.
Definition at line 124 of file inifile.hpp.
#include <inifile.hpp>
Inner Type Index: | |
struct | Entry |
An entry in a Section. More... | |
struct | EntryKey |
Hash functor for nodes hashed in field entryTable. Ignores letter case. More... | |
struct | Section |
A section of the INI-file. More... | |
Public Field Index: | |
MonoAllocator | Allocator |
A monotonic allocator used for allocating sections and entries. | |
String | FileComments = nullptr |
The file header which will be written out as a comment lines with "# " prefixes. | |
lang::system::PathString | FileName = nullptr |
The file name. | |
List< MonoAllocator, integer > | LinesWithReadErrors |
List< MonoAllocator, Section > | Sections |
The list of sections. | |
Public Method Index: | |
ALIB_API | IniFile () |
Default constructor. | |
IniFile (const lang::system::Path &path) | |
~IniFile () | |
Destructor. | |
ALIB_API void | AddComments (String &dest, const String &comments, const String &prefix=A_CHAR("# ")) |
integer | Count () |
ALIB_API Entry * | CreateEntry (Section *section, const String &name) |
ALIB_API Section * | CreateSection (const String &name) |
Entry * | DeleteEntry (const String §ionName, const String &name) |
ALIB_API Entry * | DeleteEntry (Section *section, const String &name) |
ALIB_API Section * | DeleteSection (const String &name) |
ALIB_API integer | Read (const lang::system::CPathString &path) |
ALIB_API void | Reset () |
Clears all data, resets the internal mono allocator. | |
ALIB_API std::pair< Section *, Entry * > | SearchEntry (const String §ion, const String &name) |
ALIB_API std::pair< Section *, bool > | SearchOrCreateSection (const String §ionName) |
ALIB_API Section * | SearchSection (const String §ionName) |
ALIB_API void | Write (const lang::system::PathString &path=lang::system::NULL_PATH) |
Protected Field Index: | |
HashMap< MonoAllocator, EntryKey, std::pair< Section *, Entry * >, EntryKey::Hash, EntryKey::EqualTo > | entryTable |
Protected Method Index: | |
bool | startsWithCommentSymbol (String &subs) |
MonoAllocator Allocator |
A monotonic allocator used for allocating sections and entries.
Definition at line 129 of file inifile.hpp.
|
protected |
The entry hash set. This is used to find entries by section name and entry name. The set contains all entries of all sections.
Definition at line 224 of file inifile.hpp.
String FileComments = nullptr |
The file header which will be written out as a comment lines with "# " prefixes.
Definition at line 242 of file inifile.hpp.
lang::system::PathString FileName = nullptr |
The file name.
Definition at line 239 of file inifile.hpp.
List<MonoAllocator, integer> LinesWithReadErrors |
Filled with faulty line numbers when reading the file. (E.g., when a line is no section, no comment and not the attribute "writeback", but still has no equal sign ('=').
Definition at line 246 of file inifile.hpp.
List<MonoAllocator, Section> Sections |
The list of sections.
Definition at line 236 of file inifile.hpp.
IniFile | ( | ) |
Default constructor.
Definition at line 39 of file inifile.cpp.
|
inline |
Constructs an instance of this class and reads the file specified with path.
path | The filepath to the INI-file. |
Definition at line 256 of file inifile.hpp.
|
inline |
Destructor.
Definition at line 260 of file inifile.hpp.
Parses the comments for newline tokens and adds given comment prefix to each line (in case no known comment prefix is present, yet). Then storage is allocated and output reference dest is set accordingly.
dest | The destination string to set. |
comments | The comments to add. |
prefix | The prefix to use if no other prefixes were found. Defaults to "# " . |
Definition at line 147 of file inifile.cpp.
|
inline |
Counts the number of entries over all sections.
Definition at line 280 of file inifile.hpp.
IniFile::Entry * CreateEntry | ( | Section * | section, |
const String & | name ) |
Creates a new entry. Must be invoked only if the entry does not exist, yet. The given entry name is copied to the internal allocator.
section | The section. |
name | The name of the entry to be created. |
Definition at line 105 of file inifile.cpp.
IniFile::Section * CreateSection | ( | const String & | name | ) |
Appends a new section to the end of the list of sections. Must be invoked only if a section with the same name does not exist, yet.
name | The name of the section. |
Definition at line 56 of file inifile.cpp.
Deletes an entry. This overloaded searches the section by its name first.
sectionName | The section. |
name | The name of the entry to be deleted. |
nullptr
is returned. The entry will still be a valid and accessible object within the monotonic allocator. Definition at line 325 of file inifile.hpp.
IniFile::Entry * DeleteEntry | ( | Section * | section, |
const String & | name ) |
Deletes an entry.
section | The section of the entry. |
name | The name of the entry to be deleted. |
nullptr
is returned. The entry will still be a valid and accessible object within the mono allocator. Definition at line 89 of file inifile.cpp.
IniFile::Section * DeleteSection | ( | const String & | name | ) |
Deletes a section.
name | The name of the section to be deleted. |
nullptr
is returned. The section will still be a valid and accessible object within the mono allocator. Definition at line 63 of file inifile.cpp.
integer Read | ( | const lang::system::CPathString & | path | ) |
Reads an INI-File and adds its contents to the existing data. In case only the new entries should be contained, use method Reset to delete existing data before invoking this function.
It might happen that lines are ignored or otherwise marked as faulty. All numbers of such lines get collected in field LinesWithReadErrors.
Any other i/o errors throws, with the exception of "file not found", which simply returns -1
.
path | The file to read and write. |
-1
if the file does not exist. Otherwise the number of entries read.Exception( | config::Exceptions::ErrorOpeningFile ). |
Definition at line 162 of file inifile.cpp.
void Reset | ( | ) |
Clears all data, resets the internal mono allocator.
Definition at line 46 of file inifile.cpp.
std::pair< IniFile::Section *, IniFile::Entry * > SearchEntry | ( | const String & | section, |
const String & | name ) |
Searches an entry with the given name. The search is performed case insensitive.
section | The name of the section |
name | The name of the entry to be searched. |
Definition at line 115 of file inifile.cpp.
std::pair< IniFile::Section *, bool > SearchOrCreateSection | ( | const String & | sectionName | ) |
Searches the section with the given name.
sectionName | The name of the section to be retrieved. |
Definition at line 138 of file inifile.cpp.
IniFile::Section * SearchSection | ( | const String & | sectionName | ) |
Searches the section with the given name.
sectionName | The name of the section to be retrieved. |
Definition at line 124 of file inifile.cpp.
|
protected |
Tests if the given string starts with '#', ';' or '//'.
subs | The string to test. |
true
if this is a comment line, false
otherwise. Definition at line 26 of file inifile.cpp.
void Write | ( | const lang::system::PathString & | path = lang::system::NULL_PATH | ) |
Writes the data into the file.
path | The file to to write. If this is nulled, the default, then the same file name as with the last Read is used. |
Exception( | config::Exceptions::ErrorOpeningFile ). |
Definition at line 339 of file inifile.cpp.