//Advanced Delphi Systems Code: ads_Ini
Unit Ads_Ini;

{Copyright(c)1998 Advanced Delphi Systems

 Richard Maley
 Advanced Delphi Systems
 12613 Maidens Bower Drive
 Potomac, MD 20854 USA
 phone 301-840-1554
 maley@advdelphisys.com
 maley@compuserve.com
 maley@cpcug.org}

(*
UnitIndex Master Index Implementation Section Download Units
Description: ads_Ini.pas
This unit contains the following routines.

IniGetIntegerValue   IniGetStringValue   IniSetIntegerValue   IniSetStringValue   IniUpdateFromTStringList   IniUpdateTStringList   RegDeleteSection   RegDeleteVariable   RegGetBooleanValue   RegGetIntegerValue   RegGetStringValue   RegSetBooleanValue   RegSetExpandStringValue   RegSetIntegerValue   RegSetStringValue   TEditKeyFilter.OnlyAToZ   TEditKeyFilter.OnlyNumbers   TEditKeyFilter.OnlyNumbersAbsolute   TPanel_Cmp_Sec_ads.ResizeShadowLabel 

*)
Interface

Uses
  SysUtils, Classes, ExtCtrls, StdCtrls, Ads_Strg, IniFiles, 
  Registry, Windows;

{!~RegGetStringValue:

Gets the value of a variable (VarName) in the registry.
If the variable does not exist or is null the value
of VarDefault is returned.

The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.}
Function RegGetStringValue(
  const RootKey   : HKEY;
  const SubKey    : String;
  const VarName   : String;
  const VarDefault: String): String;

{!~RegGetIntegerValue:

Gets the value of a variable (VarName) in the registry.
If the variable does not exist or is null the value
of VarDefault is returned.

The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.}
Function RegGetIntegerValue(
  const RootKey   : HKEY;
  const SubKey    : String;
  const VarName   : String;
  const VarDefault: Integer): Integer;

{!~RegGetBooleanValue:

Gets the value of a variable (VarName) in the registry.
If the variable does not exist or is null the value
of VarDefault is returned.

The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.}
Function RegGetBooleanValue(
  const RootKey   : HKEY;
  const SubKey    : String;
  const VarName   : String;
  const VarDefault: Boolean): Boolean;

{!~RegSetStringValue:

Sets a variable (VarName) in the registry to the value
VarValue.  The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
Function RegSetStringValue(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String;
  const VarValue: String): Boolean;

{!~RegSetIntegerValue:

Sets a variable (VarName) in the registry to the value
VarValue.  The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
Function RegSetIntegerValue(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String;
  const VarValue: Integer): Boolean;

{!~RegSetBooleanValue:

Sets a variable (VarName) in the registry to the value
VarValue.  The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
Function RegSetBooleanValue(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String;
  const VarValue: Boolean): Boolean;

{!~RegDeleteVariable:

Deletes a variable (VarName) in the registry.
The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
Function RegDeleteVariable(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String): Boolean;

{!~RegDeleteSection:

DANGEROUS!!! BE CAREFUL!!!

Deletes a complete section from the registry.
The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp',  the MyApp section
would be deleted.  The SubKey will be removed with all
associated values.  If an exception is thrown the
function returns False, True otherwise.}
Function RegDeleteSection(
  const RootKey : HKEY;
  const SubKey  : String): Boolean;

{!~ Returns the ini value for a variable (IntegerName)
in the ini section (IniSection) of the ini file (TheIniFile).}
Function IniGetIntegerValue(
  TheIniFile            : String;
  IniSection            : String;
  IntegerName           : String;
  DefaultInteger        : Integer): Integer;

{!~ Returns the ini value for a variable (StringName)
in the ini section (IniSection) of the ini file (TheIniFile).}
Function IniGetStringValue(
  TheIniFile            : String;
  IniSection            : String;
  StringName            : String;
  DefaultString         : String): String;

{!~ Sets a variable (IntegerName) in the ini section (IniSection)
of the ini file (TheIniFile) with the value (IntegerValue).
If an exception is thrown the function returns False,
True otherwise.}
Function IniSetIntegerValue(
  TheIniFile            : String;
  IniSection            : String;
  IntegerName           : String;
  IntegerValue          : Integer): Boolean;

{!~ Sets a variable (StringName) in the ini section (IniSection)
of the ini file (TheIniFile) with the value (StringValue).
If an exception is thrown the function returns False,
True otherwise.}
Function IniSetStringValue(
  TheIniFile            : String;
  IniSection            : String;
  StringName            : String;
  StringValue           : String): Boolean;

{!~ Updates an ini file from a TStringList}
Procedure IniUpdateFromTStringList(
  TheIniFile            : String;
  IniSection            : String;
  StringListName        : String;
  CountField            : String;
  StringList            : TStringList);

{!~ Updates a TStringList from an ini file}
Procedure IniUpdateTStringList(
  TheIniFile            : String;
  IniSection            : String;
  StringListName        : String;
  CountField            : String;
  StringList            : TStringList);

implementation


Type
  TPanel_Cmp_Sec_ads = class(TPanel)
  Public
    procedure ResizeShadowLabel(Sender: TObject);
  End;

//
Unit Description UnitIndex Master Index
procedure TPanel_Cmp_Sec_ads.ResizeShadowLabel(
  Sender     : TObject);
Var
  PH, PW : Integer;
  LH, LW : Integer;
begin
  PH := TPanel(Sender).Height;
  PW := TPanel(Sender).Width;
  LH := TLabel(Controls[0]).Height;
  LW := TLabel(Controls[0]).Width;
  TLabel(Controls[0]).Top  := ((PH-LH) div 2)-3;
  TLabel(Controls[0]).Left := ((Pw-Lw) div 2)-3;
end;

Type
  TEditKeyFilter = Class(TEdit)
  Published
    {!~ Throws away all keys except 0-9,-,+,.}
    Procedure OnlyNumbers(Sender: TObject; var Key: Char);

    {!~ Throws away all keys except 0-9}
    Procedure OnlyNumbersAbsolute(Sender: TObject; var Key: Char);

    {!~ Throws away all keys except a-z and A-Z}
    Procedure OnlyAToZ(Sender: TObject; var Key: Char);

  End;

{!~ Throws away all keys except 0-9,-,+,.}
//
Unit Description UnitIndex Master Index
Procedure TEditKeyFilter.OnlyNumbers(Sender: TObject; var Key: Char);
Begin
  KeyPressOnlyNumbers(Key);
End;

{!~ Throws away all keys except 0-9}
//
Unit Description UnitIndex Master Index
Procedure TEditKeyFilter.OnlyNumbersAbsolute(Sender: TObject; var Key: Char);
Begin
  KeyPressOnlyNumbersAbsolute(Key);
End;

{!~ Throws away all keys except a-z and A-Z}
//
Unit Description UnitIndex Master Index
Procedure TEditKeyFilter.OnlyAToZ(Sender: TObject; var Key: Char);
Begin
  KeyPressOnlyAToZ(Key);
End;
{!~ Returns the ini value for a variable (IntegerName)
in the ini section (IniSection) of the ini file (TheIniFile).}
//
Unit Description UnitIndex Master Index
Function IniGetIntegerValue(
  TheIniFile            : String;
  IniSection            : String;
  IntegerName           : String;
  DefaultInteger        : Integer): Integer;
Var
  TheIni                : TIniFile;
Begin
  TheIni                := TIniFile.Create(TheIniFile);
  Try
    Result :=
      TheIni.ReadInteger(
        IniSection,
        IntegerName,
        DefaultInteger);
  Finally
    TheIni.Free;
  End;
End;

{!~ Returns the ini value for a variable (StringName)
in the ini section (IniSection) of the ini file (TheIniFile).}
//
Unit Description UnitIndex Master Index
Function IniGetStringValue(
  TheIniFile            : String;
  IniSection            : String;
  StringName            : String;
  DefaultString         : String): String;
Var
  TheIni                : TIniFile;
Begin
  TheIni                  := TIniFile.Create(TheIniFile);
  Try
    Result :=
      TheIni.ReadString(
        IniSection,
        StringName,
        DefaultString);
    If Result = '' Then
    Begin
      Result := DefaultString;
    End;
  Finally
    TheIni.Free;
  End;
End;

{!~ Sets a variable (IntegerName) in the ini section (IniSection)
of the ini file (TheIniFile) with the value (IntegerValue).
If an exception is thrown the function returns False,
True otherwise.}
//
Unit Description UnitIndex Master Index
Function IniSetIntegerValue(
  TheIniFile            : String;
  IniSection            : String;
  IntegerName           : String;
  IntegerValue          : Integer): Boolean;
Var
  TheIni                : TIniFile;
Begin
  TheIni                := TIniFile.Create(TheIniFile);
  Try
    Try
      TheIni.WriteInteger(
        IniSection,
        IntegerName,
        IntegerValue);
      Result := True;
    Except
      Result := False;
    End;
  Finally
    TheIni.Free;
  End;
End;

{!~ Sets a variable (StringName) in the ini section (IniSection)
of the ini file (TheIniFile) with the value (StringValue).
If an exception is thrown the function returns False,
True otherwise.}
//
Unit Description UnitIndex Master Index
Function IniSetStringValue(
  TheIniFile            : String;
  IniSection            : String;
  StringName            : String;
  StringValue           : String): Boolean;
Var
  TheIni                : TIniFile;
Begin
  TheIni                := TIniFile.Create(TheIniFile);
  Try
    Try
      TheIni.WriteString(
        IniSection,
        StringName,
        StringValue);
      Result := True;
    Except
      Result := False;
    End;
  Finally
    TheIni.Free;
  End;
End;

{!~ Updates an ini file from a TStringList}
//
Unit Description UnitIndex Master Index
Procedure IniUpdateFromTStringList(
  TheIniFile            : String;
  IniSection            : String;
  StringListName        : String;
  CountField            : String;
  StringList            : TStringList);
Var
  TheIni                : TIniFile;
  i                     : Integer;
Begin
  TheIni                  := TIniFile.Create(TheIniFile);
  Try
    TheIni.EraseSection(IniSection);
    TheIni.WriteString(
      IniSection,
      CountField,
      IntToStr(StringList.Count));
    For i := 0 To StringList.Count - 1 Do
    Begin
      TheIni.WriteString(
        IniSection,
        StringListName+'['+intToStr(i)+']',
        StringList[i]);
    End;
  Finally
    TheIni.Free;
  End;
End;

{!~ Updates a TStringList from an ini file}
//
Unit Description UnitIndex Master Index
Procedure IniUpdateTStringList(
  TheIniFile            : String;
  IniSection            : String;
  StringListName        : String;
  CountField            : String;
  StringList            : TStringList);
Var
  TheIni                : TIniFile;
  i                     : Integer;
  {CountString           : String;}
  Count                 : Integer;
Begin
  TheIni                  := TIniFile.Create(TheIniFile);
  Try
    Count :=
      IniGetIntegerValue(
        TheIniFile,
        IniSection,
        CountField,
        0);

    StringList.Clear;
    For i := 0 To Count - 1 Do
    Begin
      StringList.Add(
        TheIni.ReadString(
                 IniSection,
                 StringListName+'['+intToStr(i)+']',
                 ''));
    End;
  Finally
    TheIni.Free;
  End;
End;

{!~RegSetStringValue:

Sets a variable (VarName) in the registry to the value
VarValue.  The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
//
Unit Description UnitIndex Master Index
Function RegSetStringValue(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String;
  const VarValue: String): Boolean;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Try
      Reg.RootKey := RootKey;
      Reg.OpenKey(SubKey,True);
      Reg.WriteString('', VarName, VarValue);
      Result := True;
    Except
      Result := False;
    End;
  Finally
    Reg.Free;
  End;
end;

{!~RegSetIntegerValue:

Sets a variable (VarName) in the registry to the value
VarValue.  The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
//
Unit Description UnitIndex Master Index
Function RegSetIntegerValue(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String;
  const VarValue: Integer): Boolean;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Try
      Reg.RootKey := RootKey;
      Reg.OpenKey(SubKey,True);
      Reg.WriteInteger('', VarName, VarValue);
      Result := True;
    Except
      Result := False;
    End;
  Finally
    Reg.Free;
  End;
end;

{!~RegSetBooleanValue:

Sets a variable (VarName) in the registry to the value
VarValue.  The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
//
Unit Description UnitIndex Master Index
Function RegSetBooleanValue(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String;
  const VarValue: Boolean): Boolean;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Try
      Reg.RootKey := RootKey;
      Reg.OpenKey(SubKey,True);
      Reg.WriteBool('', VarName, VarValue);
      Result := True;
    Except
      Result := False;
    End;
  Finally
    Reg.Free;
  End;
end;

{!~RegSetExpandStringValue:

Call RegSetExpandStringValue to store a string that contains
unexpanded references to environment variables such as
“%PATH%”.

Sets a variable (VarName) in the registry to the value
VarValue.  The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
//
Unit Description UnitIndex Master Index
Function RegSetExpandStringValue(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String;
  const VarValue: String): Boolean;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Try
      Reg.RootKey := RootKey;
      Reg.OpenKey(SubKey,True);
      Reg.WriteExpandString(VarName, VarValue);
      Result := True;
    Except
      Result := False;
    End;
  Finally
    Reg.Free;
  End;
end;

{!~RegGetStringValue:

Gets the value of a variable (VarName) in the registry.
If the variable does not exist or is null the value
of VarDefault is returned.

The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.}
//
Unit Description UnitIndex Master Index
Function RegGetStringValue(
  const RootKey   : HKEY;
  const SubKey    : String;
  const VarName   : String;
  const VarDefault: String): String;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Reg.RootKey := RootKey;
    Reg.OpenKey(SubKey,True);
    Result :=
      Reg.
        ReadString
          ('', VarName, VarDefault);
  Finally
    Reg.Free;
  End;
end;

{!~RegGetIntegerValue:

Gets the value of a variable (VarName) in the registry.
If the variable does not exist or is null the value
of VarDefault is returned.

The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.}
//
Unit Description UnitIndex Master Index
Function RegGetIntegerValue(
  const RootKey   : HKEY;
  const SubKey    : String;
  const VarName   : String;
  const VarDefault: Integer): Integer;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Reg.RootKey := RootKey;
    Reg.OpenKey(SubKey,True);
    Result :=
      Reg.
        ReadInteger
          ('', VarName, VarDefault);
  Finally
    Reg.Free;
  End;
end;


{!~RegGetBooleanValue:

Gets the value of a variable (VarName) in the registry.
If the variable does not exist or is null the value
of VarDefault is returned.

The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.}
//
Unit Description UnitIndex Master Index
Function RegGetBooleanValue(
  const RootKey   : HKEY;
  const SubKey    : String;
  const VarName   : String;
  const VarDefault: Boolean): Boolean;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Reg.RootKey := RootKey;
    Reg.OpenKey(SubKey,True);
    Result :=
      Reg.
        ReadBool
          ('', VarName, VarDefault);
  Finally
    Reg.Free;
  End;
end;

{!~RegDeleteVariable:

Deletes a variable (VarName) in the registry.
The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  If an exception is
thrown the function returns False, True otherwise.}
//
Unit Description UnitIndex Master Index
Function RegDeleteVariable(
  const RootKey : HKEY;
  const SubKey  : String;
  const VarName : String): Boolean;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Try
      Reg.RootKey := RootKey;
      Reg.OpenKey(SubKey,True);
      Reg.DeleteKey('', VarName);
      Result := True;
    Except
      Result := False;
    End;
  Finally
    Reg.Free;
  End;
end;

{!~RegDeleteSection:

DANGEROUS!!! BE CAREFUL!!!

Deletes a complete section from the registry.
The Registry's RootKey must be provided as the
RootKey value, e.g., HKEY_LOCAL_MACHINE (note no quotes,
double quotes or brackets).  Subkey is the key path under
the RootKey, e.g.,'\Software\MyApp'.  The SubKey will
be removed with all associated values.  If an exception is
thrown the function returns False, True otherwise.}
//
Unit Description UnitIndex Master Index
Function RegDeleteSection(
  const RootKey : HKEY;
  const SubKey  : String): Boolean;
Var
  Reg : TRegIniFile;
begin
  Reg := TRegIniFile.Create('');
  Try
    Try
      Reg.RootKey := RootKey;
      Reg.OpenKey(SubKey,True);
      Reg.EraseSection('');
      Result := True;
    Except
      Result := False;
    End;
  Finally
    Reg.Free;
  End;
end;

End.
//