MightyMacro

From NSIS Wiki
Jump to navigationJump to search
Author: Zinthose (talk, contrib)


Summary

Pure Pre-compiler macro that enhances macros.

See forum post: [Macro overloading]

Usage

Command List

TODO

Example

!include MightyMacro.nsh
 
OutFile MightyMacroDemo.exe
 
## User Defined Macro Example
	!macro _HelloWorld _Values
		!define _Parameters "_Index|_Param1|_Param2|_Param3"
		${MacroParams} ${_Parameters} `${_Values}`
 
		DetailPrint "###### Param Test ${_Index} ######"
		DetailPrint "  _Values = ${_Values}"
		DetailPrint "  _Param1 = ${_Param1}"
		DetailPrint "  _Param2 = ${_Param2}"
		DetailPrint "  _Param3 = ${_Param3}"
		DetailPrint ""
 
		${MacroClean} ${_Parameters}
		!undef _Parameters
	!macroend
	!define HelloWorld `!insertmacro _HelloWorld`
 
## Execution Example
	Section Example
 
		${HelloWorld} "1|Hello|There|World"
		${HelloWorld} "2|Hello||World"
		${HelloWorld} "3|Hello|World"
		${HelloWorld} "4|Hello World"
		${HelloWorld} "5|||Hello World"
 
	SectionEnd

Example Output

###### Param Test 1 ######
  _Values = 1|Hello|There|World
  _Param1 = Hello
  _Param2 = There
  _Param3 = World

###### Param Test 2 ######
  _Values = 2|Hello||World
  _Param1 = Hello
  _Param2 = 
  _Param3 = World

###### Param Test 3 ######
  _Values = 3|Hello|World
  _Param1 = Hello
  _Param2 = World
  _Param3 = 

###### Param Test 4 ######
  _Values = 4|Hello World
  _Param1 = Hello World
  _Param2 = 
  _Param3 = 

###### Param Test 5 ######
  _Values = 5|||Hello World
  _Param1 = 
  _Param2 = 
  _Param3 = Hello World

Completed

Macro Source

!ifndef __MightyMacro__
!define __MightyMacro__
 
!ifndef MightyMacroDelimiter
	!define MightyMacroDelimiter "|"
!endif
 
## Macro Parameter Helper
    !define MacroParams "!insertmacro _MacroParams"
    !macro _MacroParams _ParamNames _Values
        !searchreplace _TMP `${_ParamNames}` "${MightyMacroDelimiter}" " '${MightyMacroDelimiter}' "
        !searchparse /noerrors `${_Values}` '' ${_TMP}
        !undef _TMP
    !macroend
 
## Macro used to undefine trailing parameter names within the _MacroClean Macro
	!macro _MacroClean_
		!if `${_ParamNames}` != ""
			!searchparse /ignorecase /noerrors `${_ParamNames}` '' _TMP "${MightyMacroDelimiter}" _ParamNames
			!ifdef "${_TMP}"
				!undef ${_TMP}
				!undef _TMP
			!endif
		!endif
	!macroend
 
## MacroClean - Use to cleanup the defined parameters temporarily created 
    !define MacroClean "!insertmacro _MacroClean"
    !macro _MacroClean _ParamNames
		## Macro used to undefine trailing parameter names
			!define _MacroClean_ "!insertmacro _MacroClean_"
 
		## Undefine Parameter names  [20 parameters "Should" be adiquate
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
			${_MacroClean_}
 
 
 
		## One last check, then issue a warning if more paramters are found.
			!if `${_ParamNames}` != ""
				!warning "Parrent macro has more than 20 parameters! This may cause define errors!"
			!endif
 
		## Final Cleanup
			!undef _MacroClean_
			!undef _ParamNames
 
    !macroend
!endif