Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Update _FileWriteFromArray() to accept a handle


  • Please log in to reply
15 replies to this topic

#1 PsaltyDS

PsaltyDS

    Most Venerable Penguin

  • MVPs
  • 13,279 posts

Posted 07 November 2007 - 05:17 PM

Come up with in the following topic: Array

This is the demo:
AutoIt         
#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.

<_<
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law







#2 Jos

Jos

    ...

  • Developers
  • 22,856 posts

Posted 07 November 2007 - 06:16 PM

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 <_<

Visit the SciTE4AutoIt3 Download page for the latest versions                                                                 Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)


#3 PsaltyDS

PsaltyDS

    Most Venerable Penguin

  • MVPs
  • 13,279 posts

Posted 08 November 2007 - 04:33 PM

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:
AutoIt         
$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


:)

Attached Files


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

#4 Jos

Jos

    ...

  • Developers
  • 22,856 posts

Posted 08 November 2007 - 04:55 PM

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

Visit the SciTE4AutoIt3 Download page for the latest versions                                                                 Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)


#5 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 15,730 posts

Posted 08 November 2007 - 05:25 PM

This...
    If IsString($File) Then         Local $hFile = FileOpen($File, 2)     Else         Local $hFile = $File     EndIfƒo݊÷ Ø*.•ÖÞ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, 08 November 2007 - 05:26 PM.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.


#6 Jos

Jos

    ...

  • Developers
  • 22,856 posts

Posted 08 November 2007 - 05:36 PM

This...

    If IsString($File) Then         Local $hFile = FileOpen($File, 2)     Else         Local $hFile = $File     EndIfƒo݊÷ Ø*.•ÖÞ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, 08 November 2007 - 05:45 PM.

Visit the SciTE4AutoIt3 Download page for the latest versions                                                                 Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)


#7 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 15,730 posts

Posted 08 November 2007 - 05:42 PM

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. :">

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.


#8 PsaltyDS

PsaltyDS

    Most Venerable Penguin

  • MVPs
  • 13,279 posts

Posted 08 November 2007 - 07:09 PM

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, 08 November 2007 - 07:10 PM.

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

#9 Jos

Jos

    ...

  • Developers
  • 22,856 posts

Posted 08 November 2007 - 07:14 PM

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

Visit the SciTE4AutoIt3 Download page for the latest versions                                                                 Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)


#10 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 08 November 2007 - 07:25 PM

*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)


#11 Jos

Jos

    ...

  • Developers
  • 22,856 posts

Posted 08 November 2007 - 07:34 PM

Many roads lead to Rome ..

Visit the SciTE4AutoIt3 Download page for the latest versions                                                                 Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)


#12 PsaltyDS

PsaltyDS

    Most Venerable Penguin

  • MVPs
  • 13,279 posts

Posted 08 November 2007 - 08:20 PM

*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...

<_<
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

#13 PsaltyDS

PsaltyDS

    Most Venerable Penguin

  • MVPs
  • 13,279 posts

Posted 13 November 2007 - 03:16 PM

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
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

#14 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 15,730 posts

Posted 13 November 2007 - 03:22 PM

*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 ;)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.


#15 PsaltyDS

PsaltyDS

    Most Venerable Penguin

  • MVPs
  • 13,279 posts

Posted 17 January 2008 - 09:38 PM

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
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

#16 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 18 January 2008 - 12:54 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users