Defines various base classes for loading and saving configuration files.

lazconfigstorage.pas is part of the LazUtils package.

Author: Mattias Gaertner

Defines the base class for a configuration storage mechanism.

TConfigStorage is a class used to create and maintain configuration information using an XML-based storage format. TConfigStorage is a base class; it contains abstract virtual methods which must be implemented in descendent class to provide memory- or file-based storage for its values.

TConfigStorage and descendent classes have a base path where its data is stored, and sub-paths and values for the data items in the storage. Path information uses an XPath-like notation which indicates where a given value is stored, and the name associated with the stored value.

For example:

'path/to/loginform' 'path/to/loginform/top' 'path/to/loginform/left' 'path/to/loginform/width' 'path/to/loginform/height'

Values can be specified using one of the native Pascal data types supported in the class, like:

  • String
  • Integer
  • Boolean
  • TStrings
  • TRect
  • TPoint
  • TPersistent

Methods are provided to get, set, or delete stored items using their path notation. Methods are also provided to read or write published property values to or from persistent objects using RTTI.

Do not create instances of TConfigStorage. Use of the descendent classes like TConfigMemStorage or TXMLConfigStorage.

TXMLConfigStorage
Contains a path history built using AppendBasePath. Member used to store the value in CurrentBasePath. Specifies a method used to get a value or its default from the specified fully-qualified path.

Returns the value stored using the path and name in the APath argument. The value in ADefault is returned if APath is not found in the configuration storage. The overloaded variants allow the return and default values to be specified using String, Integer, or Boolean types.

GetFullPathValue is an abstract virtual method in TConfigStorage. It is implemented in a descendent class to use the storage mechanism for the class instance.

Value in the configuration storage for the specified value type. Fully-qualified path to the value. Includes both the base path and the name for the item. Default value returned when APath is not found in the configuration storage. Defines the method used to resolve the specified node path prior to storing the specified value.

SetFullPathValue is an overloaded abstract virtual method in TConfigStorage. It must be overridden in a descendent class to use the node notation required for the underlying storage mechanism.

SetFullPathValue is called from the SetValue method after ExtendPath has been called to include the CurrentBasePath value at the start of the node path in APath.

TXMLConfigStorage
Path to the node where the specified value is stored. Value stored in the specified node path. Overloaded variants allow AValue to be specified using as either a String, an Integer, or a Boolean type. Defines the method used to delete the specified node path or to reset it to a default value.

SetDeleteFullPathValue is an overloaded abstract virtual method in TConfigStorage. It must be overridden and implemented in a descendent class to use the node notation required for the underlying storage mechanism.

Overloaded variants of the method allow the AValue and DefValue arguments to be passed as either String, Integer, or Boolean types. Both arguments must use the same type. The arguments determine whether the node is removed or updated with a default value in the storage mechanism. When both arguments have the same value, the node at APath is deleted from the storage. Otherwise, the value in DefValue is stored for the specified node.

SetDeleteFullPathValue is called from the SetDeleteValue method after ExtendPath has been called to include the CurrentBasePath value at the start of the node path in APath.

TXMLConfigStorage
Path to the node updated or removed in the method. Value for the node to update or remove in the method. Causes the node to be deleted when its value matches DefValue. Default value stored in the node when AValue does not match DefValue. Defines the method used to remove the node at the specified path from the configuration storage.

DeleteFullPath is an abstract virtual method in TConfigStorage. It must be implemented in a descendent class to perform actions needed to remove the specified node from the storage mechanism for the class instance.

DeleteFullPath always physically removes the node at the specified path. Use SetDeleteFullPathValue to update or remove a node using a value and default value.

DeleteFullPath is used to implement the DeletePath method.

Path to the node deleted in the method. Defines the method used to delete the node with the specified full path name from the configuration storage.

DeleteFullPathValue is an abstract virtual method in TConfigStorage used to delete the node with the path name specified in APath. It must be implemented in a descendent class to access the storage mechanism for the derived class instance.

DeleteFullPathValue is used to implement the DeleteValue method.

Fully-qualified path name for the node deleted in the method. Stores one or more property values from an RTTI-enabled persistent object to the specified path in the configuration storage.

WriteProperty is based on the actions performed by the TWriter class in the FPC RTL. Is uses the RTTI property information passed in the PropInfo argument to access each of the properties in Instance. No actions are performed for a given property if it does not provide both a getter and a setter routine for the property value.

