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 clsVBPCntReporter
   ' clsVBPCntReporter: Report Count of Line types 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 & "COD" & vbTab & "GUI" & vbTab & "REF" & vbTab & "CMN" & vbTab & "BLN"
   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 lCOD As Integer = 0
         Dim lREF As Integer = 0

         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 s As String = ""
         s = txtfil.ReadToEnd()

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

         lCOD = UBound(arr,1)

         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
               '----------------------------------------------------
               ' parse the reference for name path etc.
               '----------------------------------------------------

               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

               '----------------------------------------------------
               ' open file and count its lines
               '----------------------------------------------------

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

            Else
                                                  ' a project directive

               If (Strings.Left(v,7) = "Object=" Or Strings.Left(v,10) = "Reference=") Then
                  lREF = lREF + 1
               End If
            End If

         Next

         '----------------------------------------------------
         ' report totals for project
         '----------------------------------------------------

         modScanToolLib.writeRpt(rpt,filpath & vbTab & fil.Name & vbTab & fil.Name & vbTab & fil.LastWriteTime & vbTab & "Project" & vbTab & lCOD & vbTab & 0 & vbTab & lREF & vbTab & 0 & vbTab & 0 & 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 Sub countLines(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 = "countLines"
      Dim lCMN As Integer = 0
      Dim lBLN As Integer = 0
      Dim lCOD As Integer = 0
      Dim lGUI As Integer = 0
      Dim lREF As Integer = 0
      Dim txtfil As System.IO.StreamReader = Nothing
      Dim fil As System.IO.FileInfo = Nothing
      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
         '----------------------------------------------------

         Dim l As Integer = 0
         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)

            If (Strings.Left(trimmedLine,1) = "'") Then
               lCMN = lCMN + 1
            ElseIf (Strings.Len(trimmedLine) = 0) Then
               lBLN = lBLN + 1
            ElseIf (Strings.Left(trimmedLine,9) = "Object = ") Then
               lREF = lREF + 1
            Else
               If (linetype = "GUI") Then
                  lGUI = lGUI + 1
                  If (sLine = "End") Then
                     linetype = "COD"
                  End If
               Else
                  lCOD = lCOD + 1
               End If
            End If

         Next

         modScanToolLib.writeRpt(rpt,vbppath & vbTab & vbpname & vbTab & modname & vbTab & LastModDate & vbTab & modtype & vbTab & lCOD & vbTab & lGUI & vbTab & lREF & vbTab & lCMN & vbTab & lBLN & Environment.NewLine)

      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 & modpath & ":" & Err.Description & vbTab & lCOD & vbTab & lGUI & vbTab & lREF & vbTab & lCMN & vbTab & lBLN & Environment.NewLine)
      End If

   End Sub
   End Class
End Namespace
