FileOpen2: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Easily opens files with more options than FileOpen)
 
mNo edit summary
Line 9: Line 9:
== Function ==
== Function ==
<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Easily opens files with more options than FileOpen
;; Easily open files with more options than FileOpen
;;  P1 :o: Handle returned
;;  P1 :o: Handle returned
;;  P2 :i: File name
;;  P2 :i: File name

Revision as of 02:46, 22 October 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.
${FileOpenEx} $1 "$SomeFile" "rw" "r" "c"

Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Easily open files with more options than FileOpen
;;   P1 :o: Handle returned
;;   P2 :i: File name
;;   P3 :i: Access Mode
;;         'r'  : Readonly
;;         'w'  : Writeonly
;;         'rw' : Read+Write
;;   P4 :i: Share mode
;;         ''    : None              (no access)
;;         'r'   : Readonly          (no delete, no write)
;;         'rw'  : Read+Write        (no delete)
;;         'rwd' : Read+Write+Delete (full share)
;;   P5 :i: Create mode
;;         ''  : Open existing only
;;         'c' : Create if not exist
;;         'o' : Create and Overwrite
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
!define FileOpenEx "!insertmacro _FileOpenEx"
!macro _FileOpenEx _Handle_ _File_ _Access_ _Share_ _Create_
   Push "${_Create_}"
   Push "${_Share_}"
   Push "${_Access_}"
   Push "${_File_}"
   Call FileOpenEx
   Pop ${_Handle_}
!macroend
 
Function FileOpenEx  ;; $0:File, $1:Access, $2:Share, $3:Create
   Exch $0
   Exch
   Exch $1
   Exch 2
   Exch $2
   Exch 3
   Exch $3
 
   StrCmp "r" $1 0 +3
      StrCpy $1 0x80000000  ;; GENERIC_READ
      Goto +6
   StrCmp "w" $1 0 +3
      StrCpy $1 0x40000000  ;; GENERIC_WRITE
      Goto +3
   StrCmp "rw" $1 0 +3
      StrCpy $1 0xC0000000  ;; GENERIC_READ | GENERIC_WRITE
 
   StrCmp "" $2 0 +3
      StrCpy $2 0           ;; FILE_SHARE_NONE
      Goto +9
   StrCmp "r" $2 0 +3
      StrCpy $2 1           ;; FILE_SHARE_READ
      Goto +6
   StrCmp "rw" $2 0 +3
      StrCpy $2 3           ;; FILE_SHARE_READ | FILE_SHARE_WRITE
      Goto +3
   StrCmp "rwd" $2 0 +3
      StrCpy $2 7           ;; FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE
 
   StrCmp "" $3 0 +3
      StrCpy $3 3           ;; OPEN_EXISTING
      Goto +6
   StrCmp "c" $3 0 +3
      StrCpy $3 4           ;; OPEN_ALWAYS
      Goto +3
   StrCmp "o" $3 0 +3
      StrCpy $3 2           ;; CREATE_ALWAYS
 
   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
FunctionEnd