//==============================================================================
//
//
//  Content        : Converter reads DFT-DFI and .001 to .999 files, converts
//                   them to DFQ files ans moves them during Upload to a target
//                   main folder.
//
//  Created on 	   : 11/2016
//
//  Author     	   : MB
//
//  Version        :(V1)
//                  a DFI File must be at least exist,so that the uploader  is
//                  active  and able to upload the data.
//
//
//
//
//
//
//
//==============================================================================



unit Upload_DFI_DFT_K00xx_Files_Conv;


const

WAIT_TIME_MILLSEC_C = 0001;





function GetFilenames( SourceFile : String;
                       FileSL     : TStringList) : Integer;
var
  A              : Integer;
  FileName,
  FileNameWOExt,
  FileNameFrac : String;
  PathStr     :  String;
begin
  if ( SourceFile <> '') then
  begin
    // (1)




    FileSL.Add( SourceFile);
    PathStr := ExtractFilePath( SourceFile);


    // (2)

    FileName := ExtractFileName( SourceFile);

    FileNameWOExt := Copy( FileName, 1, Pos( '.', Filename) - 1);







    FileName := IncludeTrailingPathDelimiter( PathStr) + FileNameWOExt + '.DFT';
    if FileExists( FileName) then
    begin

      FileSL.Add( FileName);


    end;

    //========
    //======== (4.1)
    //
    for A := 1 to  999 do
    begin
      if ( A <= 9 )  then  //
      begin
        FileName := IncludeTrailingPathDelimiter( PathStr) + FileNameWOExt + '.00' + IntToStr(A);
      end else
      if (A >= 10) and (A <= 99) then //
      begin
        FileName := IncludeTrailingPathDelimiter( PathStr) + FileNameWOExt + '.0' + IntToStr(A);
      end;
      if FileExists( FileName) then
      begin
        //
        FileSL.Add( FileName);
      end;
      result := 0;
    end;

    //======== (4.2)











  end else
  begin
    result := 1;
  end;
end;



//..............................................................................




function Main( QSystem        : Variant;
               SourceFileName : Variant;
               DestFolder     : Variant;
               LogFileName    : Variant;
               SaveFileName   : Variant;
               ConvResult     : TConvertResult): AnsiString;
var
  FileList : TStringList;
  rc       : Integer;
  QDASConv : TQDASTextConversion;
  QDASInterface : TQSSTATConverterInterface;

begin


 FileList := TStringList.Create;

  if (ConvResult <> nil) then
  begin
    ConvResult.ScriptVersion := '2016-12-12';
  end;
  if ( SourceFileName <> '') then
  begin
    rc := GetFilenames( UpperCase(SourceFileName), FileList);
    if ( rc = 0) then
    begin
      if (ConvResult <> nil) then
      begin
        ConvResult.SourceFiles.Text := FileList.Text;
      end;

      if ( FileList <> nil)  then
      begin
        try
        // (4.1)
          if  FileExists(ChangeFileExt(SourceFileName, '.DFI')) then
          begin
              delay(WAIT_TIME_MILLSEC_C);
              QDASConv := TQDASTextConversion.Create(QSystem);
              QDASInterface := TQSSTATConverterInterface.Create( QSystem);
              try
                QDASInterface.Open_QDASFile(SourceFileName);
                QDASInterface.TargetDir := DestFolder;

                QDASInterface.SaveToDFQFile(ChangeFileExt(SourceFileName,'.DFQ'));

                if QDASInterface.LastDestFile <> '' then
                begin
                  ConvResult.ConvFiles.Text := QDASInterface.LastDestFile;
                  ConvResult.ConvResult := 0;
                end;

                QDASInterface.ResetQSSTAT;
              finally

                QDASInterface.Free;
                QDASInterface := NIL;
                QDASConv.Free;
                QDASConv := NIL;
              end;
          end else //
          // (4.2)


          begin
            // (4.1)
            ConvResult.ErrorText :=  '- DFI must be exist at least!';

            // (4.2)


            ConvResult.ConvResult := -58;
            result := '';
          end;
        finally
          FileList.Free;
        end;
      end else
      begin
      //
        ConvResult.ErrorText :=  '- File list not assigned!';
        ConvResult.ConvResult := -1;
      end;
    end;
  end;
end;

function CheckIsFile( QSystem        : Variant;
                      SourceFileName : Variant) : Integer;
begin
  Result := -1;
  if Pos('.DFI',AnsiUpperCase(ExtractFileName( SourceFileName))) > 0 then
  begin
    Result := 0;
  end;
end;

end.
//check
//Gq?q8@/,PU13Qd)Y!Nprl,2889t3"I=b
