_IniRead() Eigene Funktion mit noch ein paar Fehlern

  • Hallo alle beisammen.

    Ich habe eine neue Iniread-Funktion geschrieben. Hintergrund ist die Begrenzung auf ~33.000 chars. Allerdings kommen noch einige Fehler beim Ausführen mit F5. Vor allem wegen "Return".

    Hier die Funktion mit Beispielini im Anhang:

    Spoiler anzeigen
    [autoit]

    #cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.6.0
    Author: Aquaplant
    Script Function:
    _Iniread($spath[, $sSection[, $sKey]])
    #ce ----------------------------------------------------------------------------
    #Include <String.au3>
    $AAA = _IniRead(@ScriptDir & "\Test.txt")
    If @error Then
    MsgBox(0, "", @error)
    EndIf
    For $i = 1 To $AAA[0]
    MsgBox(0, $AAA[0], $AAA[$i]
    Next

    [/autoit] [autoit][/autoit] [autoit]

    Func _IniRead($sPath, $sSection = "", $sKey = "")
    Global $sSectionname[999]
    Local $sArray = ""
    If FileExists($sPath) = 0 Then
    SetError(1, 1, "Requested file does not exist")
    Else
    If $sSection = "" And $sKey = "" Then
    $oHandle = FileOpen($sPath)
    $i = 0
    $a = 0
    While 1
    $i += 1
    $sLine = FileReadLine($oHandle, $i)
    If @error Then
    If FileGetSize($sPath) <= 1 Then
    SetError(2, 2, "No contents in the file")
    EndIf
    $sSectionname[0] = $a
    FileClose($oHandle)
    ExitLoop
    EndIf
    If StringLeft($sLine, 1) = "[" Then
    $sArray = ""
    $a += 1
    $aLine = StringSplit($sLine, "]")
    For $j = 1 To $aLine[0] - 1
    $sArray &= $aLine[$j] & "]"
    Next
    $sArray = StringTrimRight($sArray, 1)
    $sArray = StringTrimLeft($sArray, 1)
    $sSectionname[$a] = $sArray
    EndIf
    WEnd
    Return $sSectionname
    EndIf
    If $sSection <> "" And $sKey = "" Then
    Global $aKV[999][999]
    $i = 0
    $j = 0
    While 1
    $i += 1
    $sLine = FileReadLine($sPath, $i)
    If @error Then
    SetError(3, 3, "Section does not exist")
    EndIf
    If StringLeft($sLine, StringLen($sSection) + 2) = "[" & $sSection & "]" Then
    While 1
    $i += 1
    $j += 1
    $sLine = FileReadLine($sPath, $i)
    If StringLeft($sLine, 1) = "[" Or @error Then
    $aKV[0][0] = $j
    ExitLoop 2
    Else
    $sString = ""
    $aString = StringSplit($sLine, "=")
    $aKV[$j][0] = $aString[1]
    For $k = 2 To $aString[0]
    If $aString[$k] = "" Then
    $aString[$k] = "="
    EndIf
    $sString &= $aString[$k] & "="
    Next
    $sString = StringTrimRight($sString, 1)
    $aKV[$j][1] = $sString
    EndIf
    WEnd
    EndIf
    WEnd
    Return $aKV
    EndIf
    If $sSection <> "" And $sKey <> "" Then
    $sFile = FileRead($sPath)
    $aA = _StringBetween($sFile, $sKey & "=", @CRLF)
    If IsArray($aA) = 0 Then
    $aA = _StringBetween($sFile, $sKey, @CR)
    EndIf
    $sValue = $aA[0]
    Return $sValue
    EndIf
    EndIf
    EndFunc

    [/autoit]


    Fehlertext:

    Spoiler anzeigen

    C:\Users\Work\Desktop\_IniRead.au3(16,29) : ERROR: syntax error
    MsgBox(0, $AAA[0], $AAA[$i]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(20,33) : WARNING: $sSection: possibly used before declaration.
    Func _IniRead($sPath, $sSection =
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(20,45) : WARNING: $sKey: possibly used before declaration.
    Func _IniRead($sPath, $sSection = "", $sKey =
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(23,22) : WARNING: $sPath: possibly used before declaration.
    If FileExists($sPath)
    ~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(53,24) : ERROR: 'Return' not allowed from global scope.
    Return $sSectionname
    ~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(89,15) : ERROR: 'Return' not allowed from global scope.
    Return $aKV
    ~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(98,18) : ERROR: 'Return' not allowed from global scope.
    Return $sValue
    ~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(101,1) : ERROR: missing Next.
    EndFunc
    ^
    C:\Users\Work\Desktop\_IniRead.au3(15,12) : REF: missing Next.
    For $i = 1 To
    ~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(101,1) : ERROR: syntax error
    EndFunc
    ^
    C:\Users\Work\Desktop\_IniRead.au3(20,33) : ERROR: $sSection: undeclared global variable.
    Func _IniRead($sPath, $sSection =
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(11,41) : ERROR: _IniRead(): undefined function.
    $AAA = _IniRead(@ScriptDir & "\Test.txt")
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3 - 8 error(s), 3 warning(s)

  • also bei mir hat er zwar keine fehler mehr aber funzen tut da nix (gibt nichts aus)

    Spoiler anzeigen
    [autoit]

    #cs ----------------------------------------------------------------------------
    #cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.6.0
    Author: Aquaplant
    Script Function:
    _Iniread($spath[, $sSection[, $sKey]])
    #ce ----------------------------------------------------------------------------
    #Include <String.au3>
    $AAA = _IniRead(@ScriptDir & "\Test.txt")
    If @error Then
    MsgBox(0, "", @error)
    EndIf
    For $i = 1 To $AAA[0]
    MsgBox(0, $AAA[0], $AAA[$i])
    Next
    Func _IniRead($sPath, $sSection = "", $sKey = "")
    Global $sSectionname[999]
    Local $sArray = ""
    If FileExists($sPath) = 0 Then
    SetError(1, 1, "Requested file does not exist")
    Else
    If $sSection = "" And $sKey = "" Then
    $oHandle = FileOpen($sPath,0)
    $i = 0
    $a = 0
    While 1
    $i += 1
    $sLine = FileReadLine($oHandle, $i)
    If @error Then
    If FileGetSize($sPath) <= 1 Then
    SetError(2, 2, "No contents in the file")
    EndIf
    $sSectionname[0] = $a
    FileClose($oHandle)
    ExitLoop
    EndIf
    If StringLeft($sLine, 1) = "[" Then
    $sArray = ""
    $a += 1
    $aLine = StringSplit($sLine, "]")
    For $j = 1 To $aLine[0] - 1
    $sArray &= $aLine[$j] & "]"
    Next
    $sArray = StringTrimRight($sArray, 1)
    $sArray = StringTrimLeft($sArray, 1)
    $sSectionname[$a] = $sArray
    EndIf
    WEnd
    Return $sSectionname
    EndIf
    If $sSection <> "" And $sKey = "" Then
    Global $aKV[999][999]
    $i = 0
    $j = 0
    While 1
    $i += 1
    $sLine = FileReadLine($sPath, $i)
    If @error Then
    SetError(3, 3, "Section does not exist")
    EndIf
    If StringLeft($sLine, StringLen($sSection) + 2) = "[" & $sSection & "]" Then
    While 1
    $i += 1
    $j += 1
    $sLine = FileReadLine($sPath, $i)
    If StringLeft($sLine, 1) = "[" Or @error Then
    $aKV[0][0] = $j
    ExitLoop 2
    Else
    $sString = ""
    $aString = StringSplit($sLine, "=")
    $aKV[$j][0] = $aString[1]
    For $k = 2 To $aString[0]
    If $aString[$k] = "" Then
    $aString[$k] = "="
    EndIf
    $sString &= $aString[$k] & "="
    Next
    $sString = StringTrimRight($sString, 1)
    $aKV[$j][1] = $sString
    EndIf
    WEnd
    EndIf
    WEnd
    Return $aKV
    EndIf
    If $sSection <> "" And $sKey <> "" Then
    $sFile = FileRead($sPath)
    $aA = _StringBetween($sFile, $sKey & "=", @CRLF)
    If IsArray($aA) = 0 Then
    $aA = _StringBetween($sFile, $sKey, @CR)
    EndIf
    $sValue = $aA[0]
    Return $sValue
    EndIf
    EndIf
    EndFunc

    [/autoit]

    UPDATED

    Einmal editiert, zuletzt von Alizame (27. April 2010 um 16:23)

  • mal abgesehn von den fehlern:

    Spoiler anzeigen

    C:\Users\Work\Desktop\_IniRead.au3(16,29) : ERROR: syntax error
    MsgBox(0, $AAA[0], $AAA[$i]

    fehlende klammer: )


    C:\Users\Work\Desktop\_IniRead.au3(20,33) : WARNING: $sSection: possibly used before declaration.
    Func _IniRead($sPath, $sSection =
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(20,45) : WARNING: $sKey: possibly used before declaration.
    Func _IniRead($sPath, $sSection = "", $sKey =
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(23,22) : WARNING: $sPath: possibly used before declaration.
    If FileExists($sPath)
    ~~~~~~~~~~~~~~~~~~~~^

    sollten kein problem sein


    C:\Users\Work\Desktop\_IniRead.au3(53,24) : ERROR: 'Return' not allowed from global scope.
    Return $sSectionname
    ~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(89,15) : ERROR: 'Return' not allowed from global scope.
    Return $aKV
    ~~~~~~~~~~~^
    C:\Users\Work\Desktop\_IniRead.au3(98,18) : ERROR: 'Return' not allowed from global scope.
    Return $sValue

    Return werte musst du als local in der funktion definieren und nicht als global

    worin genau liegt der sinn der funktion?

    das mit begrenzung versteh ich nicht ganz wie dus meinst

  • Hallo Aquaplant,

    der eigentliche Fehler liegt in Zeile 16. Hier das geänderte Skript:

    Spoiler anzeigen
    [autoit]

    #cs ----------------------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit]

    AutoIt Version: 3.3.6.0
    Author: Aquaplant

    [/autoit] [autoit][/autoit] [autoit]

    Script Function:
    _Iniread($spath[, $sSection[, $sKey]])

    [/autoit] [autoit][/autoit] [autoit]

    #ce ----------------------------------------------------------------------------
    #Include <String.au3>
    $AAA = _IniRead(@ScriptDir & "\Test.txt")
    If @error Then
    MsgBox(0, "", @error)
    EndIf
    For $i = 1 To $AAA[0]
    MsgBox(0, $AAA[0], $AAA[$i])
    Next

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _IniRead($sPath, $sSection = "", $sKey = "")
    Global $sSectionname[999]
    Local $sArray = ""
    If FileExists($sPath) = 0 Then
    SetError(1, 1, "Requested file does not exist")
    Else
    If $sSection = "" And $sKey = "" Then
    $oHandle = FileOpen($sPath)
    $i = 0
    $a = 0
    While 1
    $i += 1
    $sLine = FileReadLine($oHandle, $i)
    If @error Then
    If FileGetSize($sPath) <= 1 Then
    SetError(2, 2, "No contents in the file")
    EndIf
    $sSectionname[0] = $a
    FileClose($oHandle)
    ExitLoop
    EndIf
    If StringLeft($sLine, 1) = "[" Then
    $sArray = ""
    $a += 1
    $aLine = StringSplit($sLine, "]")
    For $j = 1 To $aLine[0] - 1
    $sArray &= $aLine[$j] & "]"
    Next
    $sArray = StringTrimRight($sArray, 1)
    $sArray = StringTrimLeft($sArray, 1)
    $sSectionname[$a] = $sArray
    EndIf
    WEnd
    Return $sSectionname
    EndIf
    If $sSection <> "" And $sKey = "" Then
    Global $aKV[999][999]
    $i = 0
    $j = 0
    While 1
    $i += 1
    $sLine = FileReadLine($sPath, $i)
    If @error Then
    SetError(3, 3, "Section does not exist")
    EndIf
    If StringLeft($sLine, StringLen($sSection) + 2) = "[" & $sSection & "]" Then
    While 1
    $i += 1
    $j += 1
    $sLine = FileReadLine($sPath, $i)
    If StringLeft($sLine, 1) = "[" Or @error Then
    $aKV[0][0] = $j
    ExitLoop 2
    Else
    $sString = ""
    $aString = StringSplit($sLine, "=")
    $aKV[$j][0] = $aString[1]
    For $k = 2 To $aString[0]
    If $aString[$k] = "" Then
    $aString[$k] = "="
    EndIf
    $sString &= $aString[$k] & "="
    Next
    $sString = StringTrimRight($sString, 1)
    $aKV[$j][1] = $sString
    EndIf
    WEnd
    EndIf
    WEnd
    Return $aKV
    EndIf
    If $sSection <> "" And $sKey <> "" Then
    $sFile = FileRead($sPath)
    $aA = _StringBetween($sFile, $sKey & "=", @CRLF)
    If IsArray($aA) = 0 Then
    $aA = _StringBetween($sFile, $sKey, @CR)
    EndIf
    $sValue = $aA[0]
    Return $sValue
    EndIf
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit]

    mfg (Auto)Bert

  • Erstmal vielen Dank,

    An der Msgbox lag der Fehler ^^

    Jetzt funktioniert es auch.


    Noch mal zu dem Nutzen:

    Wenn man die Standardfunktion verwendet kann man maximal 32.000 irgendwas Buchstaben aus der Ini auslesen. Ich brauche aber sehr viele mehr, deshalb muss ich sie "manuell" auslesen.

    Das Problem bleibt aber weiterhin, dass ein Array "nur" 999 (1000) "Plätze" hat.


    //Edit:

    Hier noch einmal meine korrigierte Version, 2 winzige Fehler gab es noch:

    Spoiler anzeigen
    [autoit]

    #cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.6.0
    Author: Aquaplant
    Script Function:
    _Iniread($spath[, $sSection[, $sKey[, $sDefault]]])
    Output:
    _Iniread($sPath) correspond to Inireadsectionames()
    _Iniread($sPath, $sSection) correspond to Inireadsection()
    _Iniread($sPath, $sSection, $sKey[, $sDefault]) correspond to Iniread()
    #ce ----------------------------------------------------------------------------
    #include <String.au3>
    Func _IniRead($sPath, $sSection = "", $sKey = "", $sDefault = "")
    Dim $sSectionname[999]
    Local $sArray = ""
    If FileExists($sPath) = 0 Then
    SetError(1, 1, "Requested file does not exist")
    Else
    If $sSection = "" And $sKey = "" Then
    $oHandle = FileOpen($sPath)
    $i = 0
    $a = 0
    While 1
    $i += 1
    $sLine = FileReadLine($oHandle, $i)
    If @error Then
    If FileGetSize($sPath) <= 1 Then
    SetError(2, 2, "No contents in the file")
    EndIf
    $sSectionname[0] = $a
    FileClose($oHandle)
    ExitLoop
    EndIf
    If StringLeft($sLine, 1) = "[" Then
    $sArray = ""
    $a += 1
    $aLine = StringSplit($sLine, "]")
    For $j = 1 To $aLine[0] - 1
    $sArray &= $aLine[$j] & "]"
    Next
    $sArray = StringTrimRight($sArray, 1)
    $sArray = StringTrimLeft($sArray, 1)
    $sSectionname[$a] = $sArray
    EndIf
    WEnd
    Return $sSectionname
    EndIf
    If $sSection <> "" And $sKey = "" Then
    Dim $aKV[999][999]
    $i = 0
    $j = 0
    While 1
    $i += 1
    $sLine = FileReadLine($sPath, $i)
    If @error Then
    SetError(3, 3, "Section does not exist")
    EndIf
    If StringLeft($sLine, StringLen($sSection) + 2) = "[" & $sSection & "]" Then
    While 1
    $i += 1
    $j += 1
    $sLine = FileReadLine($sPath, $i)
    If StringLeft($sLine, 1) = "[" Or @error Or $sLine = "" Then
    $aKV[0][0] = $j - 1
    ExitLoop 2
    Else
    $sString = ""
    $aString = StringSplit($sLine, "=")
    $aKV[$j][0] = $aString[1]
    For $k = 2 To $aString[0]
    If $aString[$k] = "" Then
    $aString[$k] = "="
    $aString[$k] = StringTrimLeft($aString[$k], 1)
    EndIf
    $sString &= $aString[$k] & "="
    Next
    $sString = StringTrimRight($sString, 1)
    $aKV[$j][1] = $sString
    EndIf
    WEnd
    EndIf
    WEnd
    Return $aKV
    EndIf
    If $sSection <> "" And $sKey <> "" Then
    $sFile = FileRead($sPath)
    $aA = _StringBetween($sFile, $sKey & "=", @CRLF)
    If IsArray($aA) = 0 Then
    $aA = _StringBetween($sFile, $sKey, @CR)
    EndIf
    If IsArray($aA) = 0 Then
    $sValue = $sDefault
    Else
    $sValue = $aA[0]
    EndIf
    Return $sValue
    EndIf
    EndIf
    EndFunc

    [/autoit]

    3 Mal editiert, zuletzt von Aquaplant (27. April 2010 um 17:26)

  • Das Problem bleibt aber weiterhin, dass ein Array "nur" 999 (1000) "Plätze" hat.

    stimmt nich ganz. hatte erst letztens ein array mit 100000 indizies

    maximal 32.000 irgendwas Buchstaben

    biste dir da sicher? bzw wie meinst du das genau? pro key?

  • Hallo Schnitzel,

    Zitat von Hilfe zu IniReadSection

    Bemerkungen

    Eine standardkonforme .ini-Datei sieht folgendermaßen aus:
    [SektionsName]
    Schlüssel=Wert

    Die Anzahl der zurückgegebenen Elemente steht in $result[0][0]. Wenn ein @error auftaucht, wird kein Array erstellt.
    Nur die ersten 32767 Zeichen werden aufgrund der Win9x-Kompatibilität in einer Sektion berücksichtigt.

    mfg (Auto)Bert

  • Zitat

    stimmt nich ganz. hatte erst letztens ein array mit 100000 indizies


    Klasse, dass klappt ja tatsächlich. Bei mir gab es früher immer eine Fehlermeldung, wenn ich eine Zahl größer 999 eingesetzt habe :)