Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 4.43 KB | None | 0 0
  1.     Func _FileFindLine($sFile, $sText, $sRet = "")
  2.         ttc("Calling _FileFindLine(" & $sFile & ", " & $sText & ", " & $sRet & ")", "db")
  3. ;~  ConsoleWrite("  Suche nach '"&$sText&"' in File "&StringTrimLeft($sFile,StringInStr($sFile,"\",0,-1))& @CRLF)
  4.         If Not IsString($sText) Then Return SetError(1, 0, 0)
  5.         If Not FileExists($sFile) Then Return SetError(2, 0, 0)
  6.         If $sText = "" Then Return SetError(3, 0, 0)
  7.         Local $filtxt = FileRead($sFile)
  8. ;~  Local $tmp_arr = StringSplit($filtxt, @CRLF, 1)
  9. ;~  If $tmp_arr[0] < $iLine Then Return SetError(1, 0, 0)
  10.  
  11.         If $sRet = "" Then ; return 2dim-array of the  line numbers [0] and lines (text) [1], where string is found
  12.             $occ = 0
  13.             $ind = 0
  14.             $RegExpArr = StringRegExp($filtxt, "(.*\Q" & $sText & "\E[^\r\n]*)", 3)
  15.             If @error Then Return -1
  16.             $filtxt = StringSplit($filtxt, $sText, 1)
  17.             If @error Or $filtxt[0] = 1 Then Return -1
  18. ;~      _ArrayDisplay($filtxt,"$filtxt")
  19.             Dim $iLine[$filtxt[0] - 1][2]
  20.             For $r = 1 To $filtxt[0] - 1
  21.                 StringRegExpReplace($filtxt[$r], "\r\n", "")
  22.                 Local $sExt = @extended
  23. ;~          ConsoleWrite($r&" : "&$occ & @CRLF)
  24.                 If $occ > 0 Then
  25.                     If $sExt = 0 Then
  26. ;~                  ConsoleWrite("Search String in same Line:"&$iLine[$occ][0] & @CRLF)
  27.                         ContinueLoop
  28.                     EndIf
  29.                     $iLine[$occ][0] = $sExt + 1
  30.                     If $ind >= UBound($RegExpArr) Then
  31.                         $iLine[$occ][1] = $RegExpArr[UBound($RegExpArr) - 1]
  32.                         $iLine[$occ][0] += $iLine[$occ - 1][0] - 1
  33.                         $ind += 1
  34.                         $occ += 1
  35.                         ContinueLoop
  36.                     EndIf
  37.                     $iLine[$occ][1] = $RegExpArr[$ind]
  38.                     $iLine[$occ][0] += $iLine[$occ - 1][0] - 1
  39.                     $ind += 1
  40.                     $occ += 1
  41.                     ContinueLoop
  42.                 ElseIf $sExt = 0 Then
  43. ;~              ConsoleWrite("Text in Header Line" & @CRLF)
  44.                     $ind = 1
  45.                     ContinueLoop ; found text in Header Line -> Ignore
  46.                 EndIf
  47.                 $iLine[$occ][1] = $RegExpArr[$ind]
  48.                 $iLine[$occ][0] = $sExt + 1
  49.                 $ind += 1
  50.                 $occ += 1
  51.             Next
  52.  
  53. ;~      _ArrayDisplay($iLine)
  54.         ElseIf $sRet = 1 Then ; return 1dim-array of the line numbers where string is found
  55.             $occ = 0
  56.             $filtxt = StringSplit($filtxt, $sText, 1)
  57.             If Not $filtxt[0] Then Return -1
  58.             Dim $iLine[$filtxt[0] - 1]
  59.             For $r = 1 To $filtxt[0] - 1
  60.                 StringRegExpReplace($filtxt[$r], "\r\n", "")
  61.                 Local $sExt = @extended
  62.                 $iLine[$occ] = $sExt + 1
  63.                 If $occ > 0 Then
  64.                     If $sExt = 0 Then
  65. ;~                  ConsoleWrite("Search String in same Line:"&$iLine[$occ] & @CRLF)
  66.                         ContinueLoop
  67.                     EndIf
  68.                     $iLine[$occ] += $iLine[$occ - 1] - 1
  69.                 EndIf
  70.                 $occ += 1
  71.             Next
  72.         ElseIf $sRet = 2 Then ; return 1dim-array of the lines (text) where string is found
  73.             $occ = 0
  74.             $filtxt = StringSplit($filtxt, $sText, 1)
  75. ;~      ConsoleWrite("spl:"&$filtxt[0] & @CRLF)
  76.             If Not $filtxt[0] Then Return -1
  77.             Dim $iLine[$filtxt[0] - 1]
  78.             For $r = 1 To $filtxt[0] - 1
  79. ;~              $iPos = StringLen($filtxt[$r])
  80. ;~              If $iPos Then ExitLoop
  81. ;~          ConsoleWrite("Pos:"&$r & @CRLF)
  82.                 StringRegExpReplace($filtxt[$r], "\r\n", "")
  83.                 Local $sExt = @extended
  84.                 $iLine[$occ] = StringRegExpReplace($filtxt[$r], "(?:.*?\r\n)*(.+)(?:\r\n)?\z", "\1", 1)
  85.                 If @error Then
  86. ;~              ConsoleWrite($r&": 1st Line"& @CRLF  )
  87.                     $iLine[$occ] = StringRegExpReplace($filtxt[$r], "\A(\r\n)*(.*?)\r\n(.*\r\n)*(\r\n)*", "\2", 1)
  88.                 EndIf
  89. ;~          ConsoleWrite( @extended &"-conc: "&$iLine[$occ]&" with:")
  90. ;~          If $occ > 0 Then
  91.                 If $sExt = 0 Then
  92. ;~              ConsoleWrite("Search String in same Line:"&$iLine[$occ] & @CRLF)
  93.                     $iLine[$occ] &= $sText & $filtxt[$r]
  94.                     ContinueLoop
  95.                 EndIf
  96.                 $eRet = StringRegExp($filtxt[$r + 1], "(\A.+)*(?:\r\n)", 1)
  97.                 If Not IsArray($eRet) Then
  98. ;~              ConsoleWrite("2Search String in same Line:"&$iLine[$occ] & @CRLF)
  99.                     $iLine[$occ] &= $sText & $filtxt[$r + 1]
  100.                     ContinueLoop
  101.                 EndIf
  102. ;~          ConsoleWrite("n:"&$eRet[0] & @CRLF)
  103.                 If $eRet[0] = @CRLF Then $eRet[0] = ""
  104.                 $iLine[$occ] &= $sText & $eRet[0]
  105.  
  106.                 $occ += 1
  107.  
  108.             Next
  109.  
  110.         EndIf
  111. ;~  _ArrayDisplay($iLine)
  112.         Switch UBound($iLine, 0)
  113.             Case 1
  114.                 If $iLine[UBound($iLine) - 1] == "" Then
  115.                     $sUbIl = UBound($iLine) - 2
  116.                     While $iLine[$sUbIl] == ""
  117.                         $sUbIl -= 1
  118.                     WEnd
  119.                     ReDim $iLine[$sUbIl + 1]
  120.                 EndIf
  121.             Case 2
  122.                 If $iLine[UBound($iLine) - 1][0] == "" Then
  123.                     $sUbIl = UBound($iLine) - 2
  124.                     While $iLine[$sUbIl][0] == ""
  125.                         $sUbIl -= 1
  126.                     WEnd
  127.                     ReDim $iLine[$sUbIl + 1][2]
  128.                 EndIf
  129.         EndSwitch
  130.  
  131. ;~  _ArrayDisplay($iLine)
  132.  
  133.         Return $iLine
  134.  
  135.     EndFunc   ;==>_FileFindLine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement