ALib C++ Library
Library Version: 2312 R0
Documentation generated by doxygen
Public Static Fields | Public Fields | Public Static Methods | Public Methods | Protected Methods | List of all members
IniFile Class Reference

#include <inifile.hpp>

Inheritance diagram for IniFile:
[legend]
Collaboration diagram for IniFile:
[legend]

Class Description


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:

Reference Documentation

Exceptions
aworx::lib::config::Exceptions

Definition at line 104 of file inifile.hpp.

Public Static Fields

static String DefaultFileExtension = A_CHAR(".ini")
 

Public Fields

bool AutoSave = false
 
String DefaultCommentPrefix =A_CHAR("# ")
 
AString FileComments
 
AString FileName
 
bool FormatIncludeDelimInAttrAlignment = false
 
bool FormatSpaceAfterDelim = true
 
bool FormatSpaceBeforeDelim = false
 
List< integerLinesWithReadErrors
 
int LineWidth = 100
 
- Public Fields inherited from ConfigurationPlugin
XTernalizerStringConverter
 

Public Static Methods

static ALIB_API void AddResourcedSectionComments (Configuration &config, ResourcePool &resourcePool, const NString &resourceCategory, const NString &resourceNamePrefix)
 

Public Methods

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 ALIB_API bool Store (Variable &variable) override
 
virtual bool Store (Variable &variable) override
 
virtual ALIB_API void ToVariable (Entry &entry, Variable &variable) const override
 
ALIB_API void WriteFile ()
 
- Public Methods inherited from InMemoryPlugin
ALIB_API InMemoryPlugin (String pName, MonoAllocator *externalMA=nullptr)
 
virtual ALIB_API ~InMemoryPlugin () override
 
virtual ALIB_API IteratorGetIterator (const String &sectionName) override
 
virtual ALIB_API bool Load (Variable &variable, bool searchOnly=false) override
 
virtual ALIB_API bool Load (Variable &variable, bool searchOnly=false)=0
 
EntrySearchEntry (const String &section, const String &name)
 
EntrySearchEntry (Section *section, const String &name)
 
ALIB_API std::pair< Section *, bool > SearchOrCreateSection (const String &sectionName)
 
const ALIB_API SectionSearchSection (const String &sectionName)
 
const List< Section > & Sections () const
 
virtual bool Store (Variable &variable)
 
virtual bool Store (Variable &variable, const String &externalizedValue)
 
- Public Methods inherited from ConfigurationPlugin
virtual ~ConfigurationPlugin ()
 
virtual bool Store (Variable &variable, const String &externalizedValue)
 

Protected Methods

ALIB_API void writeComments (strings::compatibility::std::StringWriter &os, const AString &comments)
 
- Protected Methods inherited from InMemoryPlugin
ALIB_API EntrycreateEntry (Section *section, const String &name)
 
virtual SectioncreateSection (const String &sectionName)
 
EntrysearchEntry (const String &section, const String &name)
 
- Protected Methods inherited from ConfigurationPlugin
 ConfigurationPlugin ()
 

Additional Inherited Members

- Protected Types inherited from InMemoryPlugin
using TEntryList = List< Entry, Recycling::Shared >
 
using TValueList = List< String32, Recycling::Shared >
 
- Protected Fields inherited from InMemoryPlugin
MonoAllocatorallocator
 
TEntryList::TSharedRecycler entryRecycler
 
monomem::HashMap< EntryKey, std::pair< Section *, TEntryList::Iterator >, EntryKey::Hash, EntryKey::EqualToentryTable
 
bool isMAOwner
 
String plugInName
 
List< Sectionsections
 
TValueList::TSharedRecycler valueRecycler
 
- Protected Fields inherited from ConfigurationPlugin
XTernalizer defaultStringConverter
 

Constructor & Destructor Documentation

◆ IniFile()

IniFile ( const String filePathAndName = nullptr)

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.

Parameters
filePathAndNameThe 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.

Here is the call graph for this function:

◆ ~IniFile()

virtual ~IniFile ( )
inlineoverridevirtual

Virtual Destructor.

Definition at line 178 of file inifile.hpp.

Member Function Documentation

◆ AddResourcedSectionComments()

void AddResourcedSectionComments ( Configuration config,
ResourcePool resourcePool,
const NString resourceCategory,
const NString resourceNamePrefix 
)
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 Module::shutdown.

See also
Field LineWidth, which is respected when formatting comment lines.
Parameters
configThe configuration that is searched for INI-file plug-ins.
resourcePoolThe resource pool to use.
resourceCategoryThe category of the resourced comments.
resourceNamePrefixA prefix of the resource name.

Definition at line 616 of file inifile.cpp.

Here is the call graph for this function:

◆ Clear()

void Clear ( )
overridevirtual

Clears all configuration data.

Reimplemented from InMemoryPlugin.

Definition at line 203 of file inifile.cpp.

Here is the call graph for this function:

◆ FromVariable()

void FromVariable ( Entry entry,
Variable variable 
) const
overridevirtual

Overrides default method. Clears the raw value, and calls base method.

Parameters
entryThe entry to convert.
variableThe variable to fill with our values.

Reimplemented from InMemoryPlugin.

Definition at line 151 of file inifile.cpp.

Here is the call graph for this function:

◆ Load()

bool Load
override

Searches the variable in our storage.

Parameters
variableThe variable to retrieve.
searchOnlyIf true, the variable is not read. Defaults to false.
Returns
true if variable was found, false if not.

Definition at line 80 of file inmemoryplugin.cpp.

◆ Name()

virtual String Name ( ) const
inlineoverridevirtual

Return the plug-in name, in this case, the file name.

Returns
The name of the plug-in.

Reimplemented from InMemoryPlugin.

Definition at line 254 of file inifile.hpp.

◆ ReadFile()

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.

Exceptions
Exception(config::Exceptions::ErrorOpeningFile ).

Definition at line 225 of file inifile.cpp.

Here is the call graph for this function:

◆ Store() [1/2]

bool Store
override

Creates or replaces existing variable in our storage. If the variable has no value, an existing entry is deleted.

Parameters
variableThe variable to retrieve.
Returns
true if the variable was written, false if not. The latter might only happen if the variable given was illegal, e.g. empty name.

Definition at line 90 of file inmemoryplugin.cpp.

◆ Store() [2/2]

virtual bool Store ( Variable variable)
inlineoverridevirtual

Creates or replaces existing variable in our storage. If AutoSave is set, the file is written

Parameters
variableThe variable to retrieve.
Returns
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 267 of file inifile.hpp.

Here is the call graph for this function:

◆ ToVariable()

void ToVariable ( Entry entry,
Variable variable 
) const
overridevirtual

Overrides default method. If we have not parsed the INI file's text value, yet, we do this now.

Parameters
entryThe entry to convert.
variableThe variable to fill with our values.

Reimplemented from InMemoryPlugin.

Definition at line 60 of file inifile.cpp.

Here is the call graph for this function:

◆ writeComments()

void writeComments ( strings::compatibility::std::StringWriter os,
const AString comments 
)
protected

Writes a list of comments to the file. Comment lines are started with '#'.

Parameters
osThe encapsulated output stream to write to.
commentsThe comment lines for the section.

Definition at line 363 of file inifile.cpp.

Here is the call graph for this function:

◆ WriteFile()

void WriteFile ( )

Write all configuration data into the file.

Exceptions
Exception(config::Exceptions::ErrorOpeningFile ).

Definition at line 400 of file inifile.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ AutoSave

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 113 of file inifile.hpp.

◆ DefaultCommentPrefix

String DefaultCommentPrefix =A_CHAR("# ")

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 140 of file inifile.hpp.

◆ DefaultFileExtension

String DefaultFileExtension = A_CHAR(".ini")
static

The standard file extension used for ALib configuration files. Defaults to ".ini".

Definition at line 117 of file inifile.hpp.

◆ FileComments

AString FileComments

The file header which will be written out as a comment lines with "# " prefixes

Definition at line 124 of file inifile.hpp.

◆ FileName

AString FileName

The file name. This might include a path or not. Should be set properly before the file is read.

Definition at line 121 of file inifile.hpp.

◆ FormatIncludeDelimInAttrAlignment

bool FormatIncludeDelimInAttrAlignment = false

Denotes whether the spaces that are inserted when aligning attributes are located before or behind the delimiter.

Definition at line 151 of file inifile.hpp.

◆ FormatSpaceAfterDelim

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 147 of file inifile.hpp.

◆ FormatSpaceBeforeDelim

bool FormatSpaceBeforeDelim = false

Denotes if a space should be written before a delimiter.

Definition at line 143 of file inifile.hpp.

◆ LinesWithReadErrors

List<integer> LinesWithReadErrors

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 132 of file inifile.hpp.

◆ LineWidth

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 128 of file inifile.hpp.


The documentation for this class was generated from the following files: