问题:
今天发现自己之前写的NSIS脚本中有个BUG,BUG的来源正是NSIS的版本号比较函数:VersionCompare
这个函数在比较"2008.3.15.1"和"2008.05.1.10"返回的不是期待的2,却是1
于是,可怕的事情发生了……
解决:
同类的几个函数,经过一番测试,采用下面这个函数。
http://nsis.sourceforge.net/Comparing_Two_Version_Numbers
不过原来有个小bug,就是没有初始化自己的变量,会导致多次调用时的结果出错。
于是我也修改之后直接在wiki上提交了我的修改,并在这里转载这个函数,以便下次使用。
- ; From:http://nsis.sourceforge.net/Comparing_Two_Version_Numbers
- ; Created 1/5/05 by Comperio
- ;
- ; Usage:
- ; ${VersionCheck} "Version1" "Version2" "outputVar"
- ; Version1 = 1st version number
- ; Version2 = 2nd version number
- ; outputVar = Variable used to store the ouput
- ;
- ; Return values:
- ; 0 = both versions are equal
- ; 1 = Version 1 is NEWER than version 2
- ; 2 = Version1 is OLDER than version 2
- ; Rules for Version Numbers:
- ; Version numbers must always be a string of numbers only. (A dot
- ; in the name is optional).
- ;
- ; [Variables]
- var P1 ; file pointer, used to "remember" the position in the Version1 string
- var P2 ; file pointer, used to "remember" the position in the Version2 string
- var V1 ;version number from Version1
- var V2 ;version number from Version2
- Var Reslt ; holds the return flag
- ; [Macros]
- !macro VersionCheck Ver1 Ver2 OutVar
- ; To make this work, one character must be added to the version string:
- Push "x${Ver2}"
- Push "x${Ver1}"
- Call VersionCheckF
- Pop ${OutVar}
- !macroend
- ; [Defines]
- !define VersionCheck "!insertmacro VersionCheck"
- ; [Functions]
- Function VersionCheckF
- Exch $1 ; $1 contains Version 1
- Exch
- Exch $2 ; $2 contains Version 2
- Exch
- Push $R0
- ; initialize Variables
- StrCpy $V1 ""
- StrCpy $V2 ""
- StrCpy $P1 ""
- StrCpy $P2 ""
- StrCpy $Reslt ""
- ; Set the file pointers:
- IntOp $P1 $P1 + 1
- IntOp $P2 $P2 + 1
- ; ******************* Get 1st version number for Ver1 **********************
- V11:
- ; I use $1 and $