Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions
nsVersionComparator.cpp File Reference
#include "nsVersionComparator.h"
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  VersionPart

Functions

static char * ParseVP (char *part, VersionPart &result)
 Parse a version part into a number and "extra text".
static PRInt32 ns_strcmp (const char *str1, const char *str2)
static PRInt32 ns_strnncmp (const char *str1, PRUint32 len1, const char *str2, PRUint32 len2)
static PRInt32 ns_cmp (PRInt32 n1, PRInt32 n2)
static PRInt32 CompareVP (VersionPart &v1, VersionPart &v2)
 Compares two VersionParts.
PRInt32 NS_CompareVersions (const char *A, const char *B)
 Compare two version strings.

Class Documentation

struct VersionPart

Definition at line 43 of file nsVersionComparator.cpp.

Class Members
char * extraD
PRInt32 numA
PRInt32 numC
const char * strB
PRUint32 strBlen

Function Documentation

static PRInt32 CompareVP ( VersionPart v1,
VersionPart v2 
) [static]

Compares two VersionParts.

Definition at line 175 of file nsVersionComparator.cpp.

{
  PRInt32 r = ns_cmp(v1.numA, v2.numA);
  if (r)
    return r;

  r = ns_strnncmp(v1.strB, v1.strBlen, v2.strB, v2.strBlen);
  if (r)
    return r;

  r = ns_cmp(v1.numC, v2.numC);
  if (r)
    return r;

  return ns_strcmp(v1.extraD, v2.extraD);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRInt32 ns_cmp ( PRInt32  n1,
PRInt32  n2 
) [static]

Definition at line 163 of file nsVersionComparator.cpp.

{
  if (n1 < n2)
    return -1;

  return n1 != n2;
}

Here is the caller graph for this function:

PRInt32 NS_CompareVersions ( const char *  A,
const char *  B 
)

Compare two version strings.

See also:
nsIVersionComparator

Definition at line 193 of file nsVersionComparator.cpp.

{
  char *A2 = strdup(A);
  if (!A2)
    return 1;

  char *B2 = strdup(B);
  if (!B2) {
    free(A2);
    return 1;
  }

  PRInt32 result;
  char *a = A2, *b = B2;

  do {
    VersionPart va, vb;

    a = ParseVP(a, va);
    b = ParseVP(b, vb);

    result = CompareVP(va, vb);
    if (result)
      break;

  } while (a || b);

  free(A2);
  free(B2);

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRInt32 ns_strcmp ( const char *  str1,
const char *  str2 
) [static]

Definition at line 124 of file nsVersionComparator.cpp.

{
  // any string is *before* no string
  if (!str1)
    return str2 != 0;

  if (!str2)
    return -1;

  return strcmp(str1, str2);
}

Here is the caller graph for this function:

static PRInt32 ns_strnncmp ( const char *  str1,
PRUint32  len1,
const char *  str2,
PRUint32  len2 
) [static]

Definition at line 138 of file nsVersionComparator.cpp.

{
  // any string is *before* no string
  if (!str1)
    return str2 != 0;

  if (!str2)
    return -1;

  for (; len1 && len2; --len1, --len2, ++str1, ++str2) {
    if (*str1 < *str2)
      return -1;

    if (*str1 > *str2)
      return 1;
  }

  if (len1 == 0)
    return len2 == 0 ? 0 : -1;

  return 1;
}

Here is the caller graph for this function:

static char* ParseVP ( char *  part,
VersionPart result 
) [static]

Parse a version part into a number and "extra text".

Returns:
A pointer to the next versionpart, or null if none.

Definition at line 60 of file nsVersionComparator.cpp.

{
  char *dot;

  result.numA = 0;
  result.strB = nsnull;
  result.strBlen = 0;
  result.numC = 0;
  result.extraD = nsnull;

  if (!part)
    return part;

  dot = strchr(part, '.');
  if (dot)
    *dot = '\0';

  if (part[0] == '*' && part[1] == '\0') {
    result.numA = PR_INT32_MAX;
    result.strB = "";
  }
  else {
    result.numA = strtol(part, NS_CONST_CAST(char**, &result.strB), 10);
  }

  if (!*result.strB) {
    result.strB = nsnull;
    result.strBlen = 0;
  }
  else {
    if (result.strB[0] == '+') {
      static const char kPre[] = "pre";

      ++result.numA;
      result.strB = kPre;
      result.strBlen = sizeof(kPre) - 1;
    }
    else {
      const char *numstart = strpbrk(result.strB, "0123456789+-");
      if (!numstart) {
       result.strBlen = strlen(result.strB);
      }
      else {
       result.strBlen = numstart - result.strB;

       result.numC = strtol(numstart, &result.extraD, 10);
       if (!*result.extraD)
         result.extraD = nsnull;
      }
    }
  }

  if (dot) {
    ++dot;

    if (!*dot)
      dot = nsnull;
  }

  return dot;
}

Here is the call graph for this function:

Here is the caller graph for this function: