Archive: Using StrCmp - Is it the best option?


Using StrCmp - Is it the best option?
Hello people,

How does one use StrCmp? The example in the NSIS manual doesn't see really explanotory:

StrCmp $0 "a string" 0 +3
DetailPrint '$$0 == "a string"'
Goto +2
DeteailPrint '$$0 != "a string"'


I don't get what is 0 +3 and why Goto +2? Is there no switch statement in NSIS? As am I determining the version of the OS I will probably have about 5 options (Win95..98...Me...NT4....2000...XP...2003 etc).

StrCmp seems messy in this context? Is there a more professional clear view method?

Thank you


Did you even read: 4.9.4.19 StrCmp?

This is what it says right below it:

str1 str2 jump_if_equal [jump_if_not_equal]

Compares (case insensitively) str1 to str2. If str1 and str2 are equal, Gotos jump_if_equal, otherwise Gotos jump_if_not_equal.

So obviously...

StrCmp $0 "a string" 0 +3

Compares the value stored in $0 to the string "a string". If they are equal it continues, if not it jumps 3 instructions.

It's pretty self explanitory


Originally posted by flyakite
Did you even read: 4.9.4.19 StrCmp?

This is what it says right below it:

str1 str2 jump_if_equal [jump_if_not_equal]

Compares (case insensitively) str1 to str2. If str1 and str2 are equal, Gotos jump_if_equal, otherwise Gotos jump_if_not_equal.

So obviously...

StrCmp $0 "a string" 0 +3

Compares the value stored in $0 to the string "a string". If they are equal it continues, if not it jumps 3 instructions.

It's pretty self explanitory
Right...........but it doesn't make for a tidy way in comparing more than two strings does it?

Most string comparison implementations don't. This is what I used when I wanted to compare OS names:


1) Define a $MIN_WIN_VER to be 2000 or XP or whatever.
2) Have the OS name in variable $0.
Then this code:


StrCmp $0 '' unhappyEnding ; If nothing
StrCmp $0 '2003' happyEnding ; If 2003
StrCmp $MIN_WIN_VER '2003' unhappyEnding ; If minimum is 2003
StrCmp $0 'XP' happyEnding ; If XP
StrCmp $MIN_WIN_VER 'XP' unhappyEnding ; If minimum is XP
StrCmp $0 '2000' happyEnding ; If 2000
StrCmp $MIN_WIN_VER '2000' unhappyEnding ; If minimum is 2000
StrCmp $0 'NT 4' happyEnding ; If NT 4
StrCmp $MIN_WIN_VER 'NT 4' unhappyEnding ; If minimum is NT 4
StrCmp $0 'NT 3' happyEnding ; If NT 3
StrCmp $MIN_WIN_VER 'NT 3' unhappyEnding ; If minimum is NT 3
StrCmp $0 'ME' happyEnding ; If Windows ME
StrCmp $MIN_WIN_VER 'ME' unhappyEnding ; If minimum was Windows ME
StrCmp $0 '98' happyEnding ; If Windows 98
StrCmp $MIN_WIN_VER '98' unhappyEnding ; If minimum was Windows 98
StrCmp $0 '95' happyEnding ; If Windows 95

unhappyending:
; DOH OS didn't match.
Goto whatever

happyending:
; YAY OS matched!


The current version 'falls' through the possibilities until either it reaches a version too low (alter the order of OSs if you want to change the compatibility order) or it matches a version.

Hope this helps

-rob-

For a more structured approach see examples\logiclib.nsi