Attribute VB_Name = "StringUtilities"
Option Explicit

Public Function ConvertSecondToTime(eSecond As Long) As String
    Dim H, m, s As Long
    Select Case eSecond
        Case Is < 60
            ConvertSecondToTime = CDate(Format(0, "00") & ":" & Format(0, "00") & ":" & Format(eSecond, "00"))
        Case Else
            m = Int(eSecond / 60)
            H = Int(eSecond / 3600)
            s = Int(eSecond / 60)
            ConvertSecondToTime = Format(Int(eSecond / 3600), "00") & ":" & Format(Int(eSecond / 60), "00") & ":" & Format(Int(eSecond - Int(eSecond / 3600) - (Int(eSecond / 60) * 60)), "00")
    End Select
End Function

Public Function CompareStrings(ByVal sString1 As String, ByVal sString2 As String) As Boolean
    Dim Str1 As String
    Dim Str2 As String
    Str1 = sString1
    Str2 = sString2
    If Str1 <> "" Then
        If UCase(Left(Str1, Len(Str1))) = UCase(Left(Str2, Len(Str1))) Or UCase(Right(Str1, Len(Str1))) = UCase(Right(Str2, Len(Str1))) Then
            CompareStrings = True
            Exit Function
        End If
    End If
End Function

Public Function CompareNumbers(ByVal lNum1 As Double, ByVal lParam1 As Double, ByVal lParam2 As Double) As Boolean
    If lNum1 >= lParam1 And lNum1 <= lParam2 Then
        CompareNumbers = True
    ElseIf lNum1 <= lParam1 And lNum1 >= lParam2 Then
        CompareNumbers = True
    End If
End Function

Public Function IsInString(ByVal sText1 As String, ByVal sText2 As String) As Boolean
    Dim lTextLen As Long
    Dim lCnt As Long
    Dim sTmpVal As String
    If sText1 = "" Or sText2 = "" Then
        GoTo notfound
    End If
    If Len(sText1) > Len(sText2) Then
        GoTo notfound
    End If
    lTextLen = Len(sText2) - Len(sText1) + 1
    For lCnt = 1 To lTextLen
        sTmpVal = Mid(sText2, lCnt, Len(sText1))
        If UCase(sText1) = UCase(sTmpVal) Then
            GoTo found
        End If
    Next lCnt
    
    IsInString = False
    Exit Function
notfound:
    IsInString = False
    Exit Function
    
found:
    IsInString = True
    Exit Function
End Function

Public Function GetEnclosedString(ByVal sText As String, ByVal iBegin As Integer, ByVal sDelimiter1 As String, ByVal sDelimiter2 As String) As String
    If Len(sText) = 0 Then Exit Function
    If iBegin > Len(sText) Then Exit Function
    If sDelimiter1 = "" Or sDelimiter2 = "" Then Exit Function
    
    Dim iCnt As Integer
    Dim sTmpChar As String
    Dim bRecevoir As Boolean
    Dim bTerminer As Boolean
    Dim sRecu As String
    
    sRecu = ""
    bRecevoir = False
    bTerminer = True
    For iCnt = iBegin To Len(sText)
        sTmpChar = Mid(sText, iCnt, 1)
        If sTmpChar = sDelimiter2 Then
            bTerminer = True
            If Len(sRecu) > 0 Then
                sRecu = Right(sRecu, Len(sRecu) - 1)
            End If
            GetEnclosedString = sRecu
            Exit Function
            Exit For
        End If
        If sTmpChar = sDelimiter1 Then
            bRecevoir = True
        End If
        If bRecevoir Then
            sRecu = sRecu & sTmpChar
        End If
    Next iCnt
End Function

Public Function GetStringUntilChar(ByVal sText As String, ByVal sDelimiter As String) As String
    If sText = "" Then Exit Function
    
    Dim iCnt As Integer
    Dim sTmpChar As String
    Dim sRecu As String
    
    sRecu = ""
    For iCnt = 1 To Len(sText)
        sTmpChar = Mid(sText, iCnt, 1)
        If sTmpChar = sDelimiter Then
            Exit For
        End If
        sRecu = sRecu & sTmpChar
    Next iCnt
    GetStringUntilChar = sRecu
End Function

Public Function GetHomeURL(ByVal sURL As String, Optional ByVal bGeneralize As Boolean = False) As String
    Dim iCnt As Integer
    Dim iURLLen As Integer
    Dim sTmpVal As String
    Dim sTmpHomeURL As String
    
    iURLLen = Len(sURL)
    Select Case iURLLen
        Case 0, Is < 8
            GetHomeURL = sURL
            Exit Function
        
        Case Else
            sTmpVal = Left(sURL, 7)
            If UCase(sTmpVal) = UCase("http://") Then
                sTmpVal = Mid(sURL, 8, Len(sURL) - 7)
                sTmpVal = GetStringUntilChar(sTmpVal, "/")
                sTmpHomeURL = sTmpVal
            Else
                sTmpVal = GetStringUntilChar(sURL, "/")
                sTmpHomeURL = sTmpVal
            End If
            If bGeneralize = True Then
                GetHomeURL = sTmpHomeURL
                Exit Function
            Else
                sTmpVal = StrReverse(sTmpHomeURL)
                sTmpVal = GetStringUntilChar(sTmpHomeURL, ".")
                sTmpVal = Mid(sTmpHomeURL, Len(sTmpVal) + 2, Len(sTmpHomeURL) - Len(sTmpVal))
                sTmpVal = GetStringUntilChar(sTmpVal, ".")
                GetHomeURL = sTmpVal
                Exit Function
            End If
    End Select
End Function
