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 clsVBPRefReporter
   ' clsVBPRefReporter: Report COM references 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 & "LIBNAME" & vbTab & "LIBGUID" & vbTab & "MAJVERS" & vbTab & "MINVERS" & vbTab & "LIBPATH"
   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 libname As String = ""
         libname = "-"
         Dim libguid As String = ""
         libguid = "-"
         Dim majvers As String = ""
         majvers = "-"
         Dim minvers As String = ""
         minvers = "-"
         Dim libpath As String = ""
         libpath = "-"


         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)

            Dim inp As String() = Array.Empty(Of String)()
            Dim i As Integer = 0

            libguid = ""
            If (sLine Like "reference=*") Then
               inp = Strings.Split(sLine,"#",-1,CompareMethod.Binary)
               If (UBound(inp,1) = 4) Then
                  libguid = inp(0)
                  i = Strings.InStr(1,libguid,"{",CompareMethod.Binary)
                  libguid = Mid(libguid,i)
                  majvers = inp(1)
                  minvers = inp(2)
                  libpath = inp(3)
                  libname = inp(4)
               End If
            ElseIf (sLine Like "object=*") Then
               inp = Strings.Split(sLine,"#",-1,CompareMethod.Binary)
               If (UBound(inp,1) = 2) Then
                  libguid = inp(0)
                  i = Strings.InStr(1,libguid,"{",CompareMethod.Binary)
                  libguid = Mid(libguid,i)
                  majvers = inp(1)
                  minvers = inp(2)
                  inp = Strings.Split(minvers,";",-1,CompareMethod.Binary)
                  minvers = inp(0)
                  libpath = Mid(inp(1),2)
                  libname = libpath
               End If
            End If

            If (Strings.Len(libguid) > 0) Then

               modScanToolLib.writeRpt(rpt,filpath & vbTab & filname & vbTab & libname & vbTab & libguid & vbTab & majvers & vbTab & minvers & vbTab & libpath & Environment.NewLine)

            End If

         Next

      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
   End Class
End Namespace
