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 Module modScanToolLib
   Private Const MOD_NAME As String = "modScanToolLib"
   ' here is a command line string for testing /optRpt=DirList /Root="c:\projects\" /LOG="ScanTool.log"
   Public Function readNode(ByVal dom As System.Xml.XmlDocument,ByVal xpath As String) As String
      readNode = ""
      Const FUNC_NAME As String = "readNode"
      Dim nod As System.Xml.XmlNode = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         nod = dom.SelectSingleNode(xpath)

         If (nod.FirstChild Is Nothing) Then
            readNode = ""
         Else
            readNode = nod.FirstChild.Value
         End If

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

      nod = Nothing

      If (Err.Number <> 0) Then
         Err.Raise(Err.Number,String.Empty,MOD_NAME & "::" & FUNC_NAME & " line= " & Erl() & Err.Description)
      End If

   End Function
   Public Function writeTextNode(ByVal elmRoot As System.Xml.XmlElement,ByVal name As String,ByVal value As String) As System.Xml.XmlElement
      writeTextNode = Nothing
      Const FUNC_NAME As String = "writeNode"
      Dim elmNew As System.Xml.XmlElement = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         elmNew = elmRoot.OwnerDocument.CreateElement(name)
         elmRoot.AppendChild(elmNew)
         elmNew.AppendChild(elmNew.OwnerDocument.CreateTextNode(value))
         writeTextNode = elmNew

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

      elmNew = Nothing

      If (Err.Number <> 0) Then
         Err.Raise(Err.Number,String.Empty,MOD_NAME & "::" & FUNC_NAME & " line= " & Erl() & Err.Description)
      End If

   End Function
   Public Function getPath(ByVal sFileName As String) As String
      getPath = ""

      Dim c As Integer = 0
      c = Strings.InStrRev(sFileName,"\",-1,CompareMethod.Binary)
      If (c > 0) Then
         getPath = Strings.Left(sFileName,c)
      End If

   End Function
   Public Function filename(ByRef filepath As String) As String
      filename = ""

      Const FUNC_NAME As String = "fileName"
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         Dim c As Integer = 0
         c = Strings.InStrRev(filepath,"\",-1,CompareMethod.Binary)
         If (c > 0) Then
            filepath = Mid(filepath,c + 1)
         End If
         c = Strings.InStrRev(filepath,".",-1,CompareMethod.Binary)
         If (c > 0) Then
            filepath = Strings.Left(filepath,c - 1)
         End If

         filename = filepath

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

      If (Err.Number <> 0) Then
         Err.Raise(Err.Number,String.Empty,MOD_NAME & "::" & FUNC_NAME & " " & Err.Description)
      End If
   End Function
   Public Function openInputText(ByVal sinpFileName As String) As System.IO.StreamReader
      openInputText = Nothing
      Const FUNC_NAME As String = "openInputText"
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         openInputText = Nothing
         openInputText = new System.IO.StreamReader(new System.IO.FileStream(sinpFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite))

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
      End Try
      If (Err.Number <> 0) Then
         Err.Raise(Err.Number,String.Empty,MOD_NAME & "::" & FUNC_NAME & " line= " & Erl() & Err.Description)
      End If
   End Function
   Public Function openOutputText(ByVal soutFileName As String) As System.IO.StreamWriter
      openOutputText = Nothing
      Const FUNC_NAME As String = "openOutputText"
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         openOutputText = Nothing
         openOutputText = new System.IO.StreamWriter(new System.IO.FileStream(soutFileName,System.IO.FileMode.Create,System.IO.FileAccess.ReadWrite,System.IO.FileShare.ReadWrite))

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
      End Try
      If (Err.Number <> 0) Then
         Err.Raise(Err.Number,String.Empty,MOD_NAME & "::" & FUNC_NAME & " " & Err.Description)
      End If

   End Function
   Public Function RInStr(ByVal sSource As String,ByVal sSearch As String) As Integer
      RInStr = 0

      Dim i As Integer = 0

      For i = Strings.Len(sSource) To 1 Step -1
         If (Mid(sSource,i,1) = sSearch) Then
            RInStr = i
            Exit Function
         End If
      Next

      RInStr = 0

      Exit Function

   End Function
   Public Function Chr1Tran(ByVal sSrc As String,ByVal sOld As String,ByVal sNew As String) As String
      Chr1Tran = ""
      Dim i As Integer = 0
      Dim l As Integer = 0
      Dim c As String = ""

      If (sNew <> "") Then
         If (sOld <> sNew And sOld <> "") Then
            i = 1
            Do
               i = Strings.InStr(i,sSrc,sOld,CompareMethod.Binary)
               If (i = 0) Then
                  Exit Do
               End If
               sSrc = gmRTL.Core.StringHelper.MidCmd(sSrc,sNew,i,1)
            Loop
         End If
         Chr1Tran = sSrc
      Else
         Dim sOutput As String = ""
         sOutput = ""
         l = Strings.Len(sSrc)
         For i = 1 To l Step 1
            c = Mid(sSrc,i,1)
            If (c <> sOld) Then
               sOutput = sOutput & c
            End If
         Next
         Chr1Tran = sOutput
      End If
   End Function
   Public Sub writeRpt(ByVal rpt As System.IO.StreamWriter,ByVal str As String)
      Debug.WriteLine(str)
      rpt.Write(str)
   End Sub
   End Module
End Namespace
