Old 14th January 2011, 13:56   #1
isawen
Junior Member
 
Join Date: Jan 2010
Posts: 39
Question macro with parameters

Hi guys,

I would really appreciate your help over this issue I have.

I have a macro with 2 parameters like below:

PHP Code:
!macro TestMacro _PARAM1 _PARAM2
Push 
$1
StrCpy 
$1 $OUTDIR
// ... do some stuff here with the ${_PARAM1} and ${_PARAM2}
SetOutPath $1
Pop 
$1
!macroend 
The problem is that when I call it like this:
PHP Code:
${TestMacro} $$
inside the macro the ${_PARAM1} gets set to the $OUTDIR.

Is there a way to avoid this kind bad macro usage, beside:

PHP Code:
!macro TestMacro _PARAM1 _PARAM2
!define LastKnownOutDir $OUTDIR
// ... do some stuff here with the _PARAM1 and _PARAM2
SetOutPath ${LastKnownOutDir }
!
undef LastKnownOutDir 
!macroend 
Many thanks in advance,
Isawen

isawen is offline   Reply With Quote
Old 14th January 2011, 15:27   #2
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Well you could do:
code:
!macro TestMacro _PARAM1 _PARAM2
!if `${_PARAM1}` == $1
!error `_PARAM1 cannot be $1`
!endif
...

Stu
Afrow UK is offline   Reply With Quote
Old 14th January 2011, 15:32   #3
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
And a more sophisticated solution is to switch $1 to something else if one of the macro parameters is $1, i.e.
code:
!macro TestMacro _PARAM1 _PARAM2
!if `${_PARAM1}` == $1
!define _Var1 $2
!else
!define _Var1 $1
!endif
Push ${_Var1}
...
Pop ${_Var1}
!undef _Var1
!macroend

Here if the parameter is $1, you instead use $2 and leave $1 as it is. In fact, this is how some of the macros in Sections.nsh should be changed to use (those macros were written before !if).

Edit: I should point out that neither of these solutions will work if for example "some string containing $1" is used for _PARAM1. You are stuck there. Perhaps usage of !searchparse may work but you have to match $1 and ignore $$1 somehow.

Stu
Afrow UK is offline   Reply With Quote
Old 14th January 2011, 16:00   #4
isawen
Junior Member
 
Join Date: Jan 2010
Posts: 39
I'm somehow satisfied with the !define command to store the OUTDIR and to restore it at the end.
Thanks.

isawen is offline   Reply With Quote
Old 14th January 2011, 18:03   #5
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Edit: Err, nvm, not applicable here.
MSG is offline   Reply With Quote
Old 14th January 2011, 20:46   #6
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
I am probably wrong to ask but you do realise that !define is a compile time instruction. It will not be set to the value of $OUTDIR because that is not known until run time. Storing $OUTDIR in a constant will not benefit you at all except to save you from having to repeat $OUTDIR throughout the macro. Wherever you use that constant, $OUTDIR will be inserted.

Stu
Afrow UK is offline   Reply With Quote
Old 19th January 2011, 00:11   #7
isawen
Junior Member
 
Join Date: Jan 2010
Posts: 39
Yes, sorry my bad. Thanks for your correction.
I didn't notice that the functionality was wrong until I changed some paths that I needed inside the macro.

Much appreciated.
Isa

isawen is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump