FileOpen2: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
 
(3 intermediate revisions by one other user not shown)
Line 2: Line 2:


== Description ==
== Description ==
Open files with control over <b><i>access</i></b>, <b><i>share</i></b> and <b><i>create</i></b> modes with <b><i>intuitive</i></b> parameters.
Open files with control over <b><i>access</i></b>, <b><i>share</i></b> and <b><i>create</i></b> modes with intuitive parameters.


== Example ==
== Example ==
Line 10: Line 10:
== Function ==
== Function ==
<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Easily open files with more options than FileOpen
;; Easily open files with more options than FileOpen. Use lowercase modes
;;  P1 :o: Handle returned
;;  P1 :out: Handle
;;  P2 :i: File name
;;  P2 :in: File name
;;  P3 :i: Access Mode
;;  P3 :in: Access mode (default=r)
;;         'r' : Read
;;           'r' : Read
;;         'w' : Read+Write
;;           'w' : Read+Write
;;  P4 :i: Share mode
;;  P4 :in: Share mode (default=w)
;;         ''   : None              (no access)
;;           'l' : None              (full lock)
;;         'r'   : Read              (no delete, no write)
;;           'r' : Read              (no delete, no write)
;;         'w'   : Read+Write        (no delete)
;;           'w' : Read+Write        (no delete)
;;         'a'   : Read+Write+Delete (full share)
;;           'a' : Read+Write+Delete (full share)
;;  P5 :i: Create mode
;;  P5 :in: Create mode (default=o)
;;         'o' : Open existing only
;;           'o' : Open existing only
;;         'c' : Create if not exist
;;           'c' : Create if not exist
;;         'v' : Create and Overwrite
;;           'v' : Create and Overwrite
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
!define FileOpen2 "!insertmacro _FileOpen2"
!define FileOpen2 "!insertmacro _FileOpen2"
Line 45: Line 45:
   Exch $3  ;; Create
   Exch $3  ;; Create


   StrCmp "w" $1 0 +3
   StrCmpS "w" $1 0 +3
       StrCpy $1 0xC0000000  ;; GENERIC_READ | GENERIC_WRITE
       StrCpy $1 0xC0000000  ;; GENERIC_READ | GENERIC_WRITE
       Goto +2
       Goto +2
Line 51: Line 51:
       StrCpy $1 0x80000000  ;; GENERIC_READ
       StrCpy $1 0x80000000  ;; GENERIC_READ


   StrCmp "" $2 0 +3
   StrCmpS "l" $2 0 +3
       StrCpy $2 0          ;; FILE_SHARE_NONE
       StrCpy $2 0          ;; FILE_SHARE_NONE
       Goto +8
       Goto +8
   StrCmp "r" $2 0 +3
   StrCmpS "r" $2 0 +3
       StrCpy $2 1          ;; FILE_SHARE_READ
       StrCpy $2 1          ;; FILE_SHARE_READ
       Goto +5
       Goto +5
   StrCmp "w" $2 0 +3
   StrCmpS "a" $2 0 +3
      StrCpy $2 7          ;; FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE
;- else: "w", "", or anything else
       StrCpy $2 3          ;; FILE_SHARE_READ | FILE_SHARE_WRITE
       StrCpy $2 3          ;; FILE_SHARE_READ | FILE_SHARE_WRITE
       Goto +2
       Goto +2
;- else: "rwd", "a", or anything else
      StrCpy $2 7          ;; FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE


   StrCmp "v" $3 0 +3
   StrCmpS "v" $3 0 +3
       StrCpy $3 2          ;; CREATE_ALWAYS
       StrCpy $3 2          ;; CREATE_ALWAYS
       Goto +5
       Goto +5
   StrCmp "c" $3 0 +3
   StrCmpS "c" $3 0 +3
       StrCpy $3 4          ;; OPEN_ALWAYS
       StrCpy $3 4          ;; OPEN_ALWAYS
       Goto +2
       Goto +2

Latest revision as of 14:56, 3 December 2011

Author: Lloigor (talk, contrib)


Description

Open files with control over access, share and create modes with intuitive parameters.

Example

;; Open file with RW access, ReadOnly sharing, and if it does not exists, Creates it. $1 gets the file handle.
${FileOpen2} $1 "$SomeFile" "w" "r" "c"

Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Easily open files with more options than FileOpen. Use lowercase modes
;;   P1 :out: Handle
;;   P2 :in:  File name
;;   P3 :in:  Access mode (default=r)
;;            'r' : Read
;;            'w' : Read+Write
;;   P4 :in:  Share mode (default=w)
;;            'l' : None              (full lock)
;;            'r' : Read              (no delete, no write)
;;            'w' : Read+Write        (no delete)
;;            'a' : Read+Write+Delete (full share)
;;   P5 :in:  Create mode (default=o)
;;            'o' : Open existing only
;;            'c' : Create if not exist
;;            'v' : Create and Overwrite
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
!define FileOpen2 "!insertmacro _FileOpen2"
!macro _FileOpen2 _Handle_ _File_ _Access_ _Share_ _Create_
   Push "${_Create_}"
   Push "${_Share_}"
   Push "${_Access_}"
   Push "${_File_}"
   Call FileOpen2
   Pop ${_Handle_}
!macroend
 
Function FileOpen2  ;; File, Access, Share, Create
   Exch $0  ;; File
   Exch
   Exch $1  ;; Access
   Exch 2
   Exch $2  ;; Share / Return value
   Exch 3
   Exch $3  ;; Create
 
   StrCmpS "w" $1 0 +3
      StrCpy $1 0xC0000000  ;; GENERIC_READ | GENERIC_WRITE
      Goto +2
;- else: "r", "", or anything else
      StrCpy $1 0x80000000  ;; GENERIC_READ
 
   StrCmpS "l" $2 0 +3
      StrCpy $2 0           ;; FILE_SHARE_NONE
      Goto +8
   StrCmpS "r" $2 0 +3
      StrCpy $2 1           ;; FILE_SHARE_READ
      Goto +5
   StrCmpS "a" $2 0 +3
      StrCpy $2 7           ;; FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE
;- else: "w", "", or anything else
      StrCpy $2 3           ;; FILE_SHARE_READ | FILE_SHARE_WRITE
      Goto +2
 
   StrCmpS "v" $3 0 +3
      StrCpy $3 2           ;; CREATE_ALWAYS
      Goto +5
   StrCmpS "c" $3 0 +3
      StrCpy $3 4           ;; OPEN_ALWAYS
      Goto +2
;- else: "o", "", or anything else
      StrCpy $3 3           ;; OPEN_EXISTING
 
   System::Call 'Kernel32::CreateFile(t, i, i, i, i, i, i) i (r0, r1, r2, 0, r3, 0x80, 0) .r2'  ;; Open/Create file
 
   Pop $3
   Pop $0
   Pop $1
   Exch $2  ;; Return Handle
FunctionEnd