' Compare one file with another
' ie. useful to check whether one file is embedded in another.

$APPTYPE GUI
$TYPECHECK ON
$INCLUDE "rapidq.inc"


DECLARE SUB BrowseButton1Click
DECLARE SUB BrowseButton2Click
DECLARE SUB StartButtonClick

CREATE Form AS QForm
  Caption = "File comparison"
  Width = 320
  Height = 170
  BorderStyle = bsDialog
  Center
  CREATE Label1 AS QLabel
    Left = 10
    Top = 8
    Caption = "File 1:"
  END CREATE
  CREATE Edit1 AS QEdit
    Left = 50
    Width = 160
    Top = 5
  END CREATE
  CREATE Button1 AS QButton
    Left = 225
    Top = 5
    Caption = "Browse..."
    Height = 22
    OnClick = BrowseButton1Click
  END CREATE
  CREATE Label2 AS QLabel
    Left = 10
    Top = 45
    Caption = "File 2:"
  END CREATE
  CREATE Edit2 AS QEdit
    Left = 50
    Width = 160
    Top = 40
  END CREATE
  CREATE Button2 AS QButton
    Left = 225
    Top = 40
    Caption = "Browse..."
    Height = 22
    OnClick = BrowseButton2Click
  END CREATE
  CREATE Label3 AS QLabel
    Left = 10
    Width = 205
    Height = 20
    Top = 85
    Caption = "Click button to start search"
  END CREATE
  CREATE StartButton AS QButton
    Left = 225
    Top = 80
    Caption = "Start"
    Height = 22
    OnClick = StartButtonClick
  END CREATE
  ShowModal
END CREATE


SUB BrowseButton1Click
  DIM OpenDialog AS QOpenDialog

  OpenDialog.InitialDir = CURDIR$
  IF OpenDialog.Execute THEN
    Edit1.Text = OpenDialog.FileName
  END IF
END SUB

SUB BrowseButton2Click
  DIM OpenDialog AS QOpenDialog

  OpenDialog.InitialDir = CURDIR$
  IF OpenDialog.Execute THEN
    Edit2.Text = OpenDialog.FileName
  END IF
END SUB

SUB StartButtonClick
  DIM File1 AS QFileStream
  DIM File2 AS QFileStream
  DIM I AS INTEGER

  IF File1.Open(Edit1.Text, fmOpenRead) = False THEN
    ShowMessage("Could not open file "+Edit1.Text)
    EXIT SUB
  END IF

  IF File2.Open(Edit2.Text, fmOpenRead) = False THEN
    ShowMessage("Could not open file "+Edit2.Text)
    EXIT SUB
  END IF

  IF File1.Size > File2.Size THEN
    I = INSTR(File1.ReadStr(File1.Size), File2.ReadStr(File2.Size))
    IF I THEN
      Label3.Caption = Edit2.Text+CHR$(13)+"Found at position "+STR$(I)
    ELSE
      Label3.Caption = "No match found"
    END IF
  ELSE
    I = INSTR(File2.ReadStr(File2.Size), File1.ReadStr(File1.Size))
    IF I THEN
      Label3.Caption = Edit1.Text+CHR$(13)+"Found at position "+STR$(I)
    ELSE
      Label3.Caption = "No match found"
    END IF
  END IF

  File1.Close
  File2.Close
END SUB