APath contains the fully-qualified path where value(s) are stored in the configuration storage.

Instance is the RTTI-enabled persistent object with the value(s) retrieved and written in the method.

PropInfo in a pointer to type information for the persistent object in Instance (and DefInstance). It is used to access each property, and to convert and store each value in the storage mechanism by calling the SetValue method.

OnlyProperty contains an optional name for a single property name stored in the method. When omitted (or set to an empty string), all of the properties in the type information are stored starting at the specified path using the Name for the property type as an identifier.

DefInstance is a persistent object which contains values to be treated as the default values for the corresponding properties in Instance. When a property has the same value in both Instance and DefInstance, it is deleted from the configuration storage. DefInstance can be Nil - but default property values cannot be determined when omitted.

WriteProperty handles values for the following type kinds in property type information:

  • tkInteger (including string identifiers for the value)
  • tkChar
  • tkEnumeration
  • tkSet (as comma-delimited identifiers in the set)
  • tkWChar
  • tkFloat
  • tkSString
  • tkLString
  • tkAString
  • tkBool

The following type kinds are not explicitly handled in the current LCL version:

  • tkWString
  • tkInt64
  • tkQWord
Path where property value(s) from the specified object are stored. Persistent object with the property value(s) stored in the method. Pointer to the RTTI property information processed in the method. Persistent object with the default value(s) for properties processed in the method. Specifies a single property name to the stored in the method. All properties in PropInfo are stored when omitted (or set to ''). Reads value(s) from a path in the configuration storage into the specified persistent object.

ReadProperty is based on the actions performed by the TReader class in the FPC RTL. Is uses the RTTI property information passed in the PropInfo argument to store the property values in Instance. No actions are performed if the property information does not have both a getter and a setter routine for the property value.

APath contains the fully-qualified path where value(s) are stored in the configuration storage.

Instance is the RTTI-enabled persistent object where value(s) read from the configuration storage are stored in the method.

PropInfo in a pointer to type information for the persistent object in Instance (and DefInstance). It is used to access each property, and to convert and store each value from the storage mechanism using RTTI routines.

OnlyProperty contains an optional name for a single property name handled in the method. When omitted (or set to an empty string), all of the properties in the type information are read and stored in the properties starting at the specified path.

DefInstance is a persistent object which contains values to be treated as the default values for the corresponding properties in Instance. If DefInstance is not specified (or contains Nil), the Instance argument is assigned to DefInstance.

ReadProperty handles values for the following type kinds in property type information:

  • tkInteger (including integer which have a string identifier)
  • tkChar
  • tkEnumeration
  • tkSet
  • tkWChar
  • tkFloat
  • tkSString
  • tkLString
  • tkAString
  • tkBool

The following type kinds are not explicitly handled in the current LCL version:

  • tkWString
  • tkInt64
  • tkQWord
Path where property value(s) are stored in the configuration storage. Persistent object where value(s) read from storage are stored in the method. Pointer to the RTTI property information processed in the method. Persistent object with the default value(s) for properties processed in the method. Specifies a single property name to the stored in the method. All properties in PropInfo are stored when omitted (or set to ''). Constructor for the class instance. Create has an empty implementation in TConfigStorage. Use one of the descendent classes which re-implements the constructor as needed for its storage mechanism. File name where configuration data is stored. True to load values from the specified file if it exists. False to create a new, empty storage. Destructor for the class instance.

Destroy is the overridden destructor for the class instance. It frees resources allocated for members in the class instance, and calls the inherited destructor.

TObject.Destroy
Clears the content in the configuration storage.

Clear is an abstract virtual method in TConfigStorage. It is implemented in descendent classes to provide a file- or memory-based storage mechanism.

Gets the value stored at the specified path relative to the base path in the configuration storage.

GetValue is an overloaded method in TConfigStorage. The overloaded variants allow the value read from the specified path in the configuration storage to be returned as one of the following types:

  • String
  • Integer
  • Boolean
  • TRect (returned in the output parameter)
  • TPoint (returned in the output parameter)
  • TStrings (returned in the output parameter)

ADefault contains the default value returned if a value is not found in the storage for the specified path. The argument uses the same type as the return value for the overloaded methods.

