strversion.cpp revision f859334e89cff63a2472f1a95a28e77bc07667f1
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/* $Id$ */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/** @file
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * IPRT - Version String Parsing.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/*
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * Copyright (C) 2009 Oracle Corporation
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync *
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * available from http://www.virtualbox.org. This file is free software;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * General Public License (GPL) as published by the Free Software
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync *
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * The contents of this file may alternatively be used under the terms
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * of the Common Development and Distribution License Version 1.0
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * VirtualBox OSE distribution, in which case the provisions of the
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * CDDL are applicable instead of those of the GPL.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync *
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * You may elect to license modified versions of this file under the
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * terms and conditions of either the GPL or the CDDL or both.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/*******************************************************************************
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync* Header Files *
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync*******************************************************************************/
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <iprt/string.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include "internal/iprt.h"
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <iprt/assert.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <iprt/ctype.h>
739c77783387df1b22501b27bd870869a865d20cvboxsync#include <iprt/err.h>
739c77783387df1b22501b27bd870869a865d20cvboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync/*******************************************************************************
739c77783387df1b22501b27bd870869a865d20cvboxsync* Defined Constants And Macros *
739c77783387df1b22501b27bd870869a865d20cvboxsync*******************************************************************************/
739c77783387df1b22501b27bd870869a865d20cvboxsync#define RTSTRVER_IS_PUNCTUACTION(ch) \
739c77783387df1b22501b27bd870869a865d20cvboxsync ( (ch) == '_' || (ch) == '-' || (ch) == '+' || RT_C_IS_PUNCT(ch) )
739c77783387df1b22501b27bd870869a865d20cvboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/**
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * Parses a out the next block from a version string.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync *
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * @returns true if numeric, false if not.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * @param ppszVer The string cursor, IN/OUT.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * @param pi32Value Where to return the value if numeric.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * @param pcchBlock Where to return the block length.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsyncstatic bool rtStrVersionParseBlock(const char **ppszVer, int32_t *pi32Value, size_t *pcchBlock)
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync{
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync const char *psz = *ppszVer;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync /*
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * Check for end-of-string.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync if (!*psz)
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync {
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync *pi32Value = 0;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync *pcchBlock = 0;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync return false;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync }
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync /*
739c77783387df1b22501b27bd870869a865d20cvboxsync * Try convert the block to a number the simple way.
739c77783387df1b22501b27bd870869a865d20cvboxsync */
739c77783387df1b22501b27bd870869a865d20cvboxsync char ch;
739c77783387df1b22501b27bd870869a865d20cvboxsync bool fNumeric = RT_C_IS_DIGIT(*psz);
739c77783387df1b22501b27bd870869a865d20cvboxsync if (fNumeric)
739c77783387df1b22501b27bd870869a865d20cvboxsync {
739c77783387df1b22501b27bd870869a865d20cvboxsync do
739c77783387df1b22501b27bd870869a865d20cvboxsync ch = *++psz;
739c77783387df1b22501b27bd870869a865d20cvboxsync while (ch && RT_C_IS_DIGIT(ch));
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync int rc = RTStrToInt32Ex(*ppszVer, NULL, 10, pi32Value);
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync if (RT_FAILURE(rc) || rc == VWRN_NUMBER_TOO_BIG)
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync {
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync AssertRC(rc);
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync fNumeric = false;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync *pi32Value = 0;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync }
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync }
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync else
{
/*
* Find the end of the current string. Make a special case for SVN
* revision numbers that immediately follows a release tag string.
*/
do
ch = *++psz;
while ( ch
&& !RT_C_IS_DIGIT(ch)
&& !RTSTRVER_IS_PUNCTUACTION(ch));
size_t cchBlock = psz - *ppszVer;
if ( cchBlock > 1
&& psz[-1] == 'r'
&& RT_C_IS_DIGIT(*psz))
{
psz--;
cchBlock--;
}
/*
* Translate standard pre release terms to negative values.
*/
static const struct
{
size_t cch;
const char *psz;
int32_t iValue;
} s_aTerms[] =
{
{ 2, "RC", -100000 },
{ 3, "PRE", -200000 },
{ 5, "GAMMA", -300000 },
{ 4, "BETA", -400000 },
{ 5, "ALPHA", -500000 }
};
int32_t iVal1 = 0;
for (unsigned i = 0; i < RT_ELEMENTS(s_aTerms); i++)
if ( cchBlock == s_aTerms[i].cch
&& !RTStrNCmp(s_aTerms[i].psz, *ppszVer, cchBlock))
{
iVal1 = s_aTerms[i].iValue;
break;
}
if (iVal1 != 0)
{
/*
* Does the prelease term have a trailing number?
* Add it assuming BETA == BETA1.
*/
if (RT_C_IS_DIGIT(*psz))
{
const char *psz2 = psz;
do
ch = *++psz;
while ( ch
&& RT_C_IS_DIGIT(ch)
&& !RTSTRVER_IS_PUNCTUACTION(ch));
int rc = RTStrToInt32Ex(psz2, NULL, 10, pi32Value);
if (RT_SUCCESS(rc) && rc != VWRN_NUMBER_TOO_BIG && *pi32Value)
iVal1 += *pi32Value - 1;
else
{
AssertRC(rc);
psz = psz2;
}
}
fNumeric = true;
}
*pi32Value = iVal1;
}
*pcchBlock = psz - *ppszVer;
/*
* Skip trailing punctuation.
*/
if (RTSTRVER_IS_PUNCTUACTION(*psz))
psz++;
*ppszVer = psz;
return fNumeric;
}
RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2)
{
AssertPtr(pszVer1);
AssertPtr(pszVer2);
/*
* Do a parallel parse of the strings.
*/
while (*pszVer1 || *pszVer2)
{
const char *pszBlock1 = pszVer1;
size_t cchBlock1;
int32_t iVal1;
bool fNumeric1 = rtStrVersionParseBlock(&pszVer1, &iVal1, &cchBlock1);
const char *pszBlock2 = pszVer2;
size_t cchBlock2;
int32_t iVal2;
bool fNumeric2 = rtStrVersionParseBlock(&pszVer2, &iVal2, &cchBlock2);
if (fNumeric1 && fNumeric2)
{
if (iVal1 != iVal2)
return iVal1 < iVal2 ? -1 : 1;
}
else if ( fNumeric1 != fNumeric2
&& ( fNumeric1
? iVal1 == 0 && cchBlock2 == 0
: iVal2 == 0 && cchBlock1 == 0)
)
{
/*else: 1.0 == 1.0.0.0.0. */;
}
else if ( fNumeric1 != fNumeric2
&& (fNumeric1 ? iVal1 : iVal2) < 0)
{
/* Pre-release indicators are smaller than all other strings. */
return fNumeric1 ? -1 : 1;
}
else
{
int iDiff = RTStrNICmp(pszBlock1, pszBlock2, RT_MIN(cchBlock1, cchBlock2));
if (!iDiff && cchBlock1 != cchBlock2)
iDiff = cchBlock1 < cchBlock2 ? -1 : 1;
if (iDiff)
return iDiff < 0 ? -1 : 1;
}
}
return 0;
}
RT_EXPORT_SYMBOL(RTStrVersionCompare);