;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       OpenFile() 
;; 
;;ACTION         Opens file based on specified parameters. Supports overwriting a file. 
;; 
;;AUTHOR         Howard A. Bullock 
;; 
;;VERSION        1.0 -  
;; 
;;SYNTAX         OpenFile(FileName, Mode, Create, Overwrite) 
;; 
;;PARAMETERS      FileName - Required - String value 
;;                Mode - Required - String value 
;;               - "R" or "W" 
;;               Create - Optional - Integer 
;;               - Use 1 to create, 0 to return "file not found" (default) 
;;               OverWrite - Optional - Integer 
;;               - Use 1 to overwrite or 0 to Append (default) 
;; 
;; 
;;REMARKS        This function attempts to open the specified file. @error is set on 
;;               exit. The UDF will search for the first available file number. 
;; 
;;RETURNS        File handle (integer 1-10) if successful; nothing if error 
;; 
;;DEPENDENCIES   fMsg() 
;; 
;;EXAMPLES 
;;               Open file for write; do not create the file; append 
;;               $FH = OpenFile("junk.txt", "W") 
;;               If @error = 0 
;;                  Writeline($FH, "Some text") 
;;               EndIf 
;; 
;;               Open file for write; create the file; delete old file 
;;               $FH2 = OpenFile("junk2.txt", "W", 1, 1) 
;;               If @error = 0 
;;                  Writeline($FH2, "Some text") 
;;               EndIf; 
; 
Function OpenFile($_FileName, $ReadWrite, optional $_Create, optional $_Overwrite)
 
  Dim $FH, $RC, $Mode
  Dim $_Msg
 
  ; Validate input parameters 
  If VarType($Create) = 0
    $Create = 0
  EndIf
  If VarType($Overwrite) = 0
    $Overwrite = 0
  EndIf
 
  If not (VarType($FileName) = 8 And
   ($ReadWrite = 'R' or $ReadWrite = 'W') And
   ($Create = 0 or $Create = 1) And
   ($Overwrite = 0 or $Overwrite = 1) )
    fMsg('OpenFile: Error in parameters (' + $FileName + ', ' + $ReadWrite + ', ' + $Create + ', ' + $OverWrite + ')')
    Exit 1
  EndIf
 
  If $Create < $OverWrite
    fMsg('OpenFile: Error in parameters (You must specify Create=1 when OverWrite=1)')
    Exit 1
  EndIf
 
  ; Build Mode value 
  If $Create = 0
    $Mode = 0
  Else
    $Mode = 1
  EndIf
  If $ReadWrite = 'R'
    $Mode = $Mode + 2
  Else
    $Mode = $Mode + 4
  EndIf
 
  ;Handle OverWrite option 
  If $OverWrite = 1
    If Exist($Filename)
      Del $Filename
    EndIf
  EndIf
 
  ; Open file 
  $FH = FreeFileHandle()
  $RC = Open ($FH, $FileName, $Mode)
 
  If $RC <> 0
    fMsg(@ERROR + ': ' + @SERROR)
 
    Select
    Case $RC=2
     fMsg('open(' + $FH + ', ' + $FileName + ', ' + $Mode + ') @CRLFError: ' + $RC + '(Cannot find the file specified)')
      Exit $RC
 
    Case $RC=-3
      While $RC = -3 and $FH < 11
        $RC=Open ($FH, $FileName, $Mode)
        If $RC = -3
          $FH=$FH +1
        EndIf
      Loop
      If $RC = -3
        fMsg('open(' + $FH + ', ' + $FileName + ', ' + $Mode + ') @CRLFError: ' + $RC + '(All file numbers are in use.)')
        Exit $RC
      EndIf
 
    Case $RC=-2
      fMsg('open(' + $FH + ', ' + $FileName + ', ' + $Mode + ') @CRLFError: ' + $RC + '(Invalid file number specified)')
      Exit $RC
 
    Case $RC=-1
      Msg('open(' + $FH + ', ' + $FileName + ', ' + $Mode + ') @CRLFError: ' + $RC + '(File number already in use)')
      Exit $RC
 
    Case 1
      fMsg('open(' + $FH + ', ' + $FileName + ', ' + $Mode + ') @CRLFError: ' + $RC + '(System error)')
      Exit $RC
 
    Endselect
 
  EndIf
 
  $OpenFile = $FH
 
  Exit 0
Endfunction