Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Imports System
Imports System.Windows.Forms
Imports System.Windows
Imports gmRTL.Core
Namespace ScanToolLib
   Public Class clsVBPBinReporter
   ' clsVBPBinReporter: Report binary build product for VBP file

   Public Class statusEventArgs : Inherits System.EventArgs
      Public msg As String
      Public Sub New(ByVal msg As String)
         Me.msg = msg
      End Sub
   End Class
   Public Delegate Sub statusEventHandler(sender as Object,e As statusEventArgs)
   Public Event status_Event As EventHandler(Of statusEventArgs)
   Protected Overridable Sub Onstatus_Event(msg As String)
      Dim e As statusEventArgs
      e = New statusEventArgs(msg)
      RaiseEvent status_Event(Me, e)
   End Sub
   Public Function header() As String
      header = "VBPPATH" & vbTab & "VBPNAME" & vbTab & "EXETYPE" & vbTab & "EXENAME" & vbTab & "MAJVERS" & vbTab & "MINVERS"
   End Function
   Public Sub processFile(ByVal fil As System.IO.FileInfo,ByVal rpt As System.IO.StreamWriter)

      Const FUNC_NAME As String = "processFile"
      Dim txtfil As System.IO.StreamReader = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         Dim filpath As String = ""
         filpath = fil.Directory.FullName
         Dim filname As String = ""
         filname = fil.Name
         Dim exetype As String = ""
         exetype = "-"
         Dim exename As String = ""
         exename = "-"
         Dim majvers As String = ""
         majvers = "-"
         Dim minvers As String = ""
         minvers = "-"


         txtfil = modScanToolLib.openInputText(fil.FullName)

         Dim s As String = ""
         s = txtfil.ReadToEnd()

         Dim arr As String() = Array.Empty(Of String)()
         arr = Strings.Split(s,Environment.NewLine,-1,CompareMethod.Binary)

         For Each v As String In arr

            Dim sLine As String = ""
            sLine = Strings.LCase(v)

            If (sLine Like "type=*") Then
               exetype = parse1(sLine)
            ElseIf (sLine Like "exename32*") Then
               exename = parse1(sLine)
            ElseIf (sLine Like "majorver*") Then
               majvers = parse1(sLine)
            ElseIf (sLine Like "minorver*") Then
               minvers = parse1(sLine)
            End If

         Next

         modScanToolLib.writeRpt(rpt,filpath & vbTab & filname & vbTab & exetype & vbTab & exename & vbTab & majvers & vbTab & minvers & Environment.NewLine)

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
      End Try

      If (Not (txtfil Is Nothing)) Then
         txtfil.Close()
      End If
      txtfil = Nothing

      If (Err.Number <> 0) Then
         Err.Raise(Err.Number,String.Empty,TypeName(Me) & "::" & FUNC_NAME & " " & Environment.NewLine & "[" & fil.FullName & "]" & Environment.NewLine & Err.Description)
      End If

   End Sub
   Private Function parse1(ByVal sLine As String) As String
      parse1 = ""

      Const Q As String = """"
      Dim i As Integer = 0
      Dim j As Integer = 0

      j = Strings.InStrRev(sLine,"\",-1,CompareMethod.Binary)
      If (j > 0) Then
         j = j + 1
      Else
         j = Strings.InStr(1,sLine,"=",CompareMethod.Binary) + 1
      End If

      parse1 = Strings.Replace(Strings.Trim(Mid(sLine,j)),Q,"")

   End Function
   End Class
End Namespace
