#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)
			$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

