How to Parse an .Ini File

plinydogg

New Member
Hi everyone,

This a repeat post. My earlier post was accidentally deleted when the forums went offline a day or so ago.

My question is this: does anyone know a way to parse an .ini file (i.e., a text file)? Currently, the only section the file would have would be one containing items to populate a list box. The idea is that the items that populate the listbox should be changeable by editing an .ini file rather than having them hardcoded into the program itself. I will probably add similar info in other sections later. In other words, the contents of the .ini file will probably look something like this:

[listbox items:]
item 1 = "stuff"
item 2 = "things"
item 3 = "etc."

[someother type of config info:]
item 1 = ""
item 2 = ""

How can I locate the proper section? i.e., how can I parse the .ini file?

Thanks in advance!
 

Erel

Administrator
Staff member
Licensed User
Attached is a simple example that finds the right section in the file and parses it.
B4X:
[SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2]Globals[/SIZE]
[SIZE=2] iniFile = "ini.ini"
[/SIZE][SIZE=2][COLOR=#0000ff] Dim [/COLOR][/SIZE][SIZE=2]iniData[/SIZE][SIZE=2][COLOR=#0000ff]
End Sub
 
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2]App_Start
 Form1.Show
[/SIZE][SIZE=2][COLOR=#0000ff] If FileExist[/COLOR][/SIZE][SIZE=2](iniFile) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  FileOpen[/COLOR][/SIZE][SIZE=2](c1,iniFile,cRead)
  iniData = [/SIZE][SIZE=2][COLOR=#0000ff]FileReadToEnd[/COLOR][/SIZE][SIZE=2](c1)
[/SIZE][SIZE=2][COLOR=#0000ff]  FileClose[/COLOR][/SIZE][SIZE=2](c1)
[/SIZE][SIZE=2][COLOR=#0000ff] else
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]  'create the ini file with the default values
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] end if
[/COLOR][/SIZE][SIZE=2] LoadCompleteSection("listbox items:","ListBox1")
[/SIZE][SIZE=2][COLOR=#0000ff]End Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#008000] 
'List can be an ArrayList, ComboBox or ListBox
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2]LoadCompleteSection(Section,List)
 sectionData = GetSection(Section)
 endS = [/SIZE][SIZE=2][COLOR=#0000ff]strLength[/COLOR][/SIZE][SIZE=2](sectionData)
[/SIZE][SIZE=2][COLOR=#0000ff] if [/COLOR][/SIZE][SIZE=2]endS = 0 [/SIZE][SIZE=2][COLOR=#0000ff]then return[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'section was not found
[/COLOR][/SIZE][SIZE=2] i1 = [/SIZE][SIZE=2][COLOR=#0000ff]StrIndexOf[/COLOR][/SIZE][SIZE=2](sectionData,crlf,0)
[/SIZE][SIZE=2][COLOR=#0000ff] Do While [/COLOR][/SIZE][SIZE=2]i1 < endS [/SIZE][SIZE=2][COLOR=#0000ff]AND [/COLOR][/SIZE][SIZE=2]i1 > -1
  i3 = [/SIZE][SIZE=2][COLOR=#0000ff]StrIndexOf[/COLOR][/SIZE][SIZE=2](sectionData,[/SIZE][SIZE=2][COLOR=#0000ff]chr[/COLOR][/SIZE][SIZE=2](34),i1) [/SIZE][SIZE=2][COLOR=#008000]'first quote
[/COLOR][/SIZE][SIZE=2]  i4 = [/SIZE][SIZE=2][COLOR=#0000ff]StrIndexOf[/COLOR][/SIZE][SIZE=2](sectionData,[/SIZE][SIZE=2][COLOR=#0000ff]chr[/COLOR][/SIZE][SIZE=2](34),i3+1) [/SIZE][SIZE=2][COLOR=#008000]'second quote
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  if [/COLOR][/SIZE][SIZE=2]i3 > 0 [/SIZE][SIZE=2][COLOR=#0000ff]AND [/COLOR][/SIZE][SIZE=2]i4>0 [/SIZE][SIZE=2][COLOR=#0000ff]then Control[/COLOR][/SIZE][SIZE=2](List).Add([/SIZE][SIZE=2][COLOR=#0000ff]SubString[/COLOR][/SIZE][SIZE=2](sectionData,i3+1,i4-i3-1))
  i1 = [/SIZE][SIZE=2][COLOR=#0000ff]StrIndexOf[/COLOR][/SIZE][SIZE=2](sectionData,crlf,i1+1)
[/SIZE][SIZE=2][COLOR=#0000ff] Loop
End Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] 
Sub [/COLOR][/SIZE][SIZE=2]GetSection(Section)
 i1 = [/SIZE][SIZE=2][COLOR=#0000ff]StrIndexOf[/COLOR][/SIZE][SIZE=2](iniData,"[" & Section & "]",0) [/SIZE][SIZE=2][COLOR=#008000]'section start
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] if [/COLOR][/SIZE][SIZE=2]i1 = -1 [/SIZE][SIZE=2][COLOR=#0000ff]then return
[/COLOR][/SIZE][SIZE=2] i2 = [/SIZE][SIZE=2][COLOR=#0000ff]StrIndexOf[/COLOR][/SIZE][SIZE=2](iniData,crlf & "[",i1+1) [/SIZE][SIZE=2][COLOR=#008000]'end of section
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] if [/COLOR][/SIZE][SIZE=2]i2 = -1 [/SIZE][SIZE=2][COLOR=#0000ff]then [/COLOR][/SIZE][SIZE=2]i2 = [/SIZE][SIZE=2][COLOR=#0000ff]StrLength[/COLOR][/SIZE][SIZE=2](iniData) [/SIZE][SIZE=2][COLOR=#008000]'This is the last section
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] return SubString[/COLOR][/SIZE][SIZE=2](iniData,i1,i2-i1) [/SIZE][SIZE=2][COLOR=#008000]'It is better to work with the relevant section if the file is large.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End Sub
[/COLOR][/SIZE]
 

Attachments

BjornF

Active Member
Licensed User
Another alternative, which I have used, is to load a table, perhaps with one column giving e.g. the name of each of the listboxes (or whatever) and the second giving the value. Then just filter on the name and that is it.

all the best / Björn
 

maXim

Active Member
Licensed User
Hi BjornF,

Your solution is a good alternative but it doesn't result efficient when it is had to work with a lot of sections and in more parts of the program, besides also the occupation of the memory in relationship to the keys to be treated could condition the resources of the PPC.

Thanks for your contribution to the discussion.

Best regards.

maXim

P.S. I apologize for my bad English :sign0013:
 
Top