unit CommonDataUnit;

interface
uses
  Classes;

type
  TRecQueryResult = record
    code : String;
    data : String;
    name : String;
  end;

  PRecResultDetail = ^TRecResultDetail;
  TRecResultDetail = record
    Key : String;
    Value : String;
  end;

  TQueryResultList = class(TList)
  protected
    procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  end;


procedure LoadIni;
procedure SaveIni;

function HexStrToStr(const HexStr : String) : String;

var
  g_FacilityID : String;
  g_SvrAddr : String;

implementation

uses
  IniFiles, SysUtils;

procedure LoadIni;
begin
  with TIniFile.Create('.\' + ChangeFileExt(ExtractFileName(ParamStr(0)), '') + '.ini') do
  try
    g_FacilityID := ReadString(UpperCase('CONFIG'), UpperCase('FACILITY_ID'), '');
    g_SvrAddr    := ReadString(UpperCase('CONFIG'), UpperCase('SERVER_ADDRESS'), '');
  finally
    Free;
  end;
end;

procedure SaveIni;
begin
  with TIniFile.Create('.\' + ChangeFileExt(ExtractFileName(ParamStr(0)), '') + '.ini') do
  try
    WriteString(UpperCase('CONFIG'), UpperCase('FACILITY_ID'), g_FacilityID);
  finally
    Free;
  end;
end;


function HexStrToStr(const HexStr : String) : String;
var
  tmp : String;
begin
  Assert(not Odd(Length(HexStr)), 'HexStr length must be an even number');
  SetLength(tmp, Length(HexStr) div 2);
  HexToBin(PChar(HexStr), @tmp[1], Length(tmp));
  Result := tmp;
end;

{ TQueryResultList }

procedure TQueryResultList.Notify(Ptr: Pointer;
  Action: TListNotification);
begin
  inherited;
  if Action = lnDeleted then
    Dispose(PRecResultDetail(Ptr));
end;

end.
