I am wondering if there is any way to meaningfully shorten the chain of conditionals used here to compare two versions. struct VERSIONCODE
represents a version number of the form major.minor.revision
and CompareVersions
returns EQUAL
if they're equal; LHS_NEWER
if vLHS
is newer, and RHS_NEWER
if vRHS
is newer.
typedef enum
{
EQUAL = 0,
LHS_NEWER,
RHS_NEWER
} ECOMPARISON;
// Specifically not named "VERSION" to avoid conflicting with common names from third-party libraries etc.
typedef struct
{
int nMajor;
int nMinor;
int nRev;
} VERSIONCODE;
ECOMPARISON CompareVersions(VERSIONCODE vLHS, VERSIONCODE vRHS)
{
if (vLHS.nMajor > vRHS.nMajor)
{
return LHS_NEWER;
}
else if (vLHS.nMajor < vRHS.nMajor)
{
return RHS_NEWER;
}
else// if (vLHS.nMajor == vRHS.nMajor)
{
if (vLHS.nMinor > vRHS.nMinor)
{
return LHS_NEWER;
}
else if (vLHS.nMinor < vRHS.nMinor)
{
return RHS_NEWER;
}
else// if (vLHS.nMinor == vRHS.nMinor)
{
if (vLHS.nRev > vRHS.nRev)
{
return LHS_NEWER;
}
else if (vLHS.nRev < vRHS.nRev)
{
return RHS_NEWER;
}
else// if(vLHS.nRev == vRHS.nRev)
{
return EQUAL;
}
}
}
}
LHS_NEWER=-1
andRHS_NEWER=1
, because that's what functions likeqsort()
expect \$\endgroup\$