Search code examples
installscript

How do you compare versions in InstallScript?


It looks like there is a builtin function, VerCompare, but it requires strings that have all four components, e.g. 1.2.3.4. I haven't tried to do string manipulation in InstallScript and was hoping someone already had the code to take a version string and add .0's as necessary.


Solution

  • Needs some error checking, but here's the general idea:

    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;