Attribute VB_Name = "ExtendedParser"
Option Explicit

' Coded by CyanPhase

' allows for extracting a value by key from a string using
' the notation "key=value,key=value,key=value,..."
'
Public Function GetKeyValueEntry(ByVal FullString As String, ByVal EntryKey As String) As String
    Dim side1 As Integer
    Dim side2 As Integer
    Dim Buffer As String
    
    side1 = 1
    side2 = InStr(1, FullString, ",", vbBinaryCompare) - 1
    Do While ((side2 >= 1 And side1 >= 1) And (side1 < side2))
        Buffer = Trim$(Mid$(FullString, side1, side2 - side1 + 1))
        If (UCase$(Left$(Buffer, Len(EntryKey))) = UCase$(EntryKey)) Then
            GetKeyValueEntry = Trim$(Right$(Buffer, Len(Buffer) - InStr(1, Buffer, "=", vbBinaryCompare)))
            Exit Function
        End If
        side1 = side2 + 2
        side2 = InStr(side1, FullString, ",", vbBinaryCompare) - 1
        If (side2 < 1) Then
            If Not (side1 = Len(FullString)) Then
                side2 = Len(FullString)
            End If
        End If
    Loop
    GetKeyValueEntry = ""
End Function

' allows for extracting a value by key from a string using
' the notation "key=value,key=value,key=value,...", Integer Version
'
Public Function GetKeyValueEntryInt(ByVal FullString As String, ByVal EntryKey As String) As Integer
    Dim side1 As Integer
    Dim side2 As Integer
    Dim Buffer As String
    
    side1 = 1
    side2 = InStr(1, FullString, ",", vbBinaryCompare) - 1
    Do While (side2 >= 1 And side1 >= 1) And (side1 < side2)
        Buffer = Trim$(Mid$(FullString, side1, side2 - side1 + 1))
        If (UCase$(Left$(Buffer, Len(EntryKey))) = UCase$(EntryKey)) Then
            GetKeyValueEntryInt = Val(Trim$(Right$(Buffer, Len(Buffer) - InStr(1, Buffer, "=", vbBinaryCompare))))
            Exit Function
        End If
        side1 = side2 + 2
        side2 = InStr(side1, FullString, ",", vbBinaryCompare) - 1
        If (side2 < 1) Then
            If Not (side1 = Len(FullString)) Then
                side2 = Len(FullString)
            End If
        End If
    Loop
    GetKeyValueEntryInt = -1
End Function

