unit Envionment;

interface

uses
  Classes, SysUtils, Inifiles, Forms;

type

  TIni = class(TObject)
  private
    FIniFile: TIniFile;
    FFileName: string;
    FSectionName: string;
    FDefaultString: string;
    FDefaultInteger: Integer;
    FDefaultBoolean: Boolean;
    FDefaultFloat: Double;
    function GetSValue(const Key: string): string;
    function GetBValue(const Key: string): Boolean;
    function GetFValue(const Key: string): Double;
    function GetIValue(const Key: string): Integer;
    procedure SetSValue(const Key, Value: string);
    procedure SetBValue(const Key: string; const Value: Boolean);
    procedure SetFValue(const Key: string; const Value: Double);
    procedure SetIValue(const Key: string; const Value: Integer);
    function GetIniFile: TIniFile;
    procedure SetFileName(const Value: string);
    function GetDTValue(const Key: string): TDateTime;
    procedure SetDTValue(const Key: string; const Value: TDateTime);
  Public
    property IniFile: TIniFile read GetIniFile;
    
    property FileName: string read FFileName write SetFileName;
    property SectionName: string read FSectionName write FSectionName;
    property DefaultString: string read FDefaultString write FDefaultString;
    property DefaultInteger: Integer read FDefaultInteger write FDefaultInteger;
    property DefaultBoolean: Boolean read FDefaultBoolean write FDefaultBoolean;
    property DefaultFloat: Double read FDefaultFloat write FDefaultFloat;

    property SValue[const Key: string]: string read GetSValue write SetSValue;
    property IValue[const Key: string]: Integer read GetIValue write SetIValue;
    property BValue[const Key: string]: Boolean read GetBValue write SetBValue;
    property FValue[const Key: string]: Double read GetFValue write SetFValue;
    property DTValue[const Key: string]: TDateTime read GetDTValue write SetDTValue;

    function ReadSectionValues(Strings: TStrings = nil): TStrings;
    procedure EraseSection;

    Constructor Create(AFileName: string = '');
    destructor Destroy; override;
  end;

  
implementation

{ TIni }

const
{
  GStringIndex  = 1;
  GIntegerIndex = 2;
  GBooleanIndex = 3;
  GFloatIndex   = 4;
}
  GDefaultString  = '';
  GDefaultInteger = 0;
  GDefaultBoolean = False;
  GDefaultFloat   = 0;

constructor TIni.Create(AFileName: string);
begin
  FDefaultString := GDefaultString;
  FDefaultInteger := GDefaultInteger;
  FDefaultBoolean := GDefaultBoolean;
  FDefaultFloat := GDefaultFloat;
  FFileName := AFileName;
  if SameText(Trim(AFileName), '') then
  begin
    FFileName := Application.ExeName;
    FFileName := ChangeFileExt(FFileName, '.ini')
  end;
end;

destructor TIni.Destroy;
begin
  if Assigned(FIniFile) then
    FIniFile.Free;
  inherited;
end;

procedure TIni.EraseSection;
begin
  IniFile.EraseSection(FSectionName);
end;

function TIni.GetBValue(const Key: string): Boolean;
begin
  Result := IniFile.ReadBool(FSectionName, Key, FDefaultBoolean);
end;

function TIni.GetDTValue(const Key: string): TDateTime;
begin
  Result := IniFile.ReadDateTime(FSectionName, Key, Now);
end;

function TIni.GetFValue(const Key: string): Double;
begin
  Result := IniFile.ReadFloat(FSectionName, Key, FDefaultFloat);
end;

function TIni.GetIniFile: TIniFile;
begin
  if not Assigned(FIniFile) then
    FIniFile := TIniFile.Create(FFileName);
  Result := FIniFile;
end;

function TIni.GetIValue(const Key: string): Integer;
begin
  Result := IniFile.ReadInteger(FSectionName, Key, FDefaultInteger);
end;

function TIni.GetSValue(const Key: string): string;
begin
  Result := IniFile.ReadString(FSectionName, Key, FDefaultString);
end;

function TIni.ReadSectionValues(Strings: TStrings): TStrings;
begin
  Result := Strings;
  if not Assigned(Result) then
    Result := TStringList.Create;
  Result.Clear;
  IniFile.ReadSectionValues(FSectionName, Result);
end;

procedure TIni.SetBValue(const Key: string; const Value: Boolean);
begin
  IniFile.WriteBool(FSectionName, Key, Value);
end;

procedure TIni.SetDTValue(const Key: string; const Value: TDateTime);
begin
  IniFile.WriteDateTime(FSectionName, Key, Value);
end;

procedure TIni.SetFileName(const Value: string);
begin
  if Assigned(FIniFile) then
    FreeAndNil(FIniFile);    
  FFileName := Value;
end;

procedure TIni.SetFValue(const Key: string; const Value: Double);
begin
  IniFile.WriteFloat(FSectionName, Key, Value);
end;

procedure TIni.SetIValue(const Key: string; const Value: Integer);
begin
  IniFile.WriteInteger(FSectionName, Key, Value);
end;

procedure TIni.SetSValue(const Key, Value: string);
begin
  IniFile.WriteString(FSectionName, Key, Value);
end;

end.