GetValue calls GetFullPathValue after using ExtendPath to insert the value CurrentBasePath at the start of the value in APath. The value in ADefault is passed to GetFullPathValue as the default value.

Returns the value stored at the specified path. Path in the configuration storage relative to CurrentBasePath where the value is stored. Default value used when a value is not found at the specified path. Returns the value read from the configuration storage as a TRect type. Returns the value read from the configuration storage as a TPoint type. Returns the value read from the configuration storage as a TStrings type. Stores a value at the specified path in the configuration storage.

SetValue is an overloaded method in TConfigStorage used to store the value specified in the AValue argument to the node path in APath. The overloaded variants allow AValue to be specified using String, Integer, Boolean, TRect, or TPoint types.

APath contains the node path where the value is stored in the storage mechanism. SetValue calls the ExtendPath method to include the value in CurrentBasePath prior to the specified node path.

The SetFullPathValue method is called to store AValue to the underlying storage mechanism. Overloads for TRect and TPoint are decomposed and stored as the members values for the types. For TRect: Left, Top, Right, and Bottom are stored. For TPoint: X and Y are stored.

Use GetValue to read a stored value for a specified node path.

Node path (relative to CurrentBasePath) where the specified value is stored. Value stored for the specified node path. Updates the value for the specified node path, or deletes it when it contains a default value.

SetDeleteValue is an overloaded method in TConfigStorage. The variants allow the method to be used to store a value using either a String, Integer, Boolean, TRect, or TPoint type in the AValue argument.

APath is the path to the node where the value is stored. It is specified with a value relative to the CurrentBasePath property. ExtendPath is called to insert CurrentBasePath at the start of the node path.

DefValue contains the value treated as the default value for the node path. The overloaded variants use the same type as specified in the AValue argument.

SetDeleteValue calls the SetDeleteFullPathValue method to update or remove the value at the specified path. When DefValue is equal to AValue, the node path in APath to be removed from the configuration storage.

Path to the node value updated or removed in the method. Value stored in the specified node. Default value which triggers removal of the node when it is the same as AValue. Removes the configuration storage node with the specified path.

Calls ExtendPath to resolve APath to the CurrentBasePath for the storage. Calls DeleteFullPath to remove the resolved path to the storage node.

Path to the node removed in the method. Deletes the value stored for the specified path.

Calls ExtendPath to resolve APath to the CurrentBasePath for the storage. Calls DeleteFullPathValue to remove value for the resolved node path.

Relative path to the node removed in the method. Contains the base node path where values are read or written in the storage mechanism.

CurrentBasePath is a read-only String property. Its value is updated when the AppendBasePath and UndoAppendBasePath methods are called in the class instance. The value is used when the ExtendPath method is called to resolve a relative node path to its location in the storage mechanism.

Resolves a relative node path to the current base path for the storage mechanism.

Inserts the value in CurrentBasePath as a prefix for the value in APath.

Path to a node after it has been resolved to CurrentBasePath. Relative node path extended with the value in CurrentBasePath. Appends the specified relative path to the existing value in CurrentBasePath.

AppendBasePath is a method used to append the relative path specified for a node in Path to the value in the CurrentBasePath property. AppendBasePath ensures that a trailing path delimiter ('/') is appended to the value in CurrentBasePath when it not empty and does not already include the delimiter.

Use UndoAppendBasePath to remove a path node from the value in CurrentBasePath.

Relative path added to the value in CurrentBasePath. Removes the last node path from the value in CurrentBasePath. Defines the method used to flush the values in the configuration storage to the underlying storage mechanism.

WriteToDisk is an abstract virtual method in TConfigStorage. It must be implemented in a descendent class to perform actions needed to store values to the mechanism used in a derived class.

TXMLConfigStorage.WriteToDisk
Defines the method used to get the file name where configuration data is stored.

GetFileName is an abstract virtual String function in TConfigStorage. It is used to retrieve the file name where configuration data is stored when the WriteToDisk method is called. It must be implemented in a descendent class which uses a file as the destination for the storage mechanism.

TXMLConfigStorage.GetFileName TXMLConfigStorage.WriteToDisk
Returns the file name where the configuration data is stored. Stores property names and values in the specified object to the configuration storage.

WriteObject is a method used to store names and values from the properties in the specified persistent object into the configuration storage.

Path contains the location in configuration storage where the property values are stored. It provides a unique location for the values stored in the method.

