VersionConvert

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


Page for NSIS 2.07 and below users

You can use the latest version of headers (recommended) or the following function code (put the function code in your script before calling it)

Function Description

____________________________________________________________________________
 
                            VersionConvert v1.0
____________________________________________________________________________
 
Thanks Afrow UK (Based on his idea of Function "CharIndexReplace")
 
 
Convert version in the numerical format which can be compared.
 
 
Syntax:
${VersionConvert} "[Version]" "[CharList]" $var
 
"[Version]"         ; Version
                    ;
"[CharList]"        ; List of characters, which will be replaced by numbers
                    ; "abcdefghijklmnopqrstuvwxyz" (default)
                    ;
$var                ; Result: converted version
 
 
Note:
-Converted letters are separated with dot
-If character is non-digit and not in list then it will be converted to dot


Example1:

Section
	${VersionConvert} "9.0a" "" $R0
	; $R0="9.0.01"
 
	${VersionConvert} "9.0c" "" $R1
	; $R1="9.0.03"
 
	${VersionCompare} "$R0" "$R1" $R2
	; $R2="2"   version2 is newer
SectionEnd

Example2:

Section
	${VersionConvert} "0.15c-9m" "" $R0
	; $R0="0.15.03.9.13"
 
	${VersionConvert} "0.15c-1n" "" $R1
	; $R1="0.15.03.1.14"
 
	${VersionCompare} "$R0" "$R1" $R2
	; $R2="1"   version1 is newer
SectionEnd

Example3:

Section
	${VersionConvert} "0.15c+" "abcdefghijklmnopqrstuvwxyz+" $R0
	; $R0="0.15.0327"
 
	${VersionConvert} "0.15c" "abcdefghijklmnopqrstuvwxyz+" $R1
	; $R1="0.15.03"
 
	${VersionCompare} "$R0" "$R1" $R2
	; $R2="1"   version1 is newer
SectionEnd

Function Code

Function VersionConvert
	!define VersionConvert `!insertmacro VersionConvertCall`
 
	!macro VersionConvertCall _VERSION _CHARLIST _RESULT
		Push `${_VERSION}`
		Push `${_CHARLIST}`
		Call VersionConvert
		Pop ${_RESULT}
	!macroend
 
	Exch $1
	Exch
	Exch $0
	Exch
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
 
	StrCmp $1 '' 0 +2
	StrCpy $1 'abcdefghijklmnopqrstuvwxyz'
	StrCpy $1 $1 99
 
	StrCpy $2 0
	StrCpy $7 'dot'
	goto loop
 
	preloop:
	IntOp $2 $2 + 1
 
	loop:
	StrCpy $3 $0 1 $2
	StrCmp $3 '' endcheck
	StrCmp $3 '.' dot
	StrCmp $3 '0' digit
	IntCmp $3 '0' letter letter digit
 
	dot:
	StrCmp $7 'dot' replacespecial
	StrCpy $7 'dot'
	goto preloop
 
	digit:
	StrCmp $7 'letter' insertdot
	StrCpy $7 'digit'
	goto preloop
 
	letter:
	StrCpy $5 0
	StrCpy $4 $1 1 $5
	IntOp $5 $5 + 1
	StrCmp $4 '' replacespecial
	StrCmp $4 $3 0 -3
	IntCmp $5 9 0 0 +2
	StrCpy $5 '0$5'
 
	StrCmp $7 'letter' +2
	StrCmp $7 'dot' 0 +3
	StrCpy $6 ''
	goto +2
	StrCpy $6 '.'
 
	StrCpy $4 $0 $2
	IntOp $2 $2 + 1
	StrCpy $0 $0 '' $2
	StrCpy $0 '$4$6$5$0'
	StrLen $4 '$6$5'
	IntOp $2 $2 + $4
	IntOp $2 $2 - 1
	StrCpy $7 'letter'
	goto loop
 
	replacespecial:
	StrCmp $7 'dot' 0 +3
	StrCpy $6 ''
	goto +2
	StrCpy $6 '.'
 
	StrCpy $4 $0 $2
	IntOp $2 $2 + 1
	StrCpy $0 $0 '' $2
	StrCpy $0 '$4$6$0'
	StrLen $4 $6
	IntOp $2 $2 + $4
	IntOp $2 $2 - 1
	StrCpy $7 'dot'
	goto loop
 
	insertdot:
	StrCpy $4 $0 $2
	StrCpy $0 $0 '' $2
	StrCpy $0 '$4.$0'
	StrCpy $7 'dot'
	goto preloop
 
	endcheck:
	StrCpy $4 $0 1 -1
	StrCmp $4 '.' 0 end
	StrCpy $0 $0 -1
	goto -3
 
	end:
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Exch $0
FunctionEnd