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 software. This way, changes can easily be taken over into the INI-file in the right syntax that software expects. In the case of using class Configuration and associated type IniFileFeeder (the intended case!), the following use-cases can be achieved:
In the case that module ALib Camp is included in the ALib Build, the term "writeback" is a token resourced in class Basecamp with key "CFGIniWB"
and thus can be localized (translated to a different language) as appropriate.
alib::variables::Exceptions::ErrorOpeningFile | |
alib::variables::Exceptions::ErrorWritingFile |
Definition at line 117 of file inifile.inl.
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. | |
system::PathString | FileName = nullptr |
The file name. | |
List< MonoAllocator, integer > | LinesWithReadErrors |
List< MonoAllocator, Section > | Sections |
The list of sections. | |
Public Method Index: | |
ALIB_DLL | IniFile () |
Default constructor. | |
IniFile (const system::Path &path) | |
~IniFile () | |
Destructor. | |
ALIB_DLL void | AddComments (String &dest, const String &comments, const String &prefix=A_CHAR("# ")) |
integer | Count () |
ALIB_DLL Entry * | CreateEntry (Section *section, const String &name) |
ALIB_DLL Section * | CreateSection (const String &name) |
Entry * | DeleteEntry (const String §ionName, const String &name) |
ALIB_DLL Entry * | DeleteEntry (Section *section, const String &name) |
ALIB_DLL Section * | DeleteSection (const String &name) |
ALIB_DLL integer | Read (const system::CPathString &path) |
ALIB_DLL void | Reset () |
Clears all data, resets the internal mono allocator. | |
ALIB_DLL std::pair< Section *, Entry * > | SearchEntry (const String §ion, const String &name) |
ALIB_DLL std::pair< Section *, bool > | SearchOrCreateSection (const String §ionName) |
ALIB_DLL Section * | SearchSection (const String §ionName) |
ALIB_DLL void | Write (const system::PathString &path=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 alib::variables::IniFile::Allocator |
A monotonic allocator used for allocating sections and entries.
Definition at line 122 of file inifile.inl.
|
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 217 of file inifile.inl.
String alib::variables::IniFile::FileComments = nullptr |
The file header which will be written out as a comment lines with "# " prefixes.
Definition at line 235 of file inifile.inl.
system::PathString alib::variables::IniFile::FileName = nullptr |
The file name.
Definition at line 232 of file inifile.inl.
List<MonoAllocator, integer> alib::variables::IniFile::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 239 of file inifile.inl.
List<MonoAllocator, Section> alib::variables::IniFile::Sections |
The list of sections.
Definition at line 229 of file inifile.inl.
alib::variables::IniFile::IniFile | ( | ) |
Default constructor.
Definition at line 70 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 249 of file inifile.inl.
|
inline |
Destructor.
Definition at line 253 of file inifile.inl.
void alib::variables::IniFile::AddComments | ( | String & | dest, |
const String & | comments, | ||
const String & | prefix = A_CHAR("# ") ) |
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 178 of file inifile.cpp.
|
inline |
Counts the number of entries over all sections.
Definition at line 273 of file inifile.inl.
IniFile::Entry * alib::variables::IniFile::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 136 of file inifile.cpp.
IniFile::Section * alib::variables::IniFile::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 87 of file inifile.cpp.
|
inline |
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 318 of file inifile.inl.
IniFile::Entry * alib::variables::IniFile::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 120 of file inifile.cpp.
IniFile::Section * alib::variables::IniFile::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 94 of file inifile.cpp.
integer alib::variables::IniFile::Read | ( | const 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( | variables::Exceptions::ErrorOpeningFile ). In the case the module ALib Camp is not included in the ALib Build, then a std::runtime_error("ErrorOpeningFile") may be thrown. |
Definition at line 193 of file inifile.cpp.
void alib::variables::IniFile::Reset | ( | ) |
Clears all data, resets the internal mono allocator.
Definition at line 77 of file inifile.cpp.
std::pair< IniFile::Section *, IniFile::Entry * > alib::variables::IniFile::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 146 of file inifile.cpp.
std::pair< IniFile::Section *, bool > alib::variables::IniFile::SearchOrCreateSection | ( | const String & | sectionName | ) |
Searches the section with the given name.
sectionName | The name of the section to be retrieved. |
Definition at line 169 of file inifile.cpp.
IniFile::Section * alib::variables::IniFile::SearchSection | ( | const String & | sectionName | ) |
Searches the section with the given name.
sectionName | The name of the section to be retrieved. |
Definition at line 155 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 57 of file inifile.cpp.
void alib::variables::IniFile::Write | ( | const system::PathString & | path = 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( | variables::Exceptions::ErrorOpeningFile ). In the case the module ALib Camp is not included in the ALib Build, then a std::runtime_error("ErrorWritingFile") may be thrown. |
Definition at line 378 of file inifile.cpp.