Sign in to follow this  
Followers 0

Update _FileWriteFromArray() to accept a handle

16 posts in this topic

Posted

Come up with in the following topic: Array

This is the demo:

#include <file.au3>

Dim $avArray, $sSrc = "C:\Temp\Test1.txt", $sDest = "C:\Temp\Test2.txt"
_FileReadToArray($sSrc, $avArray)

$hDest = FileOpen($sDest, 1) ; 1 = Append
; ConsoleWrite("Debug: $hDest = " & VarGetType($hDest) & @LF) ; file handle type = int
__FileWriteFromArray($hDest, $avArray, 1)
FileClose($hDest)

Run("notepad.exe " & $sDest)

;===============================================================================
;
; Description:      Write array to File.
; Syntax:           __FileWriteFromArray( $sFilePath, $aArray )
; Parameter(s):     $sFilePath - Path and filename of the file to be written, or file handle
;                   $a_Array   - The array to retrieve the contents
;                   $i_Base    - Start reading at this Array entry.
;                   $I_Ubound  - End reading at this Array entry.
;                                Default UBound($a_Array) - 1
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error = 1
; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
;           Modified by PsaltyDS at the AutoIt forums.
; Note(s):          None
;
;===============================================================================
Func __FileWriteFromArray($sFilePath, $a_Array, $i_Base = 0, $i_UBound = 0)
    ; Check if we have a valid array as input
    If Not IsArray($a_Array) Then Return SetError(2, 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
    If IsString($sFilePath) Then
        Local $hFile = FileOpen($sFilePath, 2)
    Else
        Local $hFile = $sFilePath
    EndIf
    If $hFile = -1 Then Return SetError(1, 0, 0)
   
    FileWrite($hFile, $a_Array[$i_Base])
    For $x = $i_Base + 1 To $i_UBound
        FileWrite($hFile, @CRLF & $a_Array[$x])
    Next

    If IsString($sFilePath) Then FileClose($hFile)
    Return 1
EndFunc   ;==>__FileWriteFromArray

The function __FileWriteFromArray() is a simple change to the original _FileWriteFromArray() that accepts a file handle from an already open file. This allows the user to open the file in a different mode than the default 2, including the use of non-ANSI modes. Seems to add a lot of functionality to it without any harm to legacy scripts.

As auxiliary request, there doesn't seem to be anything like IsFileHandle() to test the input value with. A file handle is just and INT according to VarGetType() in the beta. If it is internally identifiable to the interpreter this would be handy to implement the same easy change to other functions. For this I simply tested the input path, and assume if it is not a string, it must be a file handle. That test has to happen at two places, for the FileOpen, and the FileClose.

<_<

Share this post


Link to post
Share on other sites



Posted

just sent me the updated File.au3 (using the current supplied file.au3 from the latest BETA) and updated version of :

_FileWriteFromArray.txt and _FileWriteFromArray.au3 for the generation of the helpfile page.

(current versions attached)

Jos <_<

Share this post


Link to post
Share on other sites

Posted

just sent me the updated File.au3 (using the current supplied file.au3 from the latest BETA) and updated version of :

_FileWriteFromArray.txt and _FileWriteFromArray.au3 for the generation of the helpfile page.

(current versions attached)

Jos <_<

Updated files attached. Made the following changes:

1. Tweaked text in function header.

2. Added error detection for the FileWrite() function used internally, returns @error = 3 if failure.

3. Wrote new example.

4. Updated help file text to match.

Here is the test I ran on the new version of the function:

$avOne = StringSplit("0,1,2,3,4,5,6,7,8,9,10", ",")
$avTwo = StringSplit("Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten", ",")
$sTxtFile = @ScriptDir & "\Test.txt" 

; Using string file path
_FileWriteFromArray($sTxtFile, $avOne, 2, 10) ; Write "1" thru "9"

; Using file handle
$hTxtFile = FileOpen($sTxtFile, 1) ; 1 = append
_FileWriteFromArray($hTxtFile, $avTwo, 2, 10) ; Write "One" thru "Nine"
FileClose($hTxtFile)

; Test new @error = 3
$iRet = _FileWriteFromArray($hTxtFile, $avTwo, 2, 10) ; Write "One" thru "Nine"
ConsoleWrite("Debug: $iRet = " & $iRet & "  @error = " & @error & @LF)

; Display results
Run("notepad.exe " & $sTxtFile)


;===============================================================================
;
; Description:      Write array to File.
; Syntax:           _FileWriteFromArray( $File, $aArray, $i_Base, $i_UBound )
; Parameter(s):     $File - Path and filename of the file to be written
;                   $a_Array   - The array to retrieve the contents
;                   $i_Base    - Start reading at this Array entry.
;                   $I_Ubound  - End reading at this Array entry.
;                                Default UBound($a_Array) - 1
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error:
;                        @error = 0 - No error
;                        @error = 1 - Error opening specified file
;                        @error = 2 - Input is not an Array
;                        @error = 3 - Error writing to file
; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
;                   Updated for file handles by PsaltyDS at the AutoIt forums.
; Note(s):          If $File is a string it is assumed to be a file path, else it is a file handle.
;                   If $File is a handle the file is already open for write and is not closed by this function.
;===============================================================================
Func _FileWriteFromArray($File, $a_Array, $i_Base = 0, $i_UBound = 0)
    ; Check if we have a valid array as input
    If Not IsArray($a_Array) Then Return SetError(2, 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
    If IsString($File) Then
        Local $hFile = FileOpen($File, 2)
    Else
        Local $hFile = $File
    EndIf
    If $hFile = -1 Then Return SetError(1, 0, 0)

    ; Write array data to file
    Local $ErrorSav = 0
    For $x = $i_Base To $i_UBound
        If FileWrite($hFile, @CRLF & $a_Array[$x]) = 0 Then
            $ErrorSav = 3
            ExitLoop
        EndIf
    Next
    
    ; 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)
    Else
        Return 1
    EndIf
EndFunc   ;==>_FileWriteFromArray

:)

