Bladeren bron

De-duplicate version comparison code.

Extend the VersionCompare in cmSystemTools to handle 8 components,
and port the if command to use that.
Stephen Kelly 12 jaren geleden
bovenliggende
commit
48bb48e114
2 gewijzigde bestanden met toevoegingen van 14 en 41 verwijderingen
  1. 4 36
      Source/cmIfCommand.cxx
  2. 10 5
      Source/cmSystemTools.cxx

+ 4 - 36
Source/cmIfCommand.cxx

@@ -405,38 +405,6 @@ namespace
     reducible = 1;
   }
 
-  //=========================================================================
-  enum Op { OpLess, OpEqual, OpGreater };
-  bool HandleVersionCompare(Op op, const char* lhs_str, const char* rhs_str)
-  {
-  // Parse out up to 8 components.
-  unsigned int lhs[8] = {0,0,0,0,0,0,0,0};
-  unsigned int rhs[8] = {0,0,0,0,0,0,0,0};
-  sscanf(lhs_str, "%u.%u.%u.%u.%u.%u.%u.%u",
-         &lhs[0], &lhs[1], &lhs[2], &lhs[3],
-         &lhs[4], &lhs[5], &lhs[6], &lhs[7]);
-  sscanf(rhs_str, "%u.%u.%u.%u.%u.%u.%u.%u",
-         &rhs[0], &rhs[1], &rhs[2], &rhs[3],
-         &rhs[4], &rhs[5], &rhs[6], &rhs[7]);
-
-  // Do component-wise comparison.
-  for(unsigned int i=0; i < 8; ++i)
-    {
-    if(lhs[i] < rhs[i])
-      {
-      // lhs < rhs, so true if operation is LESS
-      return op == OpLess;
-      }
-    else if(lhs[i] > rhs[i])
-      {
-      // lhs > rhs, so true if operation is GREATER
-      return op == OpGreater;
-      }
-    }
-  // lhs == rhs, so true if operation is EQUAL
-  return op == OpEqual;
-  }
-
   //=========================================================================
   // level 0 processes parenthetical expressions
   bool HandleLevel0(std::list<std::string> &newArgs,
@@ -723,16 +691,16 @@ namespace
         {
         def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
         def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
-        Op op = OpEqual;
+        cmSystemTools::CompareOp op = cmSystemTools::OP_EQUAL;
         if(*argP1 == "VERSION_LESS")
           {
-          op = OpLess;
+          op = cmSystemTools::OP_LESS;
           }
         else if(*argP1 == "VERSION_GREATER")
           {
-          op = OpGreater;
+          op = cmSystemTools::OP_GREATER;
           }
-        bool result = HandleVersionCompare(op, def, def2);
+        bool result = cmSystemTools::VersionCompare(op, def, def2);
         HandleBinaryOp(result,
           reducible, arg, newArgs, argP1, argP2);
         }

+ 10 - 5
Source/cmSystemTools.cxx

@@ -2680,13 +2680,18 @@ bool cmSystemTools::ChangeRPath(std::string const& file,
 bool cmSystemTools::VersionCompare(cmSystemTools::CompareOp op,
                                    const char* lhss, const char* rhss)
 {
-  unsigned int lhs[4] = {0,0,0,0};
-  unsigned int rhs[4] = {0,0,0,0};
-  sscanf(lhss, "%u.%u.%u.%u", &lhs[0], &lhs[1], &lhs[2], &lhs[3]);
-  sscanf(rhss, "%u.%u.%u.%u", &rhs[0], &rhs[1], &rhs[2], &rhs[3]);
+  // Parse out up to 8 components.
+  unsigned int lhs[8] = {0,0,0,0,0,0,0,0};
+  unsigned int rhs[8] = {0,0,0,0,0,0,0,0};
+  sscanf(lhss, "%u.%u.%u.%u.%u.%u.%u.%u",
+         &lhs[0], &lhs[1], &lhs[2], &lhs[3],
+         &lhs[4], &lhs[5], &lhs[6], &lhs[7]);
+  sscanf(rhss, "%u.%u.%u.%u.%u.%u.%u.%u",
+         &rhs[0], &rhs[1], &rhs[2], &rhs[3],
+         &rhs[4], &rhs[5], &rhs[6], &rhs[7]);
 
   // Do component-wise comparison.
-  for(unsigned int i=0; i < 4; ++i)
+  for(unsigned int i=0; i < 8; ++i)
     {
     if(lhs[i] < rhs[i])
       {