» Help

Reading and writing INI files

   

Here is an overloaded group of commands to read from or write to an INI file. The routines are overloaded based on the d parameter. s is the [GROUP], k is Left side (key), d is the default or new parameter and f is the filename to use.

function GetIni(s,k: string; d: boolean; f: string=''): boolean; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  if ini.ReadString(s,k,'') = '' then
  begin
    ini.WriteBool(s,k,d);
    ini.UpdateFile;
  end;
  Result := ini.ReadBool(s,k,d);
  FreeAndNil(ini);
end;

function SetIni(s,k: string; d: boolean; f: string=''): boolean; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  ini.WriteBool(s,k,d);
  ini.UpdateFile;
  Result := ini.ReadBool(s,k,d);
  FreeAndNil(ini);
end;

function GetIni(s,k: string; d: integer; f: string=''): integer; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  if ini.ReadString(s,k,'') = '' then
  begin
    ini.WriteInteger(s,k,d);
    ini.UpdateFile;
  end;
  Result := ini.ReadInteger(s,k,d);
  FreeAndNil(ini);
end;

function SetIni(s,k: string; d:integer; f: string=''): integer; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  ini.WriteInteger(s,k,d);
  ini.UpdateFile;
  Result := ini.ReadInteger(s,k,d);
  FreeAndNil(ini);
end;

function GetIni(s,k: string; d: string; f: string=''): string; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  if ini.ReadString(s,k,'') = '' then
  begin
    ini.WriteString(s,k,d);
    ini.UpdateFile;
  end;
  Result := ini.ReadString(s,k,d);
  FreeAndNil(ini);
end;

function SetIni(s,k: string; d: string; f: string=''): string; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  ini.WriteString(s,k,d);
  ini.UpdateFile;
  Result := ini.ReadString(s,k,d);
  FreeAndNil(ini);
end;
Author: Joe Donth
Contributor: Joe Donth
Added: 2007-10-29
Last updated: 2007-10-29

« Return to contents »