Virtual INI file

Everything else that doesn't fall into one of the other PB categories.
AZJIO
Addict
Addict
Posts: 1319
Joined: Sun May 14, 2017 1:48 am

Virtual INI file

Post by AZJIO »

When reading and writing * .Desktop files (Linux), I have problems: the OpenPreferences feature when saving adds a BOM. As a solution to the problem, you can open the file and rewrite it again without a BOM.

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"))
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Virtual INI file

Post by Caronte3D »

Usefull!
Thanks! :wink:
AZJIO
Addict
Addict
Posts: 1319
Joined: Sun May 14, 2017 1:48 am

Re: Virtual INI file

Post by AZJIO »

standard functions

Code: Select all

EnableExplicit

Structure Group
	Map KeyVal.s()
EndStructure

NewMap virtini.Group()

Procedure Read_INI(Map virtini.Group(), Path$)
	If OpenPreferences(Path$)
		ExaminePreferenceGroups()
		While NextPreferenceGroup()
			AddMapElement(virtini(), PreferenceGroupName(), #PB_Map_NoElementCheck)
			ExaminePreferenceKeys()
			While  NextPreferenceKey()
				If AddMapElement(virtini()\KeyVal(), PreferenceKeyName(), #PB_Map_NoElementCheck)
					virtini()\KeyVal() = PreferenceKeyValue()
				EndIf
			Wend
		Wend
		ClosePreferences()
	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"))
Post Reply