SRL Archive - Test corner

Functions related to INI files.


EvilChicken!

2009-06-08 07:46:32

Something I found on my desktop that I've neglected for some time. Could any of this be useful?

function INI_SectionExists(Section, fileName: string): Boolean;
var
fileNum: Integer;
INItext: string;

begin
Result := False;
if FileExists(fileName) then
begin
fileNum := OpenFile(fileName, False);
if (fileNum < 1) then exit;
ReadFileString(fileNum, INItext, FileSize(fileNum));
Result := (Pos('[' + Lowercase(Section) + ']', Lowercase(INItext)) > 0);
CloseFile(fileNum);
end;
end;

function INI_ValueExists(Value, Section, fileName: string): Boolean;
var
fileNum: Integer;
INItext: string;

begin
Result := false;
if FileExists(fileName) then
begin
fileNum := OpenFile(fileName, False);
if (fileNum < 1) then exit;
ReadFileString(fileNum, INItext, FileSize(fileNum));
Result := (Pos(Lowercase(Value), Between('[' + Section + ']', '[', INItext)) > 0);
CloseFile(fileNum);
end;
end;

function INI_ReadBool(Section, KeyName, fileName: string): Boolean;
begin
Result := StrToBoolDef(ReadINI(Section, KeyName, fileName), False);
end;

function INI_ReadInt(Section, KeyName, fileName: string): Integer;
begin
Result := StrToIntDef(ReadINI(Section, KeyName, fileName), 0);
end;

function INI_ReadFloat(Section, KeyName, fileName: string): Extended;
begin
Result := StrToFloatDef(ReadINI(Section, KeyName, fileName), 0);
end;

function INI_ReadStrings(Keys: TStringArray; Section, fileName: string): TStringArray;
var
I, L: Integer;

begin
if FileExists(fileName) then
begin
L := High(Keys);
SetLength(Result, L + 1);
for I := 0 to L do
Result[I] := ReadINI(Section, Keys[I], fileName);
end;
end;

function INI_ReadIntegers(Keys: TStringArray; Section, fileName: string): TIntegerArray;
var
I, L: Integer;

begin
if FileExists(fileName) then
begin
L := High(Keys);
SetLength(Result, L + 1);
for I := 0 to L do
Result[I] := INI_ReadInt(Section, Keys[I], fileName);
end;
end;

function INI_ReadBooleans(Keys: TStringArray; Section, fileName: string): TBooleanArray;
var
I, L: Integer;

begin
if FileExists(fileName) then
begin
L := High(Keys);
SetLength(Result, L + 1);
for I := 0 to L do
Result[I] := INI_ReadBool(Section, Keys[I], fileName);
end;
end;

function INI_ReadExtendeds(Keys: TStringArray; Section, fileName: string): TExtendedArray;
var
I, L: Integer;

begin
if FileExists(fileName) then
begin
L := High(Keys);
SetLength(Result, L + 1);
for I := 0 to L do
Result[I] := INI_ReadFloat(Section, Keys[I], fileName);
end;
end;

ian.

2009-06-08 08:05:30

Probably ^^ Maybe show Da 0wner :D He made a tut about INI files, and I think he knows a lot about uses :]

Da 0wner

2009-06-08 08:07:07

Yeah, an old one :(. To be honest making tutorials on simple things are kinda hard.

Naum

2009-06-08 10:52:41

EC great work :). This will be very useful.
You can get it to be added to misc INI.scar maybe?

Da 0wner

2009-06-08 11:25:14

function KeyExists(Section, KeyName, Filename : string) : boolean;
begin
result := ReadINI(Section, KeyName, Filename) <> '';
end;

function SectionExists(Section, Filename : string) : boolean;
var
FileNum : integer;
begin
if not FileExists(Filename) then
exit;
FileNum := OpenFile(Filename, True);
ReadFileString(FileNum, Filename, FileSize(FileNum));
CloseFile(FileNum);
result := pos('[' + lowercase(section) + ']', lowercase(Filename)) > 0;
end;
xD.