﻿;-- TIME_STAMP   2022-03-16 12:36:59   v 0.1


#include <File.au3>

;===================================================================================================
; Function Name....: _TabReplace_File
; Description......: Replaces TAB in all lines of the file with the number of spaces corresponding to the columns.
; .................: Overwrites the file with the replaced content.
; Parameter(s).....: $_sPath    The files path.
; .................: $_iMode    The files mode. Default is UTF8-with-BOM(128).
; .................: $_iTabsize TAB size in number of characters. If it is omitted, 4 is used.
; Return Value(s)..: Count of replacements
; Author...........: BugFix (autoit<at>bug-fix.info)
;===================================================================================================
Func _TabReplace_File($_sPath, $_iMode=128, $_iTabsize=4)
    If Not FileExists($_sPath) Then Return SetError(1,0,0)
    Local $fh = FileOpen($_sPath, $_iMode)
    Local $aFile, $iReplacements = 0, $sWrite, $sLine
    _FileReadToArray($fh, $aFile, 0)
    FileClose($fh)
    For $i = 0 To UBound($aFile) -1
        $sLine = $aFile[$i]
        $iReplacements += _TabReplace_Line($sLine, $_iTabsize)
        $sWrite &= $sLine & @CRLF
    Next
    If $iReplacements = 0 Then Return 0
    Local $fh = FileOpen($_sPath, $_iMode + 2)
    FileWrite($fh, $sWrite)
    FileClose($fh)
    Return $iReplacements
EndFunc  ;==>_TabReplace_File


;===================================================================================================
; Function Name....: _TabReplace_Line
; Description......: Replaces TAB in a row (ByRef) with the number of spaces corresponding to the column.
; Parameter(s).....: $_line     A line of text whose TAB are to be replaced by spaces.
; .................: $_iTabsize TAB size in number of characters. If it is omitted, 4 is used.
; Return Value(s)..: Count of replacements
; Author...........: BugFix (autoit<at>bug-fix.info)
;===================================================================================================
Func _TabReplace_Line(ByRef $_line, $_iTabsize=4)
    If StringRegExp($_line, '^[\r\n]+$') Then Return 0
    If $_line = '' Then Return 0
    StringRegExp($_line, '\t', 1)
    Local $posTab = @extended -1
    If $posTab = -1 Then Return 0
    Local $aTab[50]
    Local $sRepl = '', $iLen = 0, $sumLen = 0, $index = -1
    Local $aRepl[] = ['',' ','  ','   ','    ']
    While $posTab <> -1
        $index += 1
        If $index = UBound($aTab) Then ReDim $aTab[UBound($aTab)+50]
        $iLen = ($_iTabsize - Mod(($posTab + $sumLen -1), $_iTabsize))
        $sumLen += $iLen -1
        $sRepl = $aRepl[$iLen]
        $aTab[$index] = $sRepl
        StringRegExp($_line, '\t', 1, $posTab +1)
        $posTab = @extended -1
    WEnd
    ; Ersetzung
    For $i = 0 To $index
        $_line = StringRegExpReplace($_line, '\t', $aTab[$i], 1)
    Next
    Return $index +1
EndFunc  ;==>_TabReplace_Line

