Modify

Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#1134 closed Bug (Fixed)

Code cleanup of _FileWriteToLine()

Reported by: partypooper@… Owned by: Jpm
Milestone: 3.3.1.2 Component: Standard UDFs
Version: Other Severity: None
Keywords: _FileWriteToLine() Cc:

Description

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

Attachments (0)

Change History (5)

comment:1 Changed 15 years ago by TicketCleanup

  • Version 3.3.1.0 deleted

Automatic ticket cleanup.

comment:2 Changed 15 years ago by MrCreatoR <mscreator@…>

This function is litle bit bugy...

Reproduce example:

#include <File.au3>

$sFile = @ScriptDir & "\File.txt"

$hFile = FileOpen($sFile, 2)

For $i = 1 To 20
	FileWrite($hFile, "Line #" & $i & @LF)
Next

FileClose($hFile)

$iLine = 12

_FileWriteToLine($sFile, $iLine, "Hello World", 0)
ConsoleWrite(@error & @CRLF)

Here we get @error = 1, but there is 20 lines in file, yes, the lines are delimited by @LF not @CRLF, but some text editors do the same when we manualy writing lines to file, so we need to take this in count.

So here is a fixed version, plus speed improvements:

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 $sRead_File = FileRead($sFile)
	Local $aSplit_File = StringSplit(StringStripCR($sRead_File), @LF)
	If UBound($aSplit_File) < $iLine Then Return SetError(1, 0, 0)
	
	$sRead_File = ""
	
	For $i = 1 To $aSplit_File[0]
		If $i = $iLine Then
			If $fOverWrite = 1 Then
				If $sText <> '' Then $sRead_File &= $sText & @CRLF
			Else
				$sRead_File &= $sText & @CRLF & $aSplit_File[$i] & @CRLF
			EndIf
		ElseIf $i < $aSplit_File[0] Then
			$sRead_File &= $aSplit_File[$i] & @CRLF
		ElseIf $i = $aSplit_File[0] Then
			$sRead_File &= $aSplit_File[$i]
		EndIf
	Next
	
	Local $hFile = FileOpen($sFile, 2)
	If $hFile = -1 Then Return SetError(3, 0, 0)
	
	FileWrite($hFile, $sRead_File)
	FileClose($hFile)
	
	Return 1
EndFunc

comment:3 Changed 15 years ago by Jpm

  • Type changed from Feature Request to Bug

for as demonstrated by MrCreatoR it is more than a bug

comment:4 Changed 15 years ago by Jpm

  • Milestone set to 3.3.1.2
  • Owner changed from Gary to Jpm
  • Resolution set to Fixed
  • Status changed from new to closed

Fixed in version: 3.3.1.2

comment:5 Changed 15 years ago by TicketCleanup

  • Version set to Other

Automatic ticket cleanup.

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The owner will remain Jpm.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.