Modify

Opened 14 years ago

Closed 14 years ago

#1712 closed Bug (Fixed)

_FileWriteFromArray() crash.

Reported by: Spiff59 Owned by: Jpm
Milestone: 3.3.7.0 Component: AutoIt
Version: 3.3.6.0 Severity: None
Keywords: Cc:

Description

If FileWriteFromArray() is passed other than a 1-dimension array, it crashes.

I think it could use the addition of the following line:

If UBound($a_Array, 0) > 1 Then Return SetError(4, 0, 0)

The new error condition would need to be documented in the helpfile, as well as possibly mentioning in the finction definition it's limitation to working only with single-dimension arrays.

Thank you.

PS - You'd still need this new error check (except testing for > 2), but, allowing a 1 or 2 dimension array in this function might be useful? Seems you'd only have to nest a second loop inside the existing write loop to string together elements from the second dimension using some delimiter. An $sDelim parameter added to the function call would allow the flexibility to string columns from the second dimension into lines like:
Field1|Field2|Field3
or
Field1, Field2, Field3

Attachments (0)

Change History (4)

comment:1 Changed 14 years ago by Spiff59

This adds the @error = 4 message to prohibit the function from crashing, and allows for either 1-dimension or 2-dimension arrays.

; #FUNCTION# ====================================================================================================================
; Name...........: _FileWriteFromArray
; Description ...: Writes Array records to the specified file.
; Syntax.........: _FileWriteFromArray($File, $a_Array[, $i_Base = 0[, $i_UBound = 0], [$s_Delim]])
; Parameters ....: $File     - String path of the file to write to, or a file handle returned from FileOpen().
;                  $a_Array  - The array to be written to the file.
;                  $i_Base   - Optional: Start Array index to read, normally set to 0 or 1. Default=0
;                  $i_Ubound - Optional: Set to the last record you want to write to the File. default=0 - whole array.
;                  $s_Delim - Optional: Delimiter character(s) for 2-dimension arrays. default="|"
; Return values .: Success - Returns a 1
;                  Failure - Returns a 0
;                  @Error  - 0 = No error.
;                  |1 = Error opening specified file
;                  |2 = Input is not an Array
;                  |3 = Error writing to file
;                  |4 = Array dimensions > 2
; Author ........: Jos van der Zande <jdeb at autoitscript dot com>
; Modified.......: Updated for file handles by PsaltyDS, @error = 4 msg and 2-dimension capability added by SPiff59
; Remarks .......: If a string path is provided, the file is overwritten and closed.
;                  To use other write modes, like append or Unicode formats, open the file with FileOpen() first and pass the file handle instead.
;                  If a file handle is passed, the file will still be open after writing.
; Related .......: _FileReadToArray
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _FileWriteFromArray2($File, $a_Array, $i_Base = 0, $i_UBound = 0, $s_Delim = "|")
	; Check if we have a valid array as input
	If Not IsArray($a_Array) Then Return SetError(2, 0, 0)
	Local $iDims = UBound($a_Array, 0)
	If $iDims > 2 Then Return SetError(4, 0, 0)

	; determine last entry
	Local $last = UBound($a_Array) - 1
	If $i_UBound < 1 Or $i_UBound > $last Then $i_UBound = $last
	If $i_Base < 0 Or $i_Base > $last Then $i_Base = 0

	; Open output file for overwrite by default, or use input file handle if passed
	Local $hFile
	If IsString($File) Then
		$hFile = FileOpen($File, $FO_OVERWRITE)
	Else
		$hFile = $File
	EndIf
	If $hFile = -1 Then Return SetError(1, 0, 0)

	; Write array data to file
	Local $ErrorSav = 0, $s_Temp
	Switch $iDims
		Case 1
			For $x = $i_Base To $i_UBound
				If FileWrite($hFile, $a_Array[$x] & @CRLF) = 0 Then
					$ErrorSav = 3
					ExitLoop
				EndIf
			Next
		Case 2
			For $x = $i_Base To $i_UBound
				$sTemp = $a_Array[$x][0]
				For $y = 1 to $iDims
					$sTemp &= $s_Delim & $a_Array[$x][$y]
				Next
				If FileWrite($hFile, $sTemp & @CRLF) = 0 Then
					$ErrorSav = 3
					ExitLoop
				EndIf
			Next
	EndSwitch

	; Close file only if specified by a string path
	If IsString($File) Then FileClose($hFile)

	; Return results
	If $ErrorSav Then Return SetError($ErrorSav, 0, 0)
	Return 1
