### Ini Files Read and Write

This example enables to read or change Ini Files with format:
[Section]
Key=Value
...
The B4J code uses the standard Map structure Section.Key=Value to handle the Ini entries.
Two Subs are defined:
Sub IniToMap(dir As String, filename As String) As Map
Read an inifile into a map with key=value of section.key=value
Sub MapToIni(dir As String, filename As String, m As Map)
Create an inifile from a map with key=value of section.key=value
IMPORTANT NOTE:
The subs have been tested with the Ini file format as outlined below in the test.
Other eventualities not considered, like handling special chars in the value.
B4J Code Snippet
`Sub Process_Globals
Private inifile As String = "test.ini" ' located in the dirapp folder
End Sub`
`Sub Test
' Define the map to be used
Dim inimap As Map = IniToMap(File.dirapp, inifile)
' Read entries
Log("Entries")
Log($"General.Changed = ${inimap.Get("General.Changed")}"$)
Log($"Weather.Observered = ${inimap.Get("Weather.Observed")}"$)
Log($"Weather.Temperature = ${inimap.Get("Weather.Temperature")}"$)
Log($"Weather.Humidity = ${inimap.Get("Weather.Humidity")}"$)
Log($"Weather.Pressure = ${inimap.Get("Weather.Pressure")}"$)
' Update an entry
inimap.Put("General.Changed", DateTime.Time(DateTime.Now))
Log("Entry updated")
Log($"General.Changed = ${inimap.Get("General.Changed")}"$)
' Write new entry in a new section inimap.put(section.key, value)
inimap.Put("Information.Reference", "mysite")
Log("New Entry")
Log($"Information.Reference = ${inimap.Get("Information.Reference")}"$)
' Save the map to the inifile
MapToIni(File.dirapp,inifile, inimap)
End Sub`
' Read an inifile into a map with key=value of section.key=value
' If section.key not found, null is returned.
' The map can be used to change values, add new keys, delete key using Map methods.
' Example:
' Dim m As Map = IniToMap(File.dirapp, inifile)
' Log($"Dewpoint.LTime = ${m.Get("Dewpoint.LTime")}"$)
`Sub IniToMap(dir As String, filename As String) As Map
Dim section As String
Dim l As List
Dim m As Map
m.Initialize
l.Initialize
l = File.ReadList(dir, filename)
For Each inientry As String In l
If inientry.StartsWith("[") And inientry.EndsWith("]") Then section = inientry.Replace("[","").Replace("]","")
Dim kv() As String = Regex.Split("=", inientry)
If kv.Length > 0 And Not(kv(0).EqualsIgnoreCase($"[${section}]"$)) Then
If kv(0).Length > 0 Then
If kv.Length = 1 Then m.Put($"${section}.${kv(0)}"$,"")
If kv.Length = 2 Then m.Put($"${section}.${kv(0)}"$,kv(1))
End If
End If
Next
Return m
End Sub`
' Create an inifile from a map with key=value of section.key=value
`Sub MapToIni(dir As String, filename As String, m As Map)
Dim section As String
Dim newsection As String = ""
Dim l As List
l.Initialize
Dim sb As StringBuilder
sb.Initialize
For i = 0 To m.Size - 1
l.Add($"${m.GetKeyAt(i)}=${m.GetValueAt(i)}"$)
Next
l.Sort(True)
For Each s As String In l
Dim mi() As String = Regex.Split("\.", s)
If mi.length > 0 Then section = mi(0)
If Not(section.EqualsIgnoreCase(newsection)) Then
sb.append($"[${section}]"$).Append(CRLF)
newsection = section
End If
sb.Append(s.Replace(section&".", "")).append(CRLF)
Next
File.writestring(dir, filename, sb.ToString)
End Sub`
Output Tests
Ini file prior changes
[General]
Changed=
[Weather]
Observed=12:22
Temperature=12
Humidity=75
Pressure=1012
Ini file after changes
[General]
Changed=15:25:32
[Information]
Reference=www.mysite
[Weather]
Humidity=75
Observed=12:22
Pressure=1012
Temperature=12
B4J IDE Logs
Entries
General.Changed =
Weather.Observered = 12:22
Weather.Temperature = 12
Weather.Humidity = 75
Weather.Pressure = 1012
Entry updated
General.Changed = 15:25:32
New Entry
Information.Reference = www.mysite

Changed: 2018-06-07 09:15, Category: Ini Files, File: T6C200M1469.htm, ID: 1469