Attribute VB_Name = "modScanToolLib"
Option Explicit
Const MOD_NAME = "modScanToolLib"

' here is a command line string for testing /optRpt=DirList /Root="c:\projects\" /LOG="ScanTool.log"

Public Function readNode(dom As DOMDocument60, xpath As String) As String
Const FUNC_NAME$ = "readNode"
On Error GoTo ErrorHandler
    
    Dim nod As MSXML2.IXMLDOMNode
    Set nod = dom.selectSingleNode(xpath)
    
    If nod.firstChild Is Nothing Then
        readNode = ""
    Else
        readNode = nod.firstChild.nodeValue
    End If
    
ErrorHandler:

    Set nod = Nothing

If Err Then _
    Err.Raise Err.Number, Description:=MOD_NAME & "::" & FUNC_NAME$ & " line= " & Erl & Err.Description

End Function


Public Function writeTextNode(elmRoot As IXMLDOMElement, name As String, value As String) As IXMLDOMElement
Const FUNC_NAME$ = "writeNode"
On Error GoTo ErrorHandler
    
    Dim elmNew
    Set elmNew = elmRoot.ownerDocument.createElement(name)
    elmRoot.appendChild elmNew
    elmNew.appendChild elmNew.ownerDocument.createTextNode(value)
    Set writeTextNode = elmNew
        
ErrorHandler:

    Set elmNew = Nothing

If Err Then _
    Err.Raise Err.Number, Description:=MOD_NAME & "::" & FUNC_NAME$ & " line= " & Erl & Err.Description

End Function

Public Function getPath(sFileName As String) As String
    
    Dim c As Integer
    c = InStrRev(sFileName, "\")
    If c > 0 Then
        getPath = Left$(sFileName, c)
    End If

End Function


Public Function filename(filepath As String) As String

    Const FUNC_NAME$ = "fileName"
    On Error GoTo ErrorHandler
    
    Dim c As Integer
    c = InStrRev(filepath, "\")
    If c > 0 Then filepath = Mid$(filepath, c + 1)
    c = InStrRev(filepath, ".")
    If c > 0 Then filepath = Left$(filepath, c - 1)
    
    filename = filepath
            
ErrorHandler:
    
    If Err Then _
        Err.Raise Err.Number, Description:=MOD_NAME & "::" & FUNC_NAME$ & " " & Err.Description
End Function

Public Function openInputText(sinpFileName As String, fileSys As FileSystemObject) As Scripting.TextStream
Const FUNC_NAME$ = "openInputText"
On Error GoTo ErrorHandler

        Set openInputText = Nothing
    Set openInputText = fileSys.OpenTextFile(sinpFileName, _
    IOMode:=ForReading, Create:=False)
    
ErrorHandler:
If Err Then _
    Err.Raise Err.Number, Description:=MOD_NAME & "::" & FUNC_NAME$ & " line= " & Erl & Err.Description
End Function
    

Public Function openOutputText(soutFileName As String, fileSys As FileSystemObject) As Scripting.TextStream
Const FUNC_NAME$ = "openOutputText": On Error GoTo ErrorHandler
    
    Set openOutputText = Nothing
    Set openOutputText = fileSys.OpenTextFile(soutFileName, _
    IOMode:=ForWriting, Create:=True)

ErrorHandler:
If Err Then _
    Err.Raise Err.Number, Description:=MOD_NAME & "::" & FUNC_NAME$ & " " & Err.Description

End Function

Public Function RInStr(sSource As String, sSearch As String) As Integer
    
    Dim i As Integer

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

    RInStr = 0

Exit_RInStr:
    Exit Function
          
End Function

Function Chr1Tran(ByVal sSrc As String, ByVal sOld As String, ByVal sNew As String) As String
    Dim i, l As Integer
    Dim c As String * 1
    
    If sNew <> "" Then
        If sOld <> sNew And sOld <> "" Then
            i = 1
            Do
                i = InStr(i, sSrc, sOld)
                If i = 0 Then Exit Do
                Mid$(sSrc, i, 1) = sNew
            Loop
        End If
        Chr1Tran = sSrc
    Else
        Dim sOutput As String
        sOutput = ""
        l = Len(sSrc)
        For i = 1 To l
            c = Mid(sSrc, i, 1)
            If c <> sOld Then sOutput = sOutput & c
        Next
        Chr1Tran = sOutput
    End If
End Function

Public Sub writeRpt(rpt As TextStream, str As String)
    Debug.Print str
    Call rpt.Write(str)
End Sub
