Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace ScanToolLib
   Public Class clsVBPSrcReporter
   ' clsVBPSrcReporter: Report Source Code for all modules referenced by a 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 & "MODNAME" & vbTab & "MODTIME" & vbTab & "MODTYPE" & vbTab & "LINENUM" & vbTab & "LINETYPE" & vbTab & "LINETEXT"
   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 modtype As String = ""
         Dim modname As String = ""
         Dim modpath As String = ""


         txtfil = modScanToolLib.openInputText(fil.FullName)

         Dim l As Integer = 0
         l = 1

         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

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

            If (sLine Like "form=*.frm" Or sLine Like "class=*.cls" Or sLine Like "module=*.bas" Or sLine Like "usercontrol=*.ctl" Or sLine Like "propertypage=*.pag") Then

               sLine = v

               Dim j As Integer = 0
               Dim i As Integer = 0

               i = Strings.InStr(1,sLine,"=",CompareMethod.Binary) + 1

               modtype = Strings.Left(sLine,i - 2)

               sLine = Strings.Trim(Mid(sLine,i)) ' object name; path

               j = Strings.InStrRev(sLine,";",-1,CompareMethod.Binary)

               If (j = 0) Then
                  j = 1
               Else
                  j = j + 1
               End If

               sLine = Strings.Trim(Mid(sLine,j)) ' object path

               j = Strings.InStrRev(sLine,"\",-1,CompareMethod.Binary)

               If (j = 0) Then
                  j = 1
               Else
                  j = j + 1
               End If

               modname = Strings.Trim(Mid(sLine,j)) ' file name

               If (j = 1) Then
                  modpath = filpath & "\" & modname
               Else
                  modpath = sLine
               End If

               addLines(rpt,filpath,filname,modpath,modname,modtype)

            Else

               modScanToolLib.writeRpt(rpt,filpath & vbTab & fil.Name & vbTab & fil.Name & vbTab & fil.LastWriteTime & vbTab & "Project" & vbTab & l & vbTab & "PRJ" & vbTab & v & Environment.NewLine)

               l = l + 1

            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
   Private Sub addLines(ByVal rpt As System.IO.StreamWriter,ByVal vbppath As String,ByVal vbpname As String,ByVal modpath As String,ByVal modname As String,ByVal modtype As String)
      Const FUNC_NAME As String = "addLines"
      Dim txtfil As System.IO.StreamReader = Nothing
      Dim fil As System.IO.FileInfo = Nothing
      Dim l As Integer = 0
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()



         '----------------------------------------------------
         ' open the module
         '----------------------------------------------------

         txtfil = modScanToolLib.openInputText(modpath)

         '----------------------------------------------------
         ' parse the module into an array of lines
         '----------------------------------------------------

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

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

         '----------------------------------------------------
         ' default the line type for forms/ctls to GUI since
         ' the GUI spec is always at the top of the form/ctl.
         '----------------------------------------------------

         Dim linetype As String = ""

         If (modtype = "Form" Or modtype = "UserControl") Then
            linetype = "GUI"
         Else
            linetype = "COD"
         End If

         '----------------------------------------------------
         ' get modified date
         '----------------------------------------------------

         Dim LastModDate As DateTime = New DateTime(0)
         fil = new System.IO.FileInfo(modpath)
         LastModDate = fil.LastWriteTime

         '----------------------------------------------------
         ' process each line
         '----------------------------------------------------

         l = 1

         For Each v As String In arr

            Dim sLine As String = ""
            sLine = Strings.Replace(v,vbTab,"    ")

            Dim trimmedLine As String = ""
            trimmedLine = Strings.Trim(sLine)


            Dim sType As String = ""
            If (Strings.Left(trimmedLine,1) = "'") Then
               sType = "CMN"
            ElseIf (Strings.Len(trimmedLine) = 0) Then
               sType = "BLN"
            Else
               sType = linetype
            End If

            modScanToolLib.writeRpt(rpt,vbppath & vbTab & vbpname & vbTab & modname & vbTab & LastModDate & vbTab & modtype & vbTab & l & vbTab & sType & vbTab & sLine & Environment.NewLine)

            l = l + 1

            If (linetype = "GUI") Then
               If (sLine = "End") Then
                  linetype = "COD"
               End If
            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
      fil = Nothing
      txtfil = Nothing

      If (Err.Number <> 0) Then

         modScanToolLib.writeRpt(rpt,vbppath & vbTab & vbpname & vbTab & modname & vbTab & Now & vbTab & modtype & vbTab & l & vbTab & "ERR" & vbTab & modpath & ":" & Err.Description & Environment.NewLine)
      End If

   End Sub
   End Class
End Namespace
