Files Delphi

Title: An easier alternative for a TIniFile object
Question: There is an alternative for the usage of the TInifile object. Any TStringList
has a LoadFrom- and SaveToFile method and using the values property, we can extract and change item values from them.
Answer:
INI was the old and "new" way to store settings (outside the registry), cause not everything is worth XML parsing or the complicated registry.
Delphi provides us with a TIniFile object, but for simple cases (read from CD-ROM, Games, Demos, platform independence) we can use a INI-like file with the help of a TStringList.
It's a simple way to read csv-files and other files into your program or system.
Developping for a crossplatform, TStringList do show the same behaviour in CLX, TIniFile differs in the way from inheritance:
Win: TCustomIniFile Linux: TMemIniFile
So on Linux the whole ini-file is persistent in a RAM. To place information in a global location, you could store the configuration file with file permissions and access rights in the root directory. However in Kylix, you need to use TMemIniFile instead of TRegIniFile.
Let's go to a solution with a StringList. First you define the configuration file as you wish:
DATABASENAME=milo2:D:\franktech\Delphmax\interbase\stormax.gdb
USER_NAME=SYSDBA
PASSWORD=masterkey
PATTERNSIZE=39
OPTIONS=10110011
Second you read the file, store it to a StringList and you get access to each line with the value property:
patLst:= TStringlist.create;
try
fN:= filepath+ 'stormax.dat';
if fileexists(fN) then begin
patLst.LoadFromFile(fN);
ibase.DatabaseName:= patLst.Values['DATABASENAME'];
ibase.Params.Values['USER_NAME']:= patLst.values['USER_NAME'];
ibase.Params.Values['PASSWORD']:= patLst.values['PASSWORD'];
patternsk:= patLst.values['PATTERNSIZE'];
end else begin
patLst.values['DATABASENAME']:= 'LOCALHOST:C:\example.gdb';
patLst.values['USER_NAME']:= 'SYSDBA';
patLst.values['PASSWORD']:= 'masterkey';
patLst.values['PATTERNSIZE']:= '39';
patLst.SaveToFile(fN);
end;
ibase.open;
.....
The Name index is case-insensitive.
The Name that identifies the string is to the left of the equal sign (=), and the current Value of the Name identifier is on the right side. There should be no spaces present before or after the equal sign.
If the list does not contain any strings of the proper Name=Value form, or if none of those strings matches the Name index, Values returns an empty string.
Delphi 7 further extended a ValueFromIndex which gives you direct access to the value portion of a string at a given position, makes it faster too:
ibase.Params.ValuefromIndex:= patLst.ValueFromIndex[2];
Caveat: the values property does not support sections, this may cause problems with duplicate item names but we can say: as smart as possible, all the best in 2006.