﻿id	summary	reporter	owner	description	type	status	milestone	component	version	severity	resolution	keywords	cc
1134	Code cleanup of  _FileWriteToLine()	partypooper@…	Jpm	"Just a slight code clean-up by reducing the number of lines required for the function to operate.


{{{
; #FUNCTION# ====================================================================================================================
; Name...........: _FileWriteToLine
; Description ...: Writes text to a specific line in a file.
; Syntax.........: _FileWriteToLine($sFile, $iLine, $sText[, $fOverWrite = 0])
; Parameters ....: $sFile      - The file to write to
;                  $iLine      - The line number to write to
;                  $sText      - The text to write
;                  $fOverWrite - If set to 1 will overwrite the old line
;                  |If set to 0 will not overwrite
; Return values .: Success - 1
;                  Failure - 0
;                  @Error  - 0 = No error
;                  |1 = File has less lines than $iLine
;                  |2 = File does not exist
;                  |3 = Error when opening file
;                  |4 = $iLine is invalid
;                  |5 = $fOverWrite is invalid
;                  |6 = $sText is invalid
; Author ........: cdkid
; Modified.......: partypooper - code cleanup
; Remarks .......: If _FileWriteToLine is called with $fOverWrite as 1 and $sText as """", it will delete the line.
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0)
	If $iLine <= 0 Then Return SetError(4, 0, 0)
	If Not IsString($sText) Then Return SetError(6, 0, 0)
	If $fOverWrite <> 0 And $fOverWrite <> 1 Then Return SetError(5, 0, 0)
	If Not FileExists($sFile) Then Return SetError(2, 0, 0)
	Local $filtxt = FileRead($sFile, FileGetSize($sFile))
	$filtxt = StringSplit($filtxt, @CRLF, 1)
	If UBound($filtxt, 1) < $iLine Then Return SetError(1, 0, 0)
	Local $fil = FileOpen($sFile, 2)
	If $fil = -1 Then Return SetError(3, 0, 0)
	For $i = 1 To UBound($filtxt) - 1
		If $i = $iLine Then
			If Not $fOverWrite Then ; overwrite option 'off'
				FileWrite($fil, $sText & @CRLF) ; insert new line
				FileWrite($fil, $filtxt[$i] & @CRLF) ; write old line after new
			Else ; overwrite option 'on'
				If $sText <> """" Then FileWrite($fil, $sText & @CRLF) ; write new line in place of old
			EndIf
		ElseIf $i < UBound($filtxt, 1) - 1 Then
			FileWrite($fil, $filtxt[$i] & @CRLF)
		ElseIf $i = UBound($filtxt, 1) - 1 Then
			FileWrite($fil, $filtxt[$i])
		EndIf
	Next
	FileClose($fil)
	Return 1
EndFunc   ;==>_FileWriteToLine

}}}
"	Bug	closed	3.3.1.2	Standard UDFs	Other	None	Fixed	_FileWriteToLine()	