_FileWriteFromArray.new.zip

Share this post


Link to post
Share on other sites

Posted

Update committed for the next release.

I made one minor change to avoid errors in AU3CHECK when using all checks ... (-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6)

Thanks

Jos

Share this post


Link to post
Share on other sites

Posted (edited)

This...

If IsString($File) Then
        Local $hFile = FileOpen($File, 2)
    Else
        Local $hFile = $File
    EndIfoÝ÷ Ø*.ÖÞjëh×6    Local $hFile = $File
    If IsString($hFile) Then $hFile = FileOpen($File, 2)

One of the issues I see here (and you know it's going to happen), is when someone passes a handle in read mode.

Edit:

Fixed Ebonics

Edited by SmOke_N

Share this post


Link to post
Share on other sites

Posted (edited)

This...

If IsString($File) Then
        Local $hFile = FileOpen($File, 2)
    Else
        Local $hFile = $File
    EndIfoÝ÷ Ø*.ÖÞjëh×6    Local $hFile = $File
    If IsString($hFile) Then $hFile = FileOpen($File, 2)

One of the issues I see here (and you know it's going to happen), is when someone passes a handle in read mode.

Edit:

Fixed Ebonics

Yea, but the UDF will return error 3 when that happens (@error = 3 - Error writing to file)

So assuming people test for @error, which ofcourse everybody does, there should be no issue <_<

Edited by Jos

Share this post


Link to post
Share on other sites

Posted

Yea, but the UDF will return error 3 when that happens (@error = 3 - Error writing to file)

So assuming people test for @error, which ofcourse everybody does, theere should be no issue <_<

If FileWrite($hFile, @CRLF & $a_Array[$x]) = 0 Then

$ErrorSav = 3

Thanks... totally missed it. :">

Share this post


Link to post
Share on other sites

Posted (edited)

I made one minor change to avoid errors in AU3CHECK when using all checks ... (-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6)

What was that (so I know what to watch for)?

<_<

Edited by PsaltyDS

Share this post


Link to post
Share on other sites

Posted

What was that (so I know what to watch for)?

<_<

>Running AU3Check (1.54.10.0)  params:-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6  from:C:\Program Files\AutoIt3
C:\Program Files\AutoIt3\Beta\Include\File.au3(226,23) : WARNING: $hFile already declared/assigned
        Local $hFile = $File
        ~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Beta\Include\File.au3 - 0 error(s), 1 warning(s)

Changed:

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

To

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

Just add this line when doing Ctrl+F5 in SciTE:

#AutoIt3Wrapper_au3check_parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

Jos

Share this post


Link to post
Share on other sites

Posted

*Cough*

; Open output file for overwrite by default, or use input file handle if passed
    Local $hFile = $File
    If IsString($File) Then $hFile = FileOpen($File, 2)

Share this post


Link to post
Share on other sites

Posted

Many roads lead to Rome ..

Share this post


Link to post
Share on other sites

Posted

*Cough*

; Open output file for overwrite by default, or use input file handle if passed
    Local $hFile = $File
    If IsString($File) Then $hFile = FileOpen($File, 2)
You shouldn't be getting coughs with all that fresh orange juice. But yeah, that way is tighter...

<_<

Share this post


Link to post
Share on other sites

Posted

Tested clean with Alt-F5 after downloading 3.2.9.9:

#AutoIt3Wrapper_au3check_parameters = -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <file.au3>

Global $avOne = StringSplit("0,1,2,3,4,5,6,7,8,9,10", ",")
Global $avTwo = StringSplit("Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten", ",")
Global $sTxtFile = @ScriptDir & "\Test.txt"

; Using string file path
_FileWriteFromArray($sTxtFile, $avOne, 2, 10) ; Write "1" thru "9"

; Using file handle
Global $hTxtFile = FileOpen($sTxtFile, 1) ; 1 = append
_FileWriteFromArray($hTxtFile, $avTwo, 2, 10) ; Write "One" thru "Nine"
FileClose($hTxtFile)

; Test new @error = 3
Global $iRet = _FileWriteFromArray($hTxtFile, $avTwo, 2, 10) ; Write "One" thru "Nine"
ConsoleWrite("Debug: $iRet = " & $iRet & "  @error = " & @error & @LF)

; Display results
Run("notepad.exe " & $sTxtFile)

Thanks much!

:P

Share this post


Link to post
Share on other sites

Posted

*Cough*

; Open output file for overwrite by default, or use input file handle if passed
    Local $hFile = $File
    If IsString($File) Then $hFile = FileOpen($File, 2)
Could have swore I put that in my first thread :P

But my ebonics fix seemed to break the code tags ;)

Share this post


Link to post
Share on other sites

Posted

Uhmm...

Since Jon reorg'd this part of the forum, this topic wound up in "AutoIt Feature Requests (Not planned/Rejected)". Since the change was incorporated into Beta 3.2.9.14, and then added to Prod 3.2.10.0, maybe "AutoIt Feature Requests (Complete)" would be a better place for it?

:D

Share this post


Link to post
Share on other sites

Posted

As has been said before, we are not going back through these posts moving them to the "Complete" forum. That's just a waste of time and effort for no gain.

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.