StdUtils plug-in: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
Line 93: Line 93:
== Example ==
== Example ==


<highlight-nsis>!include 'StdUtils.nsh'
<highlight-nsis>!define StdUtils.Time      '!insertmacro _StdUtils_Time'        #time()
!define StdUtils.Rand      '!insertmacro _StdUtils_Rand'        #rand()
!define StdUtils.RandMax    '!insertmacro _StdUtils_RandMax'    #rand() with maximum
!define StdUtils.RandMinMax '!insertmacro _StdUtils_RandMinMax'  #rand() with minimum/maximum
!define StdUtils.RandList  '!insertmacro _StdUtils_RandList'    #rand() with list support
!define StdUtils.FormatStr  '!insertmacro _StdUtils_FormatStr'  #sprintf() with one format tag (only %d supported!)
!define StdUtils.FormatStr2 '!insertmacro _StdUtils_FormatStr2'  #sprintf() with two format tags (only %d supported!)
!define StdUtils.FormatStr3 '!insertmacro _StdUtils_FormatStr3'  #sprintf() with three format tags (only %d supported!)
!define StdUtils.SHFileMove '!insertmacro _StdUtils_SHFileMove'  #SHFileOperation with FO_MOVE
!define StdUtils.SHFileCopy '!insertmacro _StdUtils_SHFileCopy'  #SHFileOperation with FO_COPY
!define StdUtils.Unload    '!insertmacro _StdUtils_Unload'      #Unload DLL for proper clean-up (don't forget!)
!define StdUtils.SetVerbose '!insertmacro _StdUtils_SetVerbose' #Verbose mode (for debugging)


RequestExecutionLevel user
ShowInstDetails show


Section
!macro _StdUtils_Time out
${StdUtils.Time} $1
StdUtils::Time /NOUNLOAD
DetailPrint "Time: $1"
pop ${out}
Sleep 500
!macroend
${StdUtils.Time} $1
DetailPrint "Time: $1"
Sleep 500
${StdUtils.Time} $1
DetailPrint "Time: $1"
SectionEnd


Section
!macro _StdUtils_Rand out
${StdUtils.Rand} $1
StdUtils::Rand /NOUNLOAD
DetailPrint "Random: $1"
pop ${out}
${StdUtils.Rand} $1
!macroend
DetailPrint "Random: $1"
${StdUtils.Rand} $1
DetailPrint "Random: $1"
${StdUtils.Rand} $1
DetailPrint "Random: $1"
${StdUtils.Rand} $1
DetailPrint "Random: $1"
${StdUtils.Rand} $1
DetailPrint "Random: $1"
SectionEnd


Section
!macro _StdUtils_RandMax out max
${StdUtils.RandMax} $1 42
push ${max}
DetailPrint "Random Max: $1"
StdUtils::RandMax /NOUNLOAD
${StdUtils.RandMax} $1 42
pop ${out}
DetailPrint "Random Max: $1"
!macroend
${StdUtils.RandMax} $1 42
DetailPrint "Random Max: $1"
${StdUtils.RandMax} $1 42
DetailPrint "Random Max: $1"
${StdUtils.RandMax} $1 42
DetailPrint "Random Max: $1"
${StdUtils.RandMax} $1 42
DetailPrint "Random Max: $1"
SectionEnd


Section
!macro _StdUtils_RandMinMax out min max
${StdUtils.RandMinMax} $1 -4 -2
push ${min}
DetailPrint "Random Min/Max: $1"
push ${max}
${StdUtils.RandMinMax} $1 -4 -2
StdUtils::RandMinMax /NOUNLOAD
DetailPrint "Random Min/Max: $1"
pop ${out}
${StdUtils.RandMinMax} $1 -4 -2
!macroend
DetailPrint "Random Min/Max: $1"
${StdUtils.RandMinMax} $1 -4 -2
DetailPrint "Random Min/Max: $1"
${StdUtils.RandMinMax} $1 -4 -2
DetailPrint "Random Min/Max: $1"
${StdUtils.RandMinMax} $1 20 21
DetailPrint "Random Min/Max: $1"
SectionEnd


