How to Parse an .Ini File

Discussion in 'Questions (Windows Mobile)' started by plinydogg, Jul 24, 2007.

  1. plinydogg

    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!
  2. dzt

    dzt Active Member Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

    Attached is a simple example that finds the right section in the file and parses it.
    [SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2]Globals[/SIZE]
    2] iniFile = "ini.ini"
    2][COLOR=#0000ff] Dim [/COLOR][/SIZE][SIZE=2]iniData[/SIZE][SIZE=2][COLOR=#0000ff]
    End Sub
    2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2]App_Start
    2][COLOR=#0000ff] If FileExist[/COLOR][/SIZE][SIZE=2](iniFile) [/SIZE][SIZE=2][COLOR=#0000ff]Then
    2][COLOR=#0000ff]  FileOpen[/COLOR][/SIZE][SIZE=2](c1,iniFile,cRead)
      iniData = [/SIZE][SIZE=
    2][COLOR=#0000ff]  FileClose[/COLOR][/SIZE][SIZE=2](c1)
    2][COLOR=#0000ff] else
    2][COLOR=#008000]  'create the ini file with the default values
    2][COLOR=#0000ff] end if
    2] LoadCompleteSection("listbox items:","ListBox1")
    2][COLOR=#0000ff]End Sub
    '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] if [/COLOR][/SIZE][SIZE=2]endS = 0 [/SIZE][SIZE=2][COLOR=#0000ff]then return[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'section was not found
    2] i1 = [/SIZE][SIZE=2][COLOR=#0000ff]StrIndexOf[/COLOR][/SIZE][SIZE=2](sectionData,crlf,0)
    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
    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
    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] Loop
    End Sub
    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
    2][COLOR=#0000ff] if [/COLOR][/SIZE][SIZE=2]i1 = -1 [/SIZE][SIZE=2][COLOR=#0000ff]then return
    2] i2 = [/SIZE][SIZE=2][COLOR=#0000ff]StrIndexOf[/COLOR][/SIZE][SIZE=2](iniData,crlf & "[",i1+1) [/SIZE][SIZE=2][COLOR=#008000]'end of section
    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
    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.
    2][COLOR=#0000ff]End Sub

    Attached Files:

  4. plinydogg

    plinydogg New Member

    Thanks to you both. I'll take a look at this when I get home from work :)
  5. plinydogg

    plinydogg New Member

    I tried Erel's method and it worked like a charm. THANK YOU! :)
  6. maXim

    maXim Active Member Licensed User

  7. BjornF

    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
  8. maXim

    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.


    P.S. I apologize for my bad English :sign0013:
  9. giannimaione

    giannimaione Well-Known Member Licensed User

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice