I wanted to create a virtual ini file
Code: Select all
EnableExplicit
Structure Group
Map KeyVal.s()
EndStructure
NewMap virtini.Group()
Procedure Read_INI(Map virtini.Group(), Path$)
Protected tmp, tmp$, StrN$
Protected id, Pos
Protected NotGrOpen = 1
If FileSize(Path$) > 4
id = ReadFile(#PB_Any, Path$)
If id
While Eof(id) = 0
StrN$ = ReadString(id)
StrN$ = RTrim(StrN$, " ")
Repeat ; Cycle to remove space characters at the beginning of the line
tmp = Asc(StrN$)
Select tmp
Case 32, 9, 160
StrN$ = LTrim(StrN$, Chr(tmp))
Default
Break
EndSelect
ForEver
If Asc(StrN$) = 59 ; If the comment then read the following line
Continue
EndIf
If Asc(StrN$) = 91 ; If a group opens, then read her name
Pos = FindString(StrN$, "]", 3, #PB_String_CaseSensitive)
If Pos And AddMapElement(virtini(), Mid(StrN$, 2, Pos - 2), #PB_Map_NoElementCheck)
NotGrOpen = 0
EndIf
Continue
EndIf
If NotGrOpen ; If there is no open group, you skip the current line until it is open, because Parameters and values can only be in the group
Continue
EndIf
Pos = FindString(StrN$, "=", 2, #PB_String_CaseSensitive)
If Pos
tmp$ = Left(StrN$, Pos - 1)
tmp$ = RTrim(tmp$, " ") ; Remove the spaces around "="
If AddMapElement(virtini()\KeyVal(), tmp$, #PB_Map_NoElementCheck)
tmp$ = Mid(StrN$, Pos + 1)
tmp$ = LTrim(tmp$, " ") ; Remove the spaces around "="
virtini()\KeyVal() = tmp$
EndIf
ElseIf Asc(StrN$)
AddMapElement(virtini()\KeyVal(), StrN$, #PB_Map_NoElementCheck)
EndIf
Wend
CloseFile(id)
EndIf
EndIf
EndProcedure
Read_INI(virtini(), "C:\shutter.desktop")
ForEach virtini()
Debug #CRLF$ + "[" + MapKey(virtini()) + "]"
ForEach virtini()\KeyVal()
Debug MapKey(virtini()\KeyVal()) + " = " + virtini()\KeyVal()
Next
Next
; Access to the value by name section + key
MessageRequester("", virtini("Desktop Action Screen")\KeyVal("Exec"))