'Class module
Sub Class_Globals
Type ICSObject (TypeName As String, Attributes As Map, Children As List)
Private LineIndex As Int
Private Lines As List
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
End Sub
Public Sub Parse(Dir As String, FileName As String) As ICSObject
Lines = File.ReadList(Dir, FileName)
LineIndex = 0
Dim kv() As String = ReadNextLine
If kv(0) <> "BEGIN" Then
LogError("Invalid file")
Return Null
End If
Dim root As ICSObject = CreateICS(kv(1))
ParseICS(root)
Return root
End Sub
Private Sub ReadNextLine As String()
Dim first As String = Lines.Get(LineIndex)
LineIndex = LineIndex + 1
Do While LineIndex < Lines.Size - 1
Dim NextLine As String = Lines.Get(LineIndex)
If NextLine.StartsWith(" ") Then
LineIndex = LineIndex + 1
first = first & NextLine
Else
Exit
End If
Loop
first = first.Replace("\n", CRLF)
Dim i As String = first.IndexOf(":")
Return Array As String(first.SubString2(0, i), first.SubString(i + 1))
End Sub
Private Sub ParseICS (parent As ICSObject) As ICSObject
Do While True
Dim kv() As String = ReadNextLine
If kv(0) = "BEGIN" Then
parent.Children.Add(ParseICS(CreateICS(kv(1))))
Else If kv(0) = "END" Then
Exit
Else
parent.Attributes.Put(kv(0), kv(1))
End If
Loop
Return parent
End Sub
Private Sub CreateICS (TypeName As String) As ICSObject
Dim ic As ICSObject
ic.Initialize
ic.TypeName = TypeName
ic.Children.Initialize
ic.Attributes.Initialize
Return ic
End Sub
Public Sub PrintTree(Parent As ICSObject, Indent As String)
Log($"${Indent}******* ${Parent.TypeName} *******"$)
For Each key As String In Parent.Attributes.Keys
Log($"${Indent}${key}: ${Parent.Attributes.Get(key)}"$)
Next
For Each ics As ICSObject In Parent.Children
PrintTree(ics, Indent & TAB)
Next
End Sub