Jump to content

My way of 'IniReadSection' Function


Sigi2
 Share

Recommended Posts

I'm sad about the way my bug report v3 beta - possible bug in IniReadSection is treated.

So i scripted my own Function that runs as expected and with added functionality.

SYNTAX:
_IniReadSection ( "filename", "section"[, option] )

PARAMETERS:
"filename" and "section" are like the built in ones
the new parameter "option" is optional:
  0 = read only values in proper format (keyname = value)
      bad formatted lines (without the '=') are ignored (default)
  1 = read all values
      bad formatted lines are treated as keynames without a value
  2 = read all values
      bad formatted lines are treated as values without a keyname
  3 = read all values
      bad formatted lines are treated as values with a numbered keyname

and here is my version:

Func _IniReadSection($filename, $section, $option = 0)
    local $file, $error, $line, $data, $sectionfound, $returndata
    local $l, $p, $i, $counter, $counter2
    if $filename = "" or $section = "" or $option > 3 or $option < 0 Then
        $error = 1
    Else
        $file = FileOpen($filename, 0)
        If $file = -1 Then $error = 1
    EndIf
    If $error Then
        SetError(1)
        Return
    Else
        While 1
            $line = FileReadLine($file)
            If @error = -1 Then  ExitLoop
            $line = StringStripWS($line, 3)
            If StringLen($line) > 0 Then
                If StringLeft($line, 1) = "[" Then
                    if $sectionfound Then ExitLoop
                    if StringInStr($line, "[" & $section & "]") Then $sectionfound = 1
                ElseIf $sectionfound then
                    if StringInStr($line, "=") or $option Then
                        $data = $data & $line & chr(1)
                        $counter = $counter + 1
                    EndIf
                EndIf
            EndIf
        Wend
    EndIf
    FileClose($file)
    dim $returndata[$counter + 1][2]
    $returndata[0][0] = $counter
    for $i = 1 to $counter
        $p = StringInStr($data, chr(1), 0, $i)
        $line = StringMid($data, $l +1, $p - $l -1)
        $l = $p
        $p = StringInStr($line, "=")
        if $p > 1 Then
            $returndata[$i][0] = StringStripWS(StringLeft($line, $p -1), 3)
            $returndata[$i][1] = StringStripWS(StringTrimLeft($line, $p), 3)
        Else
            if $option = 1 Then
                $returndata[$i][0] = StringStripWS($line, 3)
                $returndata[$i][1] = ""
            ElseIf $option = 2 Then
                $returndata[$i][0] = ""
                $returndata[$i][1] = StringStripWS($line, 3)
            Else
                while 1
                    $counter2 = $counter2 + 1
                    if IniRead($filename, $section, $counter2, "") = "" then ExitLoop
                WEnd
                $returndata[$i][0] = $counter2
                $returndata[$i][1] = StringStripWS($line, 3)
            EndIf
        EndIf
    Next
    Return $returndata
EndFunc

as an side-effect it enables you to read ini-like files as this one:

[section1]
line 1
line 2
line 3
[section2]
line one
...

for an example script on how to use, please take a look in the latest beta v3.1 documentation and replace "IniReadSection(..." with "_IniReadSection(..."

any suggestions ?

Edited by Sigi2
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...