Section
!macro _StdUtils_RandList count max
${StdUtils.FormatStr} $1 "Hello World is %05d woha!" 89
push ${max}
DetailPrint "FormatStr: $1"
push ${count}
${StdUtils.FormatStr2} $1 "Hello World is %05d and %05d woha!" 89 384
StdUtils::RandList /NOUNLOAD
DetailPrint "FormatStr: $1"
!macroend
${StdUtils.FormatStr3} $1 "Hello World is %05d and %05d or even %05d woha!" 89 384 2384
DetailPrint "FormatStr: $1"
${StdUtils.FormatStr} $1 "Hello World is %09000d." 89
DetailPrint "FormatStr: $1"
SectionEnd


Section
!macro _StdUtils_FormatStr out format val
${StdUtils.RandList} 50 100
push '${format}'
Pop $1
push ${val}
StrCmp $1 EOL +3
StdUtils::FormatStr /NOUNLOAD
DetailPrint "RandList: $1"
pop ${out}
Goto -3
!macroend
SectionEnd


Section
!macro _StdUtils_FormatStr2 out format val1 val2
InitPluginsDir
push '${format}'
SetOutPath "$PLUGINSDIR\TestDirA"
push ${val1}
File "${NSISDIR}\Contrib\Graphics\Checks\*.*"
push ${val2}
SetOutPath "$PLUGINSDIR\TestDirA\SubDir"
StdUtils::FormatStr2 /NOUNLOAD
File "${NSISDIR}\Contrib\Graphics\Header\*.*"
pop ${out}
!macroend
${StdUtils.SHFileCopy} $0 "$PLUGINSDIR\TestDirA" "$PLUGINSDIR\SubDirX\TestDirB" $HWNDPARENT
DetailPrint "SHFileCopy: $0"
${StdUtils.SHFileMove} $0 "$PLUGINSDIR\TestDirA" "$PLUGINSDIR\SubDirY\TestDirC" $HWNDPARENT
DetailPrint "SHFileMove: $0"
ExecShell "explore" "$PLUGINSDIR"
SectionEnd


Section
!macro _StdUtils_FormatStr3 out format val1 val2 val3
${StdUtils.Unload}
push '${format}'
SectionEnd</highlight-nsis>
push ${val1}
push ${val2}
push ${val3}
StdUtils::FormatStr3 /NOUNLOAD
pop ${out}
!macroend
 
!macro _StdUtils_SHFileMove out from to hwnd
push '${from}'
push '${to}'
push ${hwnd}
StdUtils::SHFileMove /NOUNLOAD
pop ${out}
!macroend
 
!macro _StdUtils_SHFileCopy out from to hwnd
push '${from}'
push '${to}'
push ${hwnd}
StdUtils::SHFileCopy /NOUNLOAD
pop ${out}
!macroend
 
!macro _StdUtils_Unload
StdUtils::Unload
!macroend
 
!macro _StdUtils_SetVerbose on
!if "${on}" != "0"
StdUtils::EnableVerboseMode /NOUNLOAD
!else
StdUtils::DisableVerboseMode /NOUNLOAD
!endif
!macroend</highlight-nsis>


== Download ==
== Download ==

Revision as of 13:38, 14 August 2011

Author: Instructor (talk, contrib)


This plug-in provides access to a number of "standard" functions from C Standard Library. In order to keep the plug-in size as small as possible, the Visual C++ Run-Time v6.0 (MSVCRT.DLL), which is included with all versions of Windows since Windows 2000, is used.

Available Functions

!define StdUtils.Time       '!insertmacro _StdUtils_Time'        #time()
!define StdUtils.Rand       '!insertmacro _StdUtils_Rand'        #rand()
!define StdUtils.RandMax    '!insertmacro _StdUtils_RandMax'     #rand() with maximum
!define StdUtils.RandMinMax '!insertmacro _StdUtils_RandMinMax'  #rand() with minimum/maximum
!define StdUtils.RandList   '!insertmacro _StdUtils_RandList'    #rand() with list support
!define StdUtils.FormatStr  '!insertmacro _StdUtils_FormatStr'   #sprintf() with one format tag
!define StdUtils.FormatStr2 '!insertmacro _StdUtils_FormatStr2'  #sprintf() with two format tags
!define StdUtils.FormatStr3 '!insertmacro _StdUtils_FormatStr3'  #sprintf() with three format tags
!define StdUtils.SHFileMove '!insertmacro _StdUtils_SHFileMove'  #SHFileOperation with FO_MOVE
!define StdUtils.SHFileCopy '!insertmacro _StdUtils_SHFileCopy'  #SHFileOperation with FO_COPY
!define StdUtils.Unload     '!insertmacro _StdUtils_Unload'      #Unload DLL for proper clean-up
 
 
!macro _StdUtils_Time out
	StdUtils::Time /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_Rand out
	StdUtils::Rand /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_RandMax out max
	push ${max}
	StdUtils::RandMax /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_RandMinMax out min max
	push ${min}
	push ${max}
	StdUtils::RandMax /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_RandList count max
	push ${max}
	push ${count}
	StdUtils::RandList /NOUNLOAD
!macroend
 
!macro _StdUtils_FormatStr out format val
	push '${format}'
	push ${val}
	StdUtils::FormatStr /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_FormatStr2 out format val1 val2
	push '${format}'
	push ${val1}
	push ${val2}
	StdUtils::FormatStr2 /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_FormatStr3 out format val1 val2 val3
	push '${format}'
	push ${val1}
	push ${val2}
	push ${val3}
	StdUtils::FormatStr3 /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_SHFileMove out from to hwnd
	push '${from}'
	push '${to}'
	push ${hwnd}
	StdUtils::SHFileMove /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_SHFileCopy out from to hwnd
	push '${from}'
	push '${to}'
	push ${hwnd}
	StdUtils::SHFileCopy /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_Unload
	StdUtils::Unload
!macroend

Example

!define StdUtils.Time       '!insertmacro _StdUtils_Time'        #time()
!define StdUtils.Rand       '!insertmacro _StdUtils_Rand'        #rand()
!define StdUtils.RandMax    '!insertmacro _StdUtils_RandMax'     #rand() with maximum
!define StdUtils.RandMinMax '!insertmacro _StdUtils_RandMinMax'  #rand() with minimum/maximum
!define StdUtils.RandList   '!insertmacro _StdUtils_RandList'    #rand() with list support
!define StdUtils.FormatStr  '!insertmacro _StdUtils_FormatStr'   #sprintf() with one format tag (only %d supported!)
!define StdUtils.FormatStr2 '!insertmacro _StdUtils_FormatStr2'  #sprintf() with two format tags (only %d supported!)
!define StdUtils.FormatStr3 '!insertmacro _StdUtils_FormatStr3'  #sprintf() with three format tags (only %d supported!)
!define StdUtils.SHFileMove '!insertmacro _StdUtils_SHFileMove'  #SHFileOperation with FO_MOVE
!define StdUtils.SHFileCopy '!insertmacro _StdUtils_SHFileCopy'  #SHFileOperation with FO_COPY
!define StdUtils.Unload     '!insertmacro _StdUtils_Unload'      #Unload DLL for proper clean-up (don't forget!)
!define StdUtils.SetVerbose '!insertmacro _StdUtils_SetVerbose'  #Verbose mode (for debugging)
 
 
!macro _StdUtils_Time out
	StdUtils::Time /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_Rand out
	StdUtils::Rand /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_RandMax out max
	push ${max}
	StdUtils::RandMax /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_RandMinMax out min max
	push ${min}
	push ${max}
	StdUtils::RandMinMax /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_RandList count max
	push ${max}
	push ${count}
	StdUtils::RandList /NOUNLOAD
!macroend
 
!macro _StdUtils_FormatStr out format val
	push '${format}'
	push ${val}
	StdUtils::FormatStr /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_FormatStr2 out format val1 val2
	push '${format}'
	push ${val1}
	push ${val2}
	StdUtils::FormatStr2 /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_FormatStr3 out format val1 val2 val3
	push '${format}'
	push ${val1}
	push ${val2}
	push ${val3}
	StdUtils::FormatStr3 /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_SHFileMove out from to hwnd
	push '${from}'
	push '${to}'
	push ${hwnd}
	StdUtils::SHFileMove /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_SHFileCopy out from to hwnd
	push '${from}'
	push '${to}'
	push ${hwnd}
	StdUtils::SHFileCopy /NOUNLOAD
	pop ${out}
!macroend
 
!macro _StdUtils_Unload
	StdUtils::Unload
!macroend
 
!macro _StdUtils_SetVerbose on
	!if "${on}" != "0"
		StdUtils::EnableVerboseMode /NOUNLOAD
	!else
		StdUtils::DisableVerboseMode /NOUNLOAD
	!endif
!macroend

Download

StdUtils.2011-08-14.zip (86 KB)