henrywood Contributor
Joined: 21 Sep 2004 Posts: 66 Location: Copenhagen, Denmark
|
Posted: Fri Aug 05, 2005 12:09 am Post subject: @version_compare() function |
|
|
In case anyone needs a decent version_compare version, partially modelled after PHP's version of the same name
Henrik
Code: |
#-----------------------------------------------------------------------------#
# #
# Version Compare 1.0 #
# #
# Author: Henrik Skov #
# #
# Copyright: 2005 #
# #
#-----------------------------------------------------------------------------#
#define function,replace
#define function,version_compare
%%IEVER = @verinfo(@windir(S)\shdocvw.dll,V)
%%IEREQ = 6.0
%%OK = @version_compare(%%IEVER,%%IEREQ)
info OK: %%OK
stop
################################################################################
#
# %%OK = @version_compare(<version1>,<version2>,<[compare_operator = "=>" ]>, <[returnBuildInfo = FALSE]>, <[replaceParenthesis = FALSE]>
#
################################################################################
#
# Returns TRUS (1) if the relationship between version1 and version2 is as specified by
# the compare operator.
#
# <version1> First version string
#
# <version2> Second version string
#
# <[compare_operator]> Possible compare operators:
#
# lt, le, gt, >=, ge, ==, =, eq, !=,
# <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne
#
# <[returnBuldInfo]> If specified, anything after a space in the version1 string is
# returned and no version comparison is done
#
# <[replaceParenthesis]> If specified, any parenthesis (both () and []) will be removed
# before the BuildInfo part is returned
#
################################################################################
#
# TODO:
# -------------------
# - Add support for alpha, RC, beta or other build strings
# so that they are part of the version comparison
#
# - Model after PHP's version_compare() 100%
# See: http://dk2.php.net/version_compare
#
################################################################################
:VERSION_COMPARE
if @not(@both(%1,%2))
error 2
exit
end
# Default compare_operator
if @not(%3)
%3 = "=>"
end
option decimalsep,"."
%A = @fsep()
option fieldsep," "
parse "%V;%D",%1
#
if @both(%5,@not(%4))
error 2
exit
end
# Should we return Build info ?
if %4
if %5
%D = @replace("(",,%D)
%D = @replace(")",,%D)
%D = @replace("[",,%D)
%D = @replace("]",,%D)
end
exit %D
end
# Store pos of first '.' to find major ver
%P = @pos(".",%V)
# Major
%M = @substr(%V,1,@pred(%P))
#info Major: %M
# Minor
%P = @succ(%P)
%I = @substr(%V,%P,@len(%V))
#info Minor: %I
# Remove .
%N = @replace(".",,%I)
option fieldsep,%A
%V = %M.%N
#info In: %1@cr()InConv: %V
# Now test
if @equal(%3,>=)@equal(%3,=>)@equal(%3,ge)
if @greater(%V,%2)@equal(%V,%2)
%R = 1
else
%R =
end
elsif @equal(%3,<=)@equal(%3,=<)@equal(%3,le)
if @not(@greater(%V,%2))@equal(%V,%2)
%R = 1
else
%R =
end
elsif @equal(%3,=)@equal(%3,==)@equal(%3,eq)
if @equal(%V,%2)
%R = 1
else
%R =
end
elsif @equal(%3,<)@equal(%3,lt)
if @not(@greater(%V,%2))
%R = 1
else
%R =
end
elsif @equal(%3,>)@equal(%3,gt)
if @greater(%V,%2)
%R = 1
else
%R =
end
elsif @equal(%3,!=)@equal(%3,ne)@equal(%3,<>)
if @not(@equal(%V,%2))
%R = 1
else
%R =
end
end
exit %R
######################################################################################################################################################
# %%NEWSTRING = @replace(search,replace,source)
:REPLACE
list create,28
list add,28,%3
%R = %2
%S = %1
%%index = 0
repeat
%%instring = @item(28,%%index)
%%newstring = ""
repeat
%%pos = @pos(%S,%%instring)
if @greater(%%pos,0)
if @equal(%%pos,1)
%%newstring = %%newstring%R
else
%%newstring = %%newstring@substr(%%instring,1,@pred(%%pos))%R
end
%%instring = @strdel(%%instring,1,@pred(@sum(%%pos,@len(%S))))
end
until @equal(%%pos,0)
%%newstring = %%newstring%%instring
list put,28,%%newstring
%%index = @succ(%%index)
until @equal(%%index,@count(2)
%%NEW = @substr(@text(2,1,@pred(@len(@text(2)))
list close,28
exit %%NEW
|
|
|