Specialization of class InMemoryPlugin, which reads and writes a simple configuration file consisting of sections containing key/value pairs.
This class is provided for the case that no other configuration mechanism is available. In general, application specific configuration mechanisms already exist in other libraries used. Those should be adopted by creating a lean interface plug-in for ALib .
Some remarks on the functionality and supported format:
"//"
, a sharp sign '#'
or a semicolon ';'
are comment lines.'#'
. If this is changed in the file, such changes are preserved.'
[' and '
]'.'='
.'\'
.'\'
.'\'
.' '
and tabulators '\t'
are ignored at the start and end of each line and before and after the equal sign '='
.'\ '
or the whole value has to be surrounded by double quotes "
.'\'
."\\a"
is read as 'a'
.Definition at line 100 of file inifile.hpp.
#include <inifile.hpp>
Public Static Field Index: | |
static String | DefaultFileExtension = A_CHAR(".ini") |
Public Static Method Index: | |
static ALIB_API void | AddResourcedSectionComments (Configuration &config, ResourcePool &resourcePool, const NString &resourceCategory, const NString &resourceNamePrefix) |
Public Field Index: | |
bool | AutoSave = false |
String | DefaultCommentPrefix =A_CHAR("# ") |
AString | FileComments |
AString | FileName |
bool | FormatIncludeDelimInAttrAlignment = false |
bool | FormatSpaceAfterDelim = true |
bool | FormatSpaceBeforeDelim = false |
List< integer > | LinesWithReadErrors |
int | LineWidth = 100 |
Public Field Index: inherited from ConfigurationPlugin | |
XTernalizer * | StringConverter |
Public Method Index: | |
ALIB_API | IniFile (const String &filePathAndName=nullptr) |
virtual | ~IniFile () override |
virtual ALIB_API void | Clear () override |
virtual ALIB_API void | FromVariable (Entry &entry, Variable &variable) const override |
virtual ALIB_API bool | Load (Variable &variable, bool searchOnly=false) override |
virtual String | Name () const override |
ALIB_API void | ReadFile () |
virtual bool | Store (Variable &variable) override |
virtual ALIB_API void | ToVariable (Entry &entry, Variable &variable) const override |
ALIB_API void | WriteFile () |
Public Method Index: inherited from InMemoryPlugin | |
ALIB_API | InMemoryPlugin (String pName, MonoAllocator *externalMA=nullptr) |
virtual ALIB_API | ~InMemoryPlugin () override |
virtual ALIB_API Iterator * | GetIterator (const String §ionName) override |
Entry * | SearchEntry (const String §ion, const String &name) |
Entry * | SearchEntry (Section *section, const String &name) |
ALIB_API std::pair< Section *, bool > | SearchOrCreateSection (const String §ionName) |
ALIB_API const Section * | SearchSection (const String §ionName) |
const List< Section > & | Sections () const |
virtual bool | Store (Variable &variable, const String &externalizedValue) |
Public Method Index: inherited from ConfigurationPlugin | |
virtual | ~ConfigurationPlugin () |
bool AutoSave = false |
If this is set to true
, any variable change will lead to writing the file immediately by invoking WriteFile. Defaults to false
Definition at line 109 of file inifile.hpp.
The prefix that is used for comment lines of sections or variables that have been added 'in code' (variables that have not been read from the file). Comments that were read from the file preserve their prefix. If comments including one of the valid prefixes are added to a variable or section 'in code', such prefix is preserved.
Definition at line 136 of file inifile.hpp.
The standard file extension used for ALib configuration files. Defaults to ".ini".
Definition at line 113 of file inifile.hpp.
AString FileComments |
The file header which will be written out as a comment lines with "# " prefixes
Definition at line 120 of file inifile.hpp.
AString FileName |
The file name. This might include a path or not. Should be set properly before the file is read.
Definition at line 117 of file inifile.hpp.
bool FormatIncludeDelimInAttrAlignment = false |
Denotes whether the spaces that are inserted when aligning attributes are located before or behind the delimiter.
Definition at line 147 of file inifile.hpp.
bool FormatSpaceAfterDelim = true |
Denotes if a space should be written after a delimiter. (Applies only to single line mode of writing attributes.)
Definition at line 143 of file inifile.hpp.
bool FormatSpaceBeforeDelim = false |
Denotes if a space should be written before a delimiter.
Definition at line 139 of file inifile.hpp.
Is cleared and filled with faulty line numbers when reading the file. (E.g. when a line is no section and no comment but still has no equal sign ('=').
Definition at line 128 of file inifile.hpp.
int LineWidth = 100 |
The desired maximum width of the INI-file. Defaults to 100
. This value is used with utility method AddResourcedSectionComments
Definition at line 124 of file inifile.hpp.
Constructs an instance of this class and reads the file. If no file name is given, the file name is set to the process name with extension found in public static field DefaultFileExtension.
If the given file name equals '*'
, no file is read and field AutoSave is set to false
.
If the given name does not start with either a path separation character or a dot character .
, then Directory::SpecialFolder::HomeConfig is prepended to the given name.
filePathAndName | The name (and path) of the file to read and write. Provide "*" to suppress reading a file. Defaults to nullptr. |
Definition at line 161 of file inifile.cpp.
|
inlineoverridevirtual |
|
static |
This is a static utility function that reads section comments from externalized string resources.
All sections of all INI-files of given config are processed, but resourced comments are only added in the case that a section's comment string is nulled
. This is not the case if a section was read from from an INI-file, as even if no comments are given, the field is empty, but not nulled. In other words, only sections that have been programatically added during the run of a software are changed
This approach allows a user to remove the comments, without the software restoring them.
The resource names are assembled from given resourceNamePrefix and the section name. The resource strings found are processed using method Paragraphs.AddMarked . This allows to use text macros like '@HL' to format the text.
This method is best be invoked in phase ShutdownPhases::Announce of method Camp::shutdown .
config | The configuration that is searched for INI-file plug-ins. |
resourcePool | The resource pool to use. |
resourceCategory | The category of the resourced comments. |
resourceNamePrefix | A prefix of the resource name. |
Definition at line 616 of file inifile.cpp.
|
overridevirtual |
Clears all configuration data.
Reimplemented from InMemoryPlugin.
Definition at line 203 of file inifile.cpp.
Overrides default method. Clears the raw value, and calls base method.
entry | The entry to convert. |
variable | The variable to fill with our values. |
Reimplemented from InMemoryPlugin.
Definition at line 151 of file inifile.cpp.
|
overridevirtual |
Searches the variable in our storage.
variable | The variable to retrieve. |
searchOnly | If true , the variable is not read. Defaults to false . |
true
if variable was found, false
if not. Reimplemented from InMemoryPlugin.
Definition at line 445 of file inmemoryplugin.cpp.
|
inlineoverridevirtual |
Return the plug-in name, in this case, the file name.
Reimplemented from InMemoryPlugin.
Definition at line 250 of file inifile.hpp.
void ReadFile | ( | ) |
Clears all configuration data and reads the file. It might happen that lines are ignored or otherwise marked as faulty. All numbers of such lines get collected in field LinesWithReadErrors.
Exception( | config::Exceptions::ErrorOpeningFile ). |
Definition at line 225 of file inifile.cpp.
|
inlineoverridevirtual |
Creates or replaces existing variable in our storage. If AutoSave is set, the file is written
variable | The variable to retrieve. |
true
if the variable was written, false
if not. The latter might only happen if the variable given was illegal, e.g. empty name. Reimplemented from InMemoryPlugin.
Definition at line 263 of file inifile.hpp.
Overrides default method. If we have not parsed the INI file's text value, yet, we do this now.
entry | The entry to convert. |
variable | The variable to fill with our values. |
Reimplemented from InMemoryPlugin.
Definition at line 60 of file inifile.cpp.
|
protected |
Writes a list of comments to the file. Comment lines are started with '#'.
os | The encapsulated output stream to write to. |
comments | The comment lines for the section. |
Definition at line 363 of file inifile.cpp.
void WriteFile | ( | ) |
Write all configuration data into the file.
Exception( | config::Exceptions::ErrorOpeningFile ). |
Definition at line 400 of file inifile.cpp.