Adapter type between class IniFile and class Configuration. INI-Files contents usually is fed in with bootstrapping an application and optionally be written out when an application ends (to populate empty INI-files or add new variables to existing ones). For this, an instance of this type is to be locally created each time. In other words, no instance of this type needs to be kept in memory during the life-span of an application.
alib::config::Exceptions::ErrorWritingFile |
Definition at line 26 of file inifilefeeder.hpp.
#include <inifilefeeder.hpp>
Public Field Index: | |
String | DefaultCommentPrefix =A_CHAR("# ") |
int | LineWidth = 100 |
Public Method Index: | |
IniFileFeeder (Configuration &pConfiguration, Priority pPriority=Priority::ConfigFile) | |
virtual | ~IniFileFeeder () |
ALIB_API int | AddResourcedSectionComments (ResourcePool &resourcePool, const NString &resourceCategory, const NString &resourceNamePrefix) |
bool | Export (const String &path) |
ALIB_API bool | Export (const Variable &var) |
void | ExportEnd () |
Closes and deletes the internal iniFile instance without writing an opened INI-file. | |
void | ExportEnd (const lang::system::Path &path) |
IniFile * | ExportStart (const lang::system::Path &path) |
ALIB_API int | ExportSubTree (Configuration::Cursor cursor, bool directChildrenOnly=false) |
int | ExportSubTree (const String &path, bool directChildrenOnly=false) |
IniFile & | GetIniFile () |
ALIB_API int | ImportAll () |
void | ImportEnd () |
Closes and deletes the internal iniFile instance. | |
ALIB_API int | ImportSection (const String §ionName) |
ALIB_API int | ImportSection (const String §ionName, const String &typeName) |
IniFile * | ImportStart (const lang::system::Path &path) |
ALIB_API std::pair< IniFile::Section *, IniFile::Entry * > | SearchEntry (const String &path) |
ALIB_API std::pair< IniFile::Section *, IniFile::Entry * > | SearchEntry (const Variable &variable) |
ALIB_API bool | SetWriteBackFlag (const String &path) |
ALIB_API bool | SetWriteBackFlag (const Variable &var) |
Protected Field Index: | |
Configuration & | configuration |
The configuration to work with. Set with construction. | |
IniFile * | iniFile = nullptr |
The INI-file. Created with methods ImportStart and ExportStart. | |
Priority | priority |
The priority to use to define variables. Set with construction. | |
Protected Method Index: | |
ALIB_API int | importSection (IniFile::Section §ion) |
|
protected |
The configuration to work with. Set with construction.
Definition at line 31 of file inifilefeeder.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 58 of file inifilefeeder.hpp.
|
protected |
The INI-file. Created with methods ImportStart and ExportStart.
Definition at line 37 of file inifilefeeder.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 51 of file inifilefeeder.hpp.
|
protected |
The priority to use to define variables. Set with construction.
Definition at line 34 of file inifilefeeder.hpp.
|
inline |
Constructor.
pConfiguration | Stored in field configuration. |
pPriority | Stored in field priority. ConfigFile. |
Definition at line 66 of file inifilefeeder.hpp.
|
inlinevirtual |
Virtual destructor. Raises an ALib warning in debug-compilations, if the internal INI-file was not disposed before deletion. Disposal is made with methods ImportEnd and ExportEnd.
Definition at line 73 of file inifilefeeder.hpp.
int AddResourcedSectionComments | ( | ResourcePool & | resourcePool, |
const NString & | resourceCategory, | ||
const NString & | resourceNamePrefix ) |
This is a utility function that reads section comments from externalized string resources.
All sections of the underlying INI-file 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 programmatically 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 using text macros like '@HL' to format the text.
resourcePool | The resource pool to use. |
resourceCategory | The category of the resourced comments. |
resourceNamePrefix | A prefix of the resource name. |
Definition at line 253 of file inifilefeeder.cpp.
|
inline |
Creates a variable cursor with the given cfg and path and passes it to overloaded method Export(const Variable&).
path | The path of the variable to export. |
true
if the variable either did not exist yet in the INI-file or if it existed and the write-back mechanism was in place. false
otherwise. Definition at line 193 of file inifilefeeder.hpp.
bool Export | ( | const Variable & | var | ) |
Writes the given variable to the internal INI-file. If an INI-file was previously loaded, only those variables which are not existing in the INI-file are written. This method can thus be used to "populate" an empty INI-file with variables and their resourced or hard-coded default values and this way present all configurable options to an end-user.
Furthermore, a user can specify the word "writeback" (respectively what is resourced with to token "INIWB"
) to force overwriting the INI-file contents with the current value of the variable.
var | The variable to export. |
true
if the variable either did not exist yet in the INI-file or if it existed and the write-back mechanism was in place. false
otherwise. Definition at line 167 of file inifilefeeder.cpp.
|
inline |
Closes and deletes the internal iniFile instance without writing an opened INI-file.
Definition at line 154 of file inifilefeeder.hpp.
|
inline |
Writes the contents of the internal iniFile instance created with ExportStart into an (optionally) different file and then deletes the instance.
path | The file path for writing. |
Definition at line 159 of file inifilefeeder.hpp.
|
inline |
Creates the internal iniFile instance.
path | The filepath to the INI-file. |
Definition at line 136 of file inifilefeeder.hpp.
int ExportSubTree | ( | Configuration::Cursor | cursor, |
bool | directChildrenOnly = false ) |
Writes all variables below the given StringTree::Cursor of the Configuration into the iniFile, previously created with ExportStart. If an INI-file was previously loaded, only those variables which are not existing in the INI-file are written. This method can thus be used to "populate" an empty INI-file with default values and this way present all configurable options to an end-user.
Furthermore, a user can specify the word "writeback" (respectively what is resourced with to token "INIWB"
) to force overwriting the INI-file contents with the current value of the variable.
cursor | A cursor into the configuration's StringTree. |
directChildrenOnly | If true , only the direct children of this cursor are written as variables. Defaults to false . |
Definition at line 214 of file inifilefeeder.cpp.
|
inline |
Invokes ExportSubTree(Configuration::Cursor, bool) by creating a cursor from the given path
path | The path to the subtree (or single variable) to use. |
directChildrenOnly | If true , only the direct children of this path are written as variables. Defaults to false . |
Definition at line 230 of file inifilefeeder.hpp.
|
inline |
Allows access to the internal INI-file object created with either method ImportStart or ExportStart. In debug-compilations, this method asserts that an INI-file was opened. In release-compilations, a non-existing INI-file leads to undefined behavior.
Definition at line 310 of file inifilefeeder.hpp.
int ImportAll | ( | ) |
Imports all entries found INI-file into the configuration. Variables which are already declared and have a lower priority definition, will be set with the value found. Otherwise, a preset string is set for the variable.
Definition at line 146 of file inifilefeeder.cpp.
|
inline |
Closes and deletes the internal iniFile instance.
Definition at line 142 of file inifilefeeder.hpp.
int ImportSection | ( | const String & | sectionName | ) |
Imports all entries in the section named sectionName in the underlying INI-file into the configuration.
sectionName | The name of the section to import. |
Definition at line 128 of file inifilefeeder.cpp.
Imports all entries in the section named sectionName in the underlying INI-file into the configuration. This special version of the method accepts parameter typeName and hence declares the variables in case they have not been declared, yet.
sectionName | The name of the section to import. |
typeName | The type of the variable to declare. |
Definition at line 59 of file inifilefeeder.cpp.
|
protected |
Implements ImportSection.
section | The section to import |
Definition at line 97 of file inifilefeeder.cpp.
|
inline |
Creates the internal iniFile instance.
path | The filepath to the INI-file. |
Definition at line 118 of file inifilefeeder.hpp.
std::pair< IniFile::Section *, IniFile::Entry * > SearchEntry | ( | const String & | path | ) |
Searches the entry of the internal INI-file that represents the given variable determined by parameter path
path | The path of the variable to search in the INI-file. |
Definition at line 31 of file inifilefeeder.cpp.
std::pair< IniFile::Section *, IniFile::Entry * > SearchEntry | ( | const Variable & | variable | ) |
Searches the entry of the internal INI-file that represents the given variable
variable | The variable to search in the INI-file. |
Definition at line 48 of file inifilefeeder.cpp.
bool SetWriteBackFlag | ( | const String & | path | ) |
This method may be used to set the writeback flag of an entry in the associated INI-file. The flag is only set if the entry was programmaticaly created (usually with one of the Export-methods of this class) and if it was not present in the INI-file before.
path | The path of the variable to export. |
true
if the variable did exist and was newly added to the INI-file, false
otherwise. Definition at line 286 of file inifilefeeder.cpp.
bool SetWriteBackFlag | ( | const Variable & | var | ) |
Invokes overloaded method SetWriteBackFlag(const String& path).
var | The variable to modifiy. |
true
if the variable did exist and was newly added to the INI-file, false
otherwise. Definition at line 299 of file inifilefeeder.cpp.