Obj is the TPersistent instance with the property values stored in the method.

WriteObject calls WriteProperty for each of the properties returned from the RTL GetPropList routine. Sub-paths representing each of the published properties in the object are created by calling the WriteProperty method.

WriteObject (and WriteProperty) rely on RTTI types and routines to get the values from the persistent object. As such, only published properties can be serialized; the FPC RTL does not provide RTTI type information for other protected or public visibility at this time. The properties must have getter and setter routines (read and write specifiers); otherwise, they are omitted from the serialized values.

Use the OnlyProperty argument to specify the name for a single property in Obj that should be written in the method. Otherwise, all properties are stored.

Use ReadObject to read values from a given path into a persistent object instance.

TPersistent GetPropList GetPropInfo TTypeData
Path where property names and values from Obj are stored. Persistent object with properties stored in the method. Persistent object with default values for the properties in Obj. Name for a single property that should be written in the method, instead of all properties. Reads names and values from the configuration storage into properties for the specified object instance. Path where the property names and values read in the method are stored. Persistent object where the values read in the method are stored. Persistent object with the default values for properties updated in the method. Name for a single property value read and stored in Obj. Class reference used to create new instances of TConfigStorage.

TConfigStorageClass is the type used for the DefaultConfigClass variable in the Lazarus IDE.

Implements a storage node used in a memory-based configuration storage. Name for the node. Value for the node. Parent node in the tree structure where the current node is stored. Child nodes with names and values stored as sub-paths of the current node. Frees storage nodes allocated in Children. Constructor for the class instance.

Create is the constructor for the class instance. It initializes values in the Parent and Name properties to the arguments passed to the constructor.

Node where the current node is stored as a child node. Name for the new storage node. Destructor for the class instance.

Destroy is the overridden destructor for the class instance. It calls ClearChilds to remove all storage nodes allocated in Children. It ensures the current class instance is removed from the Parent node (when assigned).

Destroy calls the inherited destructor prior to exiting from the method.

Represents modification operations for configuration storage nodes.

TConfigMemStorageModification is an enumerated type with values that represent modification operations performed for nodes in a memory-based configuration storage.

Represents a set or write operation. Represents a get or read operation. Represents a delete operation for a storage node. Represents a delete operation for the value in a storage node. Current version for the storage format used in configuration storage classes. Implement a configuration storage class using in-memory storage for values in the class instance. Creates the root node used to store the values for the memory-based configuration storage. Creates the tree structure used to store child nodes in the configuration storage. Memory-based storage node where the child nodes are stored. Stores a modified value to the specified path in the configuration storage. Path in the storage where the modified value is stored. Action performed in the modification. New value stored at the specified path in the storage. Implements the method used to remove the node at the specified path from the configuration storage.

DeleteFullPath is an overridden method in TConfigMemStorage. It must be implemented in a descendent class to perform actions needed to remove the specified node from the storage mechanism for the class instance. It calls a private method used to search nodes in the memory-based storage for the specified node path. When found, the node is removed.

DeleteFullPath is called from the DeletePath method.

Path to the node removed in the method. Deletes the node with the specified full path name from the configuration storage. Fully-qualified path to the node removed in the method. Gets the value from the specified node path with an optional default value when not found.

GetFullPathValue is an overloaded method in TConfigMemStorage. It implements the abstract virtual methods defined in the TConfigStorage ancestor. The variants allow the value in ADefault to specified as either a String, Integer, or Boolean type. Please note that TRect and TPoint are not handled in TConfigMemStorage.

Value stored in the specified node path. Path to the node where the value is stored. Default value returned when the specified node path is not found in the storage. Implements the method used to delete the specified node path or to reset it to a default value.

SetDeleteFullPathValue is an overloaded overridden method in TConfigMemStorage. Overloaded variants of the method allow the AValue and DefValue arguments to be passed as either String, Integer, or Boolean types. Both arguments must use the same type. The arguments determine whether the node is removed or updated with a default value in the storage mechanism. When both arguments have the same value, the node at APath is deleted from the storage. Otherwise, the value in DefValue is stored for the specified node.

SetDeleteFullPathValue is called from the SetDeleteValue method after ExtendPath has been called to include the CurrentBasePath value at the start of the node path in APath.

