cs_AlexN
Messages postés
694
Date d'inscription
lundi 5 décembre 2005
Statut
Membre
Dernière intervention
8 janvier 2014
19
14 juil. 2006 à 02:04
Si cela peut t'être utile, voilà quelques fonctions qui montrent les possibilités d'interface avec le système de fichiers en appelant les fonctions de l'API Win32 depuis nsis et des fonctions de manipulation de chaines de caractères :
Certaines fonctions utilisent la LogicLib, pour simplifier le code. Il faudra insérer un :
!include LogicLib.nsh ; Logic structures and functions
;************************
; File System functions *
;************************
Function FileRequest
;Here is a function to open up the standard Windows File Save/Open dialog.
;This can be called from a Section or Function "forcing" the user to Save/Open a file before the installation continues.
;
;Usage :
;
;Push "0xA01800" ;flags (see below) separated by commas [,]
;Push "open" ;use "open" or "save"
;Push "Zip Files (*.zip)|*.zip" ;file type 1
;Push "All Files (*.*)|*.*" ;file type 2
;Push "Select Zip file to install" ;dialog title
;Call FileRequest
;Pop $R0
;
;$R0 = Selected file or "error" for cancelled
;
;Flags :
;
; * OFN_READONLY 0x00000001
; * OFN_OVERWRITEPROMPT 0x00000002
; * OFN_HIDEREADONLY 0x00000004
; * OFN_NOCHANGEDIR 0x00000008
; * OFN_SHOWHELP 0x00000010
; * OFN_ENABLEHOOK 0x00000020
; * OFN_ENABLETEMPLATE 0x00000040
; * OFN_ENABLETEMPLATEHANDLE 0x00000080
;
; * OFN_NOVALIDATE 0x00000100
; * OFN_ALLOWMULTISELECT 0x00000200
; * OFN_EXTENSIONDIFFERENT 0x00000400
; * OFN_PATHMUSTEXIST 0x00000800
; * OFN_FILEMUSTEXIST 0x00001000
; * OFN_CREATEPROMPT 0x00002000
; * OFN_SHAREAWARE 0x00004000
; * OFN_NOREADONLYRETURN 0x00008000
;
; * OFN_NOTESTFILECREATE 0x00010000
; * OFN_NONETWORKBUTTON 0x00020000
; * OFN_NOLONGNAMES 0x00040000 ; force no long names for 4.x modules
; * OFN_EXPLORER 0x00080000 ; new look commdlg
; * OFN_NODEREFERENCELINKS 0x00100000
; * OFN_LONGNAMES 0x00200000 ; force long names for 3.x modules
; * OFN_ENABLEINCLUDENOTIFY 0x00400000 ; send include message to callback
; * OFN_ENABLESIZING 0x00800000
;
; * OFN_DONTADDTORECENT 0x02000000
; * OFN_FORCESHOWHIDDEN 0x10000000 ; Show All files including System and hidden files
;
;Thanks brainsucker and Kichik for help.
;
;-Stu
;
; gaijin : some code fixing
;
Exch $0 ;title
Exch
Exch $1 ;sort type 1
Exch 2
Exch $2 ;sort type 2
Exch 2
Exch 3
Exch $R4 ;save/open
Exch 3
Exch 4
Exch $R5 ;flag
Exch 4 ; At this point, stack status is from top : $1 $0 $2 $R4 $R5
Exch ; Fix : At this point, stack status is from top : $0 $1 $2 $R4 $R5
Exch 4 ; Fix : At this point, stack status is from top : $R5 $0 $2 $R4 $0
Push $3
Push $4
Push $5 ;type 1 / 1
Push $6 ;tupe 1 / 2
Push $7 ;type 2 / 1
Push $8 ;type 2 / 2
Push $9
Push $R0 ;len 1 / 1
Push $R1 ;len 1 / 2
Push $R2 ;len 2 / 1
Push $R3 ;len 2 / 2
StrCmp $R5 "" 0 +2
StrCpy $R5 0xA01800
StrCpy $9 $0 ;title
StrCpy $3 0
loop1:
IntOp $3 $3 - 1
StrCpy $4 $2 1 $3
StrCmp $4 "" error
StrCmp $4 "|" 0 loop1
StrCpy $5 $2 $3
IntOp $3 $3 + 1
StrCpy $6 $2 "" $3
StrCpy $3 0
loop2:
IntOp $3 $3 - 1
StrCpy $4 $1 1 $3
StrCmp $4 "" error
StrCmp $4 "|" 0 loop2
StrCpy $7 $1 $3
IntOp $3 $3 + 1
StrCpy $8 $1 "" $3
StrLen $R0 $5
IntOp $R0 $R0 + 1
StrLen $R1 $6
IntOp $R1 $R1 + 1
StrLen $R2 $7
IntOp $R2 $R2 + 1
StrLen $R3 $8
IntOp $R3 $R3 + 1
StrCpy $4 '(&l4, i, i 0, i, i 0, i 0, i 0, t, i ${NSIS_MAX_STRLEN}, t, i ${NSIS_MAX_STRLEN}, t, t, i, &i2, &i2, t, i 0, i 0, i 0) i'
System::Call '*(&t$R0 "$5" , &t$R1 "$6", &t$R2 "$7", &t$R3 "$8", &i1 0) i.r0'
System::Call '*$4(, $HWNDPARENT,, r0,,,,"",,"",, i 0, "$9", $R5,,,,,,) .r1'
System::Call 'comdlg32::Get$R4FileNameA(i r1) i .r2'
System::Call '*$1$4(,,,,,,,.r3)'
System::Free $1
System::Free $0
StrCmp $2 0 0 +2
error:
StrCpy $3 error
StrCpy $0 $3
Pop $R3
Pop $R2
Pop $R1
Pop $R0
Pop $9
Pop $8
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $R5 ; Fix : Retrieve appropriate register value in the messed stack
Pop $1 ; Fix : Retrieve appropriate register value in the messed stack
Pop $2
Pop $R4 ; Fix : Retrieve appropriate register value in the messed stack
Exch $0
FunctionEnd
Function GetFileSize
;Usage :
;Push "${NSISDIR}\makensis.exe" ; Input full path of the file
;Call "FileSize" ; Call the function
;Pop $0 = filesize in Kb
;
ClearErrors
Exch $R0 ;Grab the Inputed path file
Push $0
Push $1
Push $2
${If} ${FileExists} $R0
System::Call 'kernel32::_lopen(t "$R0", i 0) i .r0' ; Call the API to open the file
System::Call 'kernel32::GetFileSize(i $0, i) i .r1' ; Call API to read file size
System::Call 'kernel32::_lclose(i $0) i' ; API to close the opened file
StrCpy $R0 $1
${Else}
SetErrors
StrCpy $R0 -1
${EndIf}
Pop $2
Pop $1
Pop $0
Exch $R0
FunctionEnd
Function GetParentPath
; GetParentPath
; In : Top of stack = Path (C:\Program Files\Dummy)
; Out : Top of stack = Parent Path (C:\Program Files)
Exch $R0
Push $R1
Push $R2
Push $R3
StrCpy $R1 0
StrLen $R2 $R0
loop:
IntOp $R1 $R1 + 1
IntCmp $R1 $R2 get 0 get
StrCpy $R3 $R0 1 -$R1
StrCmp $R3 "" get
Goto loop
get:
StrCpy $R0 $R0 -$R1
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Function GetFilenameWithoutExtension
; In : Top of stack = Absolute Filename (C:\Program Files\Dummy.ext)
; Out : Top of stack = Short Filename (Dummy)
Exch $R0
Push $R1
Push $R2
Push $R3
Push $R4
Push $R5
StrCpy $R1 0
StrLen $R2 $R0
StrCpy $R5 "."
loop:
IntOp $R1 $R1 + 1
IntCmp $R1 $R2 get 0 get
IntOp $R4 $R2 - $R1
StrCpy $R3 $R0 1 $R4
StrCmp $R3 "" get
${If} $R3 == "."
${AndIf} $R5 == "."
StrCpy $R5 $R4
${EndIf}
Goto loop
get:
IntOp $R4 $R4 + 1
StrCpy $R0 $R0 $R1 $R4
IntOp $R5 $R2 - $R5
StrCpy $R0 $R0 -$R5
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Function GetFilePath
;
;Section
; $R0 = Result
; $R1 = full filename
; ${GetFilePath} $R0 $R1
; $R0="C:\ftp"
;SectionEnd
;
!define GetFilePath `!insertmacro GetFilePathCall`
!macro GetFilePathCall _RESULT _FILENAME
Push ${_FILENAME}
Call GetFilePath
Pop ${_RESULT}
!macroend
Exch $0
Push $1
Push $2
ClearErrors
${If} ${FileExists} $0
System::Call 'kernel32::GetLongPathNameA(t r0, t .r1, i 1024)i .r2'
StrCmp $2 error GetFilePathError
StrCpy $0 $1
${Else}
GetFilePathError:
SetErrors
StrCpy $0 -1
${EndIf}
Pop $2
Pop $1
Exch $0
FunctionEnd
;*******************
; String functions *
;*******************
Function StrStr
; StrStr
; entrée, haut de la pile = chaine a rechercher
; haut de la pile-1 = chaine dans laquelle chercher
; sortie, haut de la pile (remplacement avec la portion de la chaine restante)
; ne modifie aucune autre variable.
;
; Usage:
; Push "c'est une putain de longue chaine" ; Original string
; Push "putain" ; pattern to search for
; Call StrStr
; Pop $R0
; ($R0 at this point is "putain de longue chaine")
Exch $R1 ; st=tete,vieux$R1, $R1=pointeur
Exch ; st=vieux$R1,tete
Exch $R2 ; st=vieux$R1,vieux$R2, $R2=tete
Push $R3
Push $R4
Push $R5
StrLen $R3 $R1
StrCpy $R4 0
$R1=pointeur
$R2=tete
$R3=taille(pointeur)
$R4=cnt
$R5=tmp
loop:
StrCpy $R5 $R2 $R3 $R4
StrCmp $R5 $R1 done
StrCmp $R5 "" done
IntOp $R4 $R4 + 1
Goto loop
done:
StrCpy $R1 $R2 "" $R4
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Exch $R1
FunctionEnd
Function AddStringToString
; Add a new string to an initial one separated by newline if initial string was not empty
; In : Top of stack = initial string
; 2nd of stack = string to add
; Out : Top of stack = new string
; Save registers
Exch $0 ; Get initial string
Exch
Exch $1 ; Get string to add
${If} $1 != ""
${If} $0 == ""
StrCpy $0 $1
${Else}
StrCpy $0 "$0$\n$1."
${EndIf}
${EndIf}
; Restore registers and stack result
Pop $1
Exch $0
FunctionEnd