unit StandardLibs;

interface

uses Classes, StandardLib, ZhAPI, ConfigDoc;

type
  TStandardLibs = class
  private
    FConfigInfo: TConfigInfo;
    FLibs: TList;

    function GetStandardGclLib: TStandardLib;
    function GetStandardXmLib: TStandardLib;

    function FindLib(const AFileName: string): TStandardLib;
    function CreateLib(const AFileName: string): TStandardLib;
    procedure ClearLibs;

    procedure OpenDefaultStandardLibs;
  public
    constructor Create(AConfigInfo: TConfigInfo);
    destructor Destroy; override;

    function GetLib(const AFileName: string): TStandardLib;

    property StandardXmLib: TStandardLib read GetStandardXmLib;
    property StandardGclLib: TStandardLib read GetStandardGclLib;
  end;

implementation

{ TStandardLibs }

procedure TStandardLibs.ClearLibs;
begin
  ClearObjects(FLibs);
end;

constructor TStandardLibs.Create(AConfigInfo: TConfigInfo);
begin
  FConfigInfo := AConfigInfo;
  FLibs := TList.Create;
  OpenDefaultStandardLibs;
end;

function TStandardLibs.CreateLib(const AFileName: string): TStandardLib;
begin
  Result := TStandardLib.Create;
  Result.Open(AFileName);
  FLibs.Add(Result);
end;

destructor TStandardLibs.Destroy;
begin
  ClearLibs;
  FLibs.Free;
  inherited;
end;

function TStandardLibs.FindLib(const AFileName: string): TStandardLib;
var
  I: Integer;
begin
  for I := 0 to FLibs.Count - 1 do
  begin
    Result := TStandardLib(FLibs.List^[I]);
    if Result.FileName = AFileName then
      Exit;
  end;
  Result := nil;
end;

function TStandardLibs.GetLib(const AFileName: string): TStandardLib;
begin
  Result := FindLib(AFileName);
  if Result = nil then
    Result := CreateLib(AFileName);
end;

function TStandardLibs.GetStandardGclLib: TStandardLib;
begin
  Result := GetLib(FConfigInfo.StandardGclLib);
end;

function TStandardLibs.GetStandardXmLib: TStandardLib;
begin
  Result := GetLib(FConfigInfo.StandardXmLib);
end;

procedure TStandardLibs.OpenDefaultStandardLibs;
begin
  GetLib(FConfigInfo.StandardXmLib);
  GetLib(FConfigInfo.StandardGclLib);
end;

end.