TXMLConfigStorage
Path to the node updated or removed in the method. Value for the node to update or remove in the method. Causes the node to be deleted when its value matches DefValue. Default value stored in the node when AValue does not match DefValue. Implements the method used to resolve the specified node path prior to storing the specified value.

SetFullPathValue is an overloaded method in TConfigStorage. It implements the abstract virtual method introduced in the TConfigStorage ancestor. The overloaded variants convert AValue to a String type (when needed) and stores the value in a memory-based configuration node with the fully-qualified path specified in APath.

SetFullPathValue is called from the SetValue method after ExtendPath has been called to include the CurrentBasePath value at the start of the node path in APath.

TXMLConfigStorage
Resolved path to the node where the specified value is stored. Value stored in the specified node path. Overloaded variants allow AValue to be specified using as either a String, an Integer, or a Boolean type. TConfigMemStorageNode instance with the root node for the configuration storage.

Root is a TConfigMemStorageNode member with the root node for the storage. The node is created (when needed) when a value is stored to the memory-based configuration storage, and freed when the class instance is destroyed. Nodes in the storage are accessed starting at the Root node, and navigated by accessing the child nodes recursively. The node path reflects the hierarchy of nodes accessed to reach a specific node.

Re-implements the method used to get the file name where the configuration items are stored. Not used in TConfigMemStorage. Always returns an empty string ('') in TConfigMemStorage. Re-implements the method used to store the configuration items to disk.

WriteToDisk raises an Exception with the message:

'TConfigMemStorage.WriteToDisk invalid operation'
Destructor for the class instance.

Frees resources allocate to the Root storage node in the class instance. Calls the inherited destructor prior to exit.

Clears the storage nodes in the memory-based configuration storage.

Free resources allocated to the Root storage node in the class instance.

Save the storage nodes to the specified path in another configuration storage class instance. Configuration storage instance where values are stored. Path to the location in Config where values in the current class instance are stored. Loads storage nodes from the specified path in another configuration storage class instance. Configuration storage instance with the values loaded into the current class. Path to the values in Config which are loaded in the method. Generates the content in the Root node in a format suitable for debugger output. Loads names and values at the specified path in a configuration storage instance to the specified Tree. Configuration storage instance with the names and values loaded into the tree. Path with the item names and values loaded from Config. String map where the names and values for configuration items stored in the routine. Not used in the current LCL version.

SaveStringToStringTree is a routine used to save values found in the specified Tree structure to a TConfigStorage instance under the specified path.

Configuration storage where the value(s) in Tree are stored. Path to the node in Config where the value(s) in Tree are stored. String map enumerator with the values stored in the routine. Default locale-independent format used for date values in DateToCfgStr. Added in LazUtils version 3.0. Default locale-independent format used for date/time values. Added in LazUtils version 3.0. Returns a string representation for TDateTime value using the specified format. Added in LazUtils version 3.0. String with the date and/or time represented using the specified format. TDateTime value converted in the routine. Format for the date/time value. Default value is defined in the DateAsCfgStrFormat constant and represents the date part of the specified value. Converts a string with a date/time value using the specified format to the TDateTime output value. Added in LazUtils version 3.0. True if the string was successfully converted to a TDateTIme value in the routine. String with the date and/or time value converted in the routine. Returns the TDateTime value for the specified string, or an empty TDateTime value (0.0) when the routine returns False. Format used for the date/time value passed in the s argument. Compares the specified PChar values to determine the relative order for the memory node paths. Returns 0 if the arguments have the same value (or both are empty). Returns -1 if p1 has a shorter path length than (comes before) p2. Returns 1 if p1 has a longer path length than (comes after) p2. Node path compared up to the first '/' or #0 character. Node path compared up to the first '/' or #0 character. Compares the names for the specified memory-based storage nodes to determine their relative order. Returns 0 if the node names have the same value (or both are empty). Returns -1 if Node1 has a node name shorter than (comes before) Node2. Returns 1 if Node1 has a node name longer than (comes before) Node2. Memory-based storage node compared in the routine. Memory-based storage node compared in the routine. Compares the specified string to the name for a the specified memory-based storage nodes to determine their relative order. Returns 0 if the string and the node name have the same value (or both are empty). Returns -1 if the string is shorter than (comes before) the node name in ANode. Returns 1 if the string is longer than (comes after) the node name in ANode. Pointer to the null-terminated character values compared in the routine. Memory-based storage node with the name compared in the routine.