Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Func _FileFindLine($sFile, $sText, $sRet = "")
- ttc("Calling _FileFindLine(" & $sFile & ", " & $sText & ", " & $sRet & ")", "db")
- ;~ ConsoleWrite(" Suche nach '"&$sText&"' in File "&StringTrimLeft($sFile,StringInStr($sFile,"\",0,-1))& @CRLF)
- If Not IsString($sText) Then Return SetError(1, 0, 0)
- If Not FileExists($sFile) Then Return SetError(2, 0, 0)
- If $sText = "" Then Return SetError(3, 0, 0)
- Local $filtxt = FileRead($sFile)
- ;~ Local $tmp_arr = StringSplit($filtxt, @CRLF, 1)
- ;~ If $tmp_arr[0] < $iLine Then Return SetError(1, 0, 0)
- If $sRet = "" Then ; return 2dim-array of the line numbers [0] and lines (text) [1], where string is found
- $occ = 0
- $ind = 0
- $RegExpArr = StringRegExp($filtxt, "(.*\Q" & $sText & "\E[^\r\n]*)", 3)
- If @error Then Return -1
- $filtxt = StringSplit($filtxt, $sText, 1)
- If @error Or $filtxt[0] = 1 Then Return -1
- ;~ _ArrayDisplay($filtxt,"$filtxt")
- Dim $iLine[$filtxt[0] - 1][2]
- For $r = 1 To $filtxt[0] - 1
- StringRegExpReplace($filtxt[$r], "\r\n", "")
- Local $sExt = @extended
- ;~ ConsoleWrite($r&" : "&$occ & @CRLF)
- If $occ > 0 Then
- If $sExt = 0 Then
- ;~ ConsoleWrite("Search String in same Line:"&$iLine[$occ][0] & @CRLF)
- ContinueLoop
- EndIf
- $iLine[$occ][0] = $sExt + 1
- If $ind >= UBound($RegExpArr) Then
- $iLine[$occ][1] = $RegExpArr[UBound($RegExpArr) - 1]
- $iLine[$occ][0] += $iLine[$occ - 1][0] - 1
- $ind += 1
- $occ += 1
- ContinueLoop
- EndIf
- $iLine[$occ][1] = $RegExpArr[$ind]
- $iLine[$occ][0] += $iLine[$occ - 1][0] - 1
- $ind += 1
- $occ += 1
- ContinueLoop
- ElseIf $sExt = 0 Then
- ;~ ConsoleWrite("Text in Header Line" & @CRLF)
- $ind = 1
- ContinueLoop ; found text in Header Line -> Ignore
- EndIf
- $iLine[$occ][1] = $RegExpArr[$ind]
- $iLine[$occ][0] = $sExt + 1
- $ind += 1
- $occ += 1
- Next
- ;~ _ArrayDisplay($iLine)
- ElseIf $sRet = 1 Then ; return 1dim-array of the line numbers where string is found
- $occ = 0
- $filtxt = StringSplit($filtxt, $sText, 1)
- If Not $filtxt[0] Then Return -1
- Dim $iLine[$filtxt[0] - 1]
- For $r = 1 To $filtxt[0] - 1
- StringRegExpReplace($filtxt[$r], "\r\n", "")
- Local $sExt = @extended
- $iLine[$occ] = $sExt + 1
- If $occ > 0 Then
- If $sExt = 0 Then
- ;~ ConsoleWrite("Search String in same Line:"&$iLine[$occ] & @CRLF)
- ContinueLoop
- EndIf
- $iLine[$occ] += $iLine[$occ - 1] - 1
- EndIf
- $occ += 1
- Next
- ElseIf $sRet = 2 Then ; return 1dim-array of the lines (text) where string is found
- $occ = 0
- $filtxt = StringSplit($filtxt, $sText, 1)
- ;~ ConsoleWrite("spl:"&$filtxt[0] & @CRLF)
- If Not $filtxt[0] Then Return -1
- Dim $iLine[$filtxt[0] - 1]
- For $r = 1 To $filtxt[0] - 1
- ;~ $iPos = StringLen($filtxt[$r])
- ;~ If $iPos Then ExitLoop
- ;~ ConsoleWrite("Pos:"&$r & @CRLF)
- StringRegExpReplace($filtxt[$r], "\r\n", "")
- Local $sExt = @extended
- $iLine[$occ] = StringRegExpReplace($filtxt[$r], "(?:.*?\r\n)*(.+)(?:\r\n)?\z", "\1", 1)
- If @error Then
- ;~ ConsoleWrite($r&": 1st Line"& @CRLF )
- $iLine[$occ] = StringRegExpReplace($filtxt[$r], "\A(\r\n)*(.*?)\r\n(.*\r\n)*(\r\n)*", "\2", 1)
- EndIf
- ;~ ConsoleWrite( @extended &"-conc: "&$iLine[$occ]&" with:")
- ;~ If $occ > 0 Then
- If $sExt = 0 Then
- ;~ ConsoleWrite("Search String in same Line:"&$iLine[$occ] & @CRLF)
- $iLine[$occ] &= $sText & $filtxt[$r]
- ContinueLoop
- EndIf
- $eRet = StringRegExp($filtxt[$r + 1], "(\A.+)*(?:\r\n)", 1)
- If Not IsArray($eRet) Then
- ;~ ConsoleWrite("2Search String in same Line:"&$iLine[$occ] & @CRLF)
- $iLine[$occ] &= $sText & $filtxt[$r + 1]
- ContinueLoop
- EndIf
- ;~ ConsoleWrite("n:"&$eRet[0] & @CRLF)
- If $eRet[0] = @CRLF Then $eRet[0] = ""
- $iLine[$occ] &= $sText & $eRet[0]
- $occ += 1
- Next
- EndIf
- ;~ _ArrayDisplay($iLine)
- Switch UBound($iLine, 0)
- Case 1
- If $iLine[UBound($iLine) - 1] == "" Then
- $sUbIl = UBound($iLine) - 2
- While $iLine[$sUbIl] == ""
- $sUbIl -= 1
- WEnd
- ReDim $iLine[$sUbIl + 1]
- EndIf
- Case 2
- If $iLine[UBound($iLine) - 1][0] == "" Then
- $sUbIl = UBound($iLine) - 2
- While $iLine[$sUbIl][0] == ""
- $sUbIl -= 1
- WEnd
- ReDim $iLine[$sUbIl + 1][2]
- EndIf
- EndSwitch
- ;~ _ArrayDisplay($iLine)
- Return $iLine
- EndFunc ;==>_FileFindLine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement