Jump to content
seadoggie01

File Open Handle Mode Check

Recommended Posts

Is there a way to check which mode a file handle was opened with? I want to verify that the handle passed to my function was created with $FO_OVERWRITE so I can edit files correctly.

I'm writing an IniDelete replacement function that I'm trying to check this in...

; #FUNCTION# ====================================================================================================================
; Name ..........: _Ini_Delete
; Description ...: Deletes a section or key-value pair.
; Syntax ........: _Ini_Delete($vFile, $sSection[, $sKey = ""])
; Parameters ....: $vFile               - the full file path and name or a file handle.
;                  $sSection            - the section to use or delete.
;                  $sKey                - [optional] the key to delete. Default is Default.
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |1- Can't read $vFile
;                  |2- $sSection doesn't exist
;                  |3- $sKey doesn't exist
;                  |4- Can't get handle to $vFile
;                  |5- Can't write to $vFile
; Author ........: Seadoggie01
; Modified ......: January 22, 2020
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: No
; ===============================================================================================================================
Func _Ini_Delete($vFile, $sSection, $sKey = Default)
    
    If $sKey = Default Then $sKey = ""
    
    Local $hFile
    ; If we were given a file path + name
    If IsString($vFile) Then
        ; Get a file handle
        $hFile = FileOpen($vFile, $FO_OVERWRITE)
        If $hFile = -1 Then Return SetError(4, 0, False)
    Else
        #ToDo: Check to see if handle is opened in overwrite mode
        $hFile = $vFile
    EndIf
    ; Read the file
    Local $sContents = FileRead($hFile)
        If @error Then Return SetError(1, @error, False)
    ; Get the section's text
    Local $sSectionText = __Ini_SectionText($sContents, $sSection)
        If @error Then Return SetError(2, @extended, False)
    Local $sNewSectionText
    ; If the key is empty
    If $sKey = "" Then
        ; We'll replace the section with nothing
        $sNewSectionText = ""
    Else
        ; Replace the key-value pair(s?)
        $sNewSectionText = StringRegExpReplace($sSectionText, "(?m)^(\Q" & $sKey & "\E=.*)$\s+", "")
            If @error Then Return SetError(3, @error, False)
    EndIf
    
    ; Replace the old section with the new one
    $sContents = StringReplace($sContents, $sSectionText, $sNewSectionText)
    
    ; Write the contents of the file
    If Not FileWrite($hFile, $sContents) Then Return SetError(5, 0, False)
    ; If it's a string Then close the handle
    If IsString($vFile) Then FileClose($hFile)
    
    Return True
EndFunc

I just realized I've left the handle to the file open if it errors... I'll have to fix that, but this should give you an idea of what I'm trying to do.

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

How can you read a file you just overwrited before ?  I don't get it.  Anyway, you could close it and reopen it with the right mode ?  Or if the file has write mode and is empty ?

But really read the file before you overwrite it ;)

Share this post


Link to post
Share on other sites

@SmOke_N wrote a UDF a while back which I use from time to time when trying to read sections larger than 32kb, it also includes _IniDeleteEx function which does something similar, it may give you ideas on how to handle FileOpen.

 

Share this post


Link to post
Share on other sites

Sorry, I think I don't understand FileOpen well enough, sorry. I'm taking another look.


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...