added a section about logical code structures, with LogicLib examples
git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@4436 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
parent
f5875933bc
commit
90a7765cd6
1 changed files with 73 additions and 0 deletions
|
@ -96,6 +96,79 @@ For more information about functions see \R{functions}{Functions}.
|
|||
|
||||
\S1{tut-working-with-scripts} Working with Scripts
|
||||
|
||||
\S2{tut-logic} Logical Code Structures
|
||||
|
||||
Conditionally executing code, or executing code in a loop can be done using \R{strcmp}{StrCmp}, \R{intcmp}{IntCmp}, \R{iferrors}{IfErrors}, \R{goto}{Goto} and more. However, there's a much easier way do this. The LogicLib provides some very simple macros that allow easy construction of complex logical structures. Its syntax, explained in \L{../Include/LogicLib.nsh}{LogicLib.nsh}, is similar to other programming languages and can prove to be simpler for beginners and advanced users alike.
|
||||
|
||||
For example, checking a value of a variable without the LogicLib can be done as followed.
|
||||
|
||||
\c StrCmp $0 'some value' 0 +3
|
||||
\c MessageBox MB_OK '$$0 is some value'
|
||||
\c Goto done
|
||||
\c StrCmp $0 'else' 0 +3
|
||||
\c MessageBox MB_OK '$$0 is some other value'
|
||||
\c Goto done
|
||||
\c # else
|
||||
\c MessageBox MB_OK '$$0 is "$0"'
|
||||
\c done:
|
||||
|
||||
However, with the LogicLib, the code gets is much more readable and easy to understand, as can be seen in the following example.
|
||||
|
||||
\c ${If} $0 == 'some value'
|
||||
\c MessageBox MB_OK '$$0 is some value'
|
||||
\c ${ElseIf} $0 == 'some other value'
|
||||
\c MessageBox MB_OK '$$0 is some other value'
|
||||
\c ${Else}
|
||||
\c MessageBox MB_OK '$$0 is "$0"'
|
||||
\c ${EndIf}
|
||||
|
||||
The same can also be done using a switch, as shown in the following example.
|
||||
|
||||
\c ${Switch} $0
|
||||
\c ${Case} 'some value'
|
||||
\c MessageBox MB_OK '$$0 is some value'
|
||||
\c ${Break}
|
||||
\c ${Case} 'some other value'
|
||||
\c MessageBox MB_OK '$$0 is some other value'
|
||||
\c ${Break}
|
||||
\c ${Default}
|
||||
\c MessageBox MB_OK '$$0 is "$0"'
|
||||
\c ${Break}
|
||||
\c ${EndSwitch}
|
||||
|
||||
Multiple conditions are also supported. The following example will notify the user, if both $0 and $1 are empty.
|
||||
|
||||
\c ${If} $0 == ''
|
||||
\c ${AndIf} $1 == ''
|
||||
\c MessageBox MB_OK|MB_ICONSTOP 'both are empty!'
|
||||
\c ${EndIf}
|
||||
|
||||
The LogicLib removes the need for labels and relative jumps, thus prevents label name conflicts, and removes the need to manually adjust relative jump offsets every time the script is changed.
|
||||
|
||||
It also simplifies looping, by supporting the common while, do and for loops. All of the following examples count to five using the LogicLib.
|
||||
|
||||
\c StrCpy $R1 0
|
||||
\c ${While} $R1 < 5
|
||||
\c IntOp $R1 $R1 + 1
|
||||
\c DetailPrint $R1
|
||||
\c ${EndWhile}
|
||||
|
||||
\c ${For} $R1 1 5
|
||||
\c DetailPrint $R1
|
||||
\c ${Next}
|
||||
|
||||
\c StrCpy $R1 0
|
||||
\c ${Do}
|
||||
\c IntOp $R1 $R1 + 1
|
||||
\c DetailPrint $R1
|
||||
\c ${LoopUntil} $R1 >= 5
|
||||
|
||||
To use the LogicLib, the following line needs to put at the top of the script.
|
||||
|
||||
\c !include LogicLib.nsh
|
||||
|
||||
More examples can be found in \R{../Examples/LogicLib.nsi}{LogicLib.nsi}.
|
||||
|
||||
\S2{tutVariables} Variables
|
||||
|
||||
You can declare your own variables ($VARNAME) with the \R{var}{Var} command. Variables are global and can be used in any Section or Function.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue