unit Config;

interface

uses
  // Borland Software Corporation
  Windows, Registry, ADCDevice;

type

//----------------------------------------------------------------------------
//  TConfig = class(TRegistry); Класс данных настроек
//----------------------------------------------------------------------------
  PGeneralItem = ^TGeneralItem;
  TGeneralItem = record
    Channel : array [0..CHANNEL_COUNT - 1] of boolean;
    Gain    : array [0..CHANNEL_COUNT - 1] of integer;
    Round   : array [0..CHANNEL_COUNT - 1] of byte;
  end;

  TConfiguration = class(TRegistry)
  private
    FKey       : string;
    FGeneral   : PGeneralItem;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Load;
    procedure Save;

    property General : PGeneralItem read FGeneral write FGeneral;
    property Key : string read FKey;
  end; //  TConfig = class(TRegistry)


var
  Configuration : TConfiguration;

implementation

//----------------------------------------------------------------------------
//  TConfig = class(TRegistry); Класс данных настроек
//----------------------------------------------------------------------------

constructor TConfiguration.Create;
begin
  inherited Create;
  RootKey := HKEY_CURRENT_USER;
  FKey := '\Software\Spetspribor\Data Logger\Plugins\ADCx148D.dll';
  New(FGeneral);

  Load;
end;

destructor TConfiguration.Destroy;
begin
  Save;
  Dispose(FGeneral);
  inherited Destroy;
end;

procedure TConfiguration.Load;
var
  i : integer;
  GeneralSize : integer;
begin
  if OpenKey(FKey, true) then
  begin
    if ValueExists('GeneralSize') then
        GeneralSize := ReadInteger('GeneralSize')
    else
        GeneralSize := 0;

    if GeneralSize <> sizeof(TGeneralItem) then
    begin
      for i := 0 to CHANNEL_COUNT - 1 do
      begin
        FGeneral^.Channel[i] := true;
        FGeneral^.Gain[i]    := 0;
        FGeneral^.Round[i]   := 3;
      end;
    end
    else
        ReadBinaryData('General', FGeneral^, sizeof(TGeneralItem));


    CloseKey;
  end;

end;

procedure TConfiguration.Save;
begin
 if OpenKey(FKey, true) then
  begin
    WriteBinaryData('General', FGeneral^, sizeof(TGeneralItem));
    WriteInteger('GeneralSize', sizeof(TGeneralItem));

    CloseKey();
  end;
end;


initialization
  Configuration := TConfiguration.Create;

finalization
  Configuration.Free;

end.
