如何比较InstallScript中的版本?

时间:2012-11-20 00:43:34

标签: installscript

看起来有一个内置函数VerCompare,但它需要包含所有四个组件的字符串,例如1.2.3.4。我没有尝试在InstallScript中进行字符串操作,并希望有人已经有代码来获取版本字符串并根据需要添加.0。

1 个答案:

答案 0 :(得分:5)

需要进行一些错误检查,但这是一般性的想法:

prototype NUMBER CompareVersions(STRING, STRING);
prototype STRING FormatVersion(STRING);

function NUMBER CompareVersions(leftVersion, rightVersion)
    STRING formattedLeftVersion, formattedRightVersion;
begin
    formattedLeftVersion = FormatVersion(leftVersion);
    formattedRightVersion = FormatVersion(rightVersion);

    return VerCompare(formattedLeftVersion, formattedRightVersion, VERSION);
end;

function STRING FormatVersion(version)
    STRING formattedVersion;
    LIST tokens;
    NUMBER count;
begin
    tokens = ListCreate(STRINGLIST);
    StrGetTokens(tokens, version, ".");
    count = ListCount(tokens);
    ListSetIndex(tokens, LISTLAST);
    while (count < 4)
        ListAddString(tokens, "0", AFTER);
        count = count + 1;
    endwhile;
    StrPutTokens(tokens, formattedVersion, ".", FALSE);
    ListDestroy(tokens);
    return formattedVersion;
 end;