unit GeneratorClass;

interface

uses classes, sysUtils;

type
  TGenerator = Class
  public
    Fe : double;
    Used : boolean;
    FloatOrInt : string;
    Values : array of double;
    FileName : string;
    constructor CreateGenerator(pFe : double);
    procedure ReadFromFile(pFileName : string);
    procedure SaveToFile;
    function Length : integer;
    procedure FreeValues;
    procedure SetDuree(pDuree : double);
    procedure AddValue(pValue : double);
    function  GetValue(pINDEX : integer) : double;
    procedure ImportFromTextFile(pFileName : string);
    procedure ImportFromBinFile(pFileName : string);
  end;

implementation

uses UDocometre,XMLClasses;

constructor TGenerator.CreateGenerator(pFe : double);
begin
  inherited Create;
  Fe := pFe;
  Used := true;
  SetLength(Values,1);
end;

procedure TGenerator.ReadFromFile(pFileName : string);
var SimFile : TFileStream;
    i : integer;
begin
  Self.FileName := pFileName;
  SimFile := TFileStream.Create(pFileName,fmOpenRead);
  SetLength(Values,SimFile.Size div 8);
  SimFile.Read(Values[0],SimFile.Size);
  SimFile.Free;
  if Self.FloatOrInt = 'int' then
  begin
    for i := 0 to Self.Length - 1
    do if Values[i] >= 1 then Values[i] := 1 else Values[i] := 0;
  end;
end;

procedure TGenerator.SaveToFile;
var SimFile : TFileStream;
begin
  SimFile := TFileStream.Create(Self.FileName,fmCreate);
  SimFile.Write(Values[0],(high(Values)+1)*8);
  SimFile.Free;
end;

function TGenerator.Length : integer;
begin
  result := high(Values) + 1;
end;

procedure TGenerator.FreeValues;
begin
  Values := nil;
end;

procedure TGenerator.AddValue(pValue : double);
begin
  SetLength(Values,high(Values) + 1);
  Values[high(Values)] := pValue;
end;

procedure TGenerator.SetDuree(pDuree : double);
var M : integer;
begin
  M := trunc(pDuree*Fe/1000) + 1;
  SetLength(Values,M);
end;

function TGenerator.GetValue(pINDEX : integer) : double;
var n1,n2,RF : integer;
begin
  RF := trunc(DocDOCOMETRE.EXPERIENCE.FEG / Fe);
  n1 := (pINDEX - 1) mod RF;
  n2 := (pINDEX - 1) div RF;
  if n1 <> 0
  then result := 0.0
  else if n2 > high(Values)
       then result := 0.0
       else result := Values[n2];
end;

procedure TGenerator.ImportFromTextFile(pFileName : string);
var TextFile : TStringList;
    i : integer;
begin
  TextFile := TStringList.Create;
  TextFile.Sorted := false;
  try
    TextFile.LoadFromFile(pFileName);
    if TextFile.Count > Length then SetLength(Values,TextFile.Count);
    for i := 0 to Length - 1 do
    begin
      if i < TextFile.Count
      then Values[i] := strtofloat(TextFile[i])
      else Values[i] := 0.0;
    end;
    if Self.FloatOrInt = 'int' then
    begin
      for i := 0 to Self.Length - 1
      do if Values[i] >= 1 then Values[i] := 1 else Values[i] := 0;
    end;
  finally
    TextFile.Free;
  end;
end;

procedure TGenerator.ImportFromBinFile(pFileName : string);
var BinFile : TFileStream;
    M,i : integer;
    SingleValues : array of single;
begin
  BinFile := TFileStream.Create(pFileName,fmOpenRead);
  try
    M := BinFile.Size;
    SetLength(SingleValues,M div 4);//Les fichiers sau sont en single (32 bits - 4 octets)
    BinFile.Read(SingleValues[0],M);
    if (M div 4) > Length then SetLength(Values,M div 4);
    for i := 0 to Length - 1 do
    begin
      if i < (M div 4) - 1
      then Values[i] := SingleValues[i]
      else Values[i] := 0.0;
    end;
    if Self.FloatOrInt = 'int' then
    begin
      for i := 0 to Self.Length - 1
      do if Values[i] >= 1 then Values[i] := 1 else Values[i] := 0;
    end;
  finally
    BinFile.Free;
  end;
end;

end.