EndFunc   ;==>_FileWriteFromArray

comment:2 Changed 14 years ago by Spiff59

Poop! The Local definition I stuck in to get rid of the compile warning is incorrect. Needs to be "$sTemp" not "$s_Temp" (or globally replace $sTemp to $s_Temp)

comment:3 Changed 14 years ago by anonymous

Am in too big a rush...
The function name in the example was left at _FileWriteFromArray2, which I used for testing. This would be the appropriate prod version:

; #FUNCTION# ====================================================================================================================
; Name...........: _FileWriteFromArray
; Description ...: Writes Array records to the specified file.
; Syntax.........: _FileWriteFromArray($File, $a_Array[, $i_Base = 0[, $i_UBound = 0], [$s_Delim]])
; Parameters ....: $File     - String path of the file to write to, or a file handle returned from FileOpen().
;                  $a_Array  - The array to be written to the file.
;                  $i_Base   - Optional: Start Array index to read, normally set to 0 or 1. Default=0
;                  $i_Ubound - Optional: Set to the last record you want to write to the File. default=0 - whole array.
;                  $s_Delim - Optional: Delimiter character(s) for 2-dimension arrays. default="|"
; Return values .: Success - Returns a 1
;                  Failure - Returns a 0
;                  @Error  - 0 = No error.
;                  |1 = Error opening specified file
;                  |2 = Input is not an Array
;                  |3 = Error writing to file
;                  |4 = Array dimensions > 2
; Author ........: Jos van der Zande <jdeb at autoitscript dot com>
; Modified.......: Updated for file handles by PsaltyDS, @error = 4 msg and 2-dimension capability added by SPiff59
; Remarks .......: If a string path is provided, the file is overwritten and closed.
;                  To use other write modes, like append or Unicode formats, open the file with FileOpen() first and pass the file handle instead.
;                  If a file handle is passed, the file will still be open after writing.
; Related .......: _FileReadToArray
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _FileWriteFromArray($File, $a_Array, $i_Base = 0, $i_UBound = 0, $s_Delim = "|")
	; Check if we have a valid array as input
	If Not IsArray($a_Array) Then Return SetError(2, 0, 0)
	Local $iDims = UBound($a_Array, 0)
	If $iDims > 2 Then Return SetError(4, 0, 0)

	; determine last entry
	Local $last = UBound($a_Array) - 1
	If $i_UBound < 1 Or $i_UBound > $last Then $i_UBound = $last
	If $i_Base < 0 Or $i_Base > $last Then $i_Base = 0

	; Open output file for overwrite by default, or use input file handle if passed
	Local $hFile
	If IsString($File) Then
		$hFile = FileOpen($File, $FO_OVERWRITE)
	Else
		$hFile = $File
	EndIf
	If $hFile = -1 Then Return SetError(1, 0, 0)

	; Write array data to file
	Local $ErrorSav = 0, $s_Temp
	Switch $iDims
		Case 1
			For $x = $i_Base To $i_UBound
				If FileWrite($hFile, $a_Array[$x] & @CRLF) = 0 Then
					$ErrorSav = 3
					ExitLoop
				EndIf
			Next
		Case 2
			For $x = $i_Base To $i_UBound
				$s_Temp = $a_Array[$x][0]
				For $y = 1 to $iDims
					$s_Temp &= $s_Delim & $a_Array[$x][$y]
				Next
				If FileWrite($hFile, $s_Temp & @CRLF) = 0 Then
					$ErrorSav = 3
					ExitLoop
				EndIf
			Next
	EndSwitch

	; Close file only if specified by a string path
	If IsString($File) Then FileClose($hFile)

	; Return results
	If $ErrorSav Then Return SetError($ErrorSav, 0, 0)
	Return 1
EndFunc   ;==>_FileWriteFromArray

comment:4 Changed 14 years ago by Jpm

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

Fixed by revision [5914] in version: 3.3.7.0

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.