Jump to content

Get Ini Sections And Section Entries


bcording
 Share

Recommended Posts

The first function will fill the passed array with all the section names of the given INI file. The second function will fill the passed array with all the entries of the given section of the given INI file. Both set @Error to 1 if they succeed.

Func GetSections(Byref $DataArray, $INIFile)
    Local $File, $Line
    Local $Sections

    $File = FileOpen($INIFile,0)
    If $File = -1 Then Return

    While 1
        $Line = FileReadLine($File)
        If @error = -1 Then ExitLoop
        $Line = StringStripWS($Line,3)
        If StringLeft($Line,1) = "[" And StringRight($Line,1) = "]" then
            $Line = StringMid($Line,2,StringLen($Line)-2)
            $Sections = $Sections & $Line & @LF
        EndIF
    Wend

    FileClose($File)
    If StringLen($Sections) = 0 Then Return
    $DataArray = StringSplit(StringTrimRight($Sections,1),@LF)
    SetError(1)
EndFunc

Func GetSectionEntries(Byref $DataArray, $Section, $INIFile)
    Local $File, $Line
    Local $Entries

    $File = FileOpen($INIFile,0)
    If $File = -1 Then Return

    While 1
        $Line = FileReadLine($File)
        If @error = -1 Then ExitLoop
        $Line = StringStripWS($Line,3)
        If $Line = "[" & $Section & "]" then
            While 1
                $Line = FileReadLine($File)
                If @error = -1 Then ExitLoop
                $Line = StringStripWS($Line,3)
                If StringLeft($Line,1) = "[" then ExitLoop(2)
                If StringLeft($Line,1) <> ";" And StringLen($Line) <> 0 then
                    $Entries = $Entries & $Line & @LF
                EndIF
            Wend
        EndIF
    Wend

    FileClose($File)
    If StringLen($Entries) = 0 Then Return
    $DataArray = StringSplit(StringTrimRight($Entries,1),@LF)
    SetError(1)
EndFunc
Link to comment
Share on other sites

  • 3 weeks later...

Shouldn't they set @Error to 1 if they fail?

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...