Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace ScanToolLib
   Public Class clsDirList
   ' clsDirList: Generic Directory Search

   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
   Private mtxtFilter As String = ""
   Private mtxtPats As String() = Array.Empty(Of String)()
   Private mtxtPatCnt As Integer = 0
   Public noCase As Boolean = False
   Public Function header() As String
      header = ""
      If (Strings.Len(mtxtFilter) = 0) Then
         header = "PATH" & vbTab & "FILE" & vbTab & "EXT" & vbTab & "SIZE" & vbTab & "CREATED"
      Else
         header = "PATH" & vbTab & "FILE" & vbTab & "LINE" & vbTab & "TEXT"
      End If
   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


         txtfil = modScanToolLib.openInputText(fil.FullName)

         Dim i As Integer = 0
         If (Strings.Len(mtxtFilter) = 0) Then
            Dim sExt As String = ""
            i = Strings.InStrRev(filname,".",-1,CompareMethod.Binary)
            If (i > 0) Then
               sExt = Mid(filname,i + 1)
            Else
               sExt = "-"
            End If
            modScanToolLib.writeRpt(rpt,filpath & vbTab & filname & vbTab & sExt & vbTab & fil.Length & vbTab & fil.CreationTime & Environment.NewLine)
         Else
                                               ' FILTER FOR SPECIFIC TEXT
            Dim l As Integer = 0
            Dim s As String = ""
            Do While Not (txtfil.Peek() = -1)
               l = l + 1
               s = txtfil.ReadLine()
               For i = 0 To mtxtPatCnt - 1 Step 1
                  Dim ss As String = ""
                  Dim pt As String = ""
                  If (noCase) Then
                     ss = Strings.LCase(s)
                     pt = Strings.LCase(mtxtPats(i))
                  Else
                     ss = s
                     pt = mtxtPats(i)
                  End If
                  If (ss Like pt) Then
                     s = Strings.Replace(s,vbTab," ")

                     modScanToolLib.writeRpt(rpt,filpath & vbTab & filname & vbTab & l & vbTab & s & Environment.NewLine)

                  End If
               Next
            Loop
         End If

      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
   Public Property contentFilter() As String
      Get
         contentFilter = mtxtFilter
      End Get
      Set
         mtxtFilter = Value
         mtxtPats = Strings.Split(Strings.LCase(Value),"|",-1,CompareMethod.Binary)
         mtxtPatCnt = UBound(mtxtPats,1) + 1
      End Set
   End Property
   End Class
End Namespace
