Collection which can write itself to inifile.

inicol contains an implementation of TCollection and TCollectionItem descendents which cooperate to read and write the collection from and to a .ini file. It uses the TCustomIniFile class for this.

Exception and string formatting Collections Ini file support Collection item with support for loading/saving from/to ini file

TIniCollectionItem is a descendent which has some extra methods for saving/loading the item to or from an .ini file.

To use this class, a descendent should be made, and the SaveToIni and LoadFromIni methods should be overridden. They should implement the actual loading and saving. The loading and saving is always initiated by the methods in , and respectively.

Method called when the item must be saved SaveToIni is called by when it saves this item. Descendent classes should override this method to save the data they need to save. All write methods of the TCustomIniFile instance passed in Ini can be used, as long as the writing happens in the section passed in Section. No checking is done to see whether the values are actually written to the correct section. LoadFromIni SaveToFile LoadFromFile TCustomIniFile instance to which the item should be saved Section under which data should be saved. Method called when the item must be loaded LoadFromIni is called by when it saves this item. Descendent classes should override this method to load the data they need to load. All read methods of the TCustomIniFile instance passed in Ini can be used, as long as the reading happens in the section passed in Section. No checking is done to see whether the values are actually read from the correct section. SaveToIni LoadFromFile SaveToFile TCustomIniFile instance from which the item should be loaded Section from which data should be loaded. Save item to a file SaveToFile creates an instance of TIniFile with the indicated FileName calls SaveToIni to save the item to the indicated file in .ini format under the section Section An exception can occur if the file is not writeable. SaveToIni LoadFromFile Name of file to which the item should be saved. Section under which the item should be saved. Load item from a file LoadFromFile creates an instance of TMemIniFile and calls LoadFromIni to load the item from the indicated file in .ini format from the section Section. None. SaveToFile LoadFromIni Filename from which to load item Section from which to load Default section name SectionName is the section name under which the item will be saved or from which it should be read. The read/write functions should be overridden in descendents to determine a unique section name within the .ini file. SaveToFile LoadFromIni Collection which can write itself to an .ini file

TIniCollection is a collection descendent which has the capability to write itself to an .ini file. It introduces some load and save mechanisms, which can be used to write all items in the collection to disk. The items should be descendents of the type .

All methods work using a TCustomInifile class, making it possible to save to alternate file formats, or even databases.

An instance of TIniCollection should never be used directly. Instead, a descendent should be used, which sets the FPrefix and FSectionPrefix protected variables.

Loads the collection from the default filename. Load loads the collection from the file as specified in the FileName property. It calls the LoadFromFile method to do this. If the collection was not loaded or saved to file before this call, an EIniCol exception will be raised. FileName Save the collection to the default filename. Save writes the collection to the file as specified in the FileName property, using GlobalSection as the section. It calls the SaveToFile method to do this. If the collection was not loaded or saved to file before this call, an EIniCol exception will be raised. FileName Save the collection to a TCustomIniFile descendent

SaveToIni does the actual writing. It writes the number of elements in the global section (as specified by the Section argument), as well as the section name for each item in the list. The item names are written using the Prefix property for the key. After this it calls the SaveToIni method of all instances.

This means that the global section of the .ini file will look something like this:

[globalsection]
Count=3
Prefix1=SectionPrefixFirstItemName
Prefix2=SectionPrefixSecondItemName
Prefix3=SectionPrefixThirdItemName

This construct allows to re-use an ini file for multiple collections.

After this method is called, the GlobalSection property contains the value of Section, it will be used in the Save method.

Inifile class to use when writing data. Section to which to write the global data Save collection to a file in .ini file format SaveToFile will create a TMemIniFile instance with the AFileName argument as a filename. This instance is passed on to the SaveToIni method, together with the Section argument, to do the actual saving. An exception may be raised if the path in AFileName does not exist. File to save collection to. Section name for global section. Load collection from a file in .ini file format.

LoadFromIni will load the collection from the Ini instance. It first clears the collection, and reads the number of items from the global section with the name as passed through the Section argument. After this, an item is created and added to the collection, and its data is read by calling the method, passing the appropriate section name as found in the global section.

The description of the global section can be found in the method description.

Ini object to load data from. Name of the global section. Load collection from file. LoadFromFile creates a TMemIniFile instance using AFileName as the filename. It calls LoadFromIni using this instance and Section as the parameters. Filename to load collection from Section to use as global section Prefix used in global section

Prefix is used when writing the section names of the items in the collection to the global section, or when reading the names from the global section. If the prefix is set to Item then the global section might look something like this:

[MyCollection]
Count=2
Item1=FirstItem
Item2=SecondItem

A descendent of TIniCollection should set the value of this property, it cannot be empty.

Prefix string for section names SectionPrefix is a string that is prepended to the section name as returned by the property to return the exact section name. It can be empty. Filename of the collection FileName is the filename as used in the last LoadFromFile or SaveToFile operation. It is used in the Load or Save calls. Save LoadFromFile SaveToFile Load Name of the global section GlobalSection contains the value of the Section argument in the LoadFromIni or SaveToIni calls. It's used in the Load or Save calls. Save LoadFromFile SaveToFile Load Exception used for error reporting. EIniCol is used to report error conditions in the load and save methods of . Name used for the count item KeyCount is used as a key name when reading or writing the number of items in the collection from the global section. Default global section name SGlobal is used as the default name of the global section when reading or writing the collection. TIniCollectionItem with a name property TNamedIniCollectionItem is a descent with a published name property. The name is used as the section name when saving the item to the ini file. User-defined data UserData can be used to associate an arbitrary object with the item - much like the Objects property of a TStrings. Name of the item Name is the name of this item. It is also used as the section name when writing the collection item to the .ini file. Collection of named collection items TNamedIniCollection is the collection to go with the item class. it provides some functions to look for items based on the UserData or based on the Name. IndexOfUserData IndexOfName Search for an item based on it's UserData property

IndexOfUserData searches the list of items and returns the index of the item which has UserData in its UserData property. If no such item exists, -1 is returned.

Note that the (linear) search starts at the last element and works it's way back to the first.

If no item exists, -1 is returned. IndexOfName
Index of found item, or -1 if none was found UserData to search for Search for an item, based on its name, and return its position

IndexOfName searches the list of items and returns the index of the item which has name equal to AName (case insentitive). If no such item exists, -1 is returned.

Note that the (linear) search starts at the last element and works it's way back to the first.

If no item exists, -1 is returned. IndexOfUserData
Index of found item or -1 if none was found Name to look for (case insensitive) Return the item based on its name FindByName returns the collection item whose name matches AName (case insensitive match). It calls IndexOfName and returns the item at the found position. If no item is found, Nil is returned. If no item is found, Nil is returned. IndexOfName FindByUserData Named item, or Nil if none was found Name to look for Return the item based on its UserData FindByName returns the collection item whose UserData property value matches the UserData parameter. If no item is found, Nil is returned. If no item is found, Nil is returned. Found item, or Nil if none was found. Userdata to look for Indexed access to the TNamedIniCollectionItem items NamedItem is the default property of the TNamedIniCollection collection. It allows indexed access to the items. The index is zero based. Integer index, zero based.