From d3a9692ebec86173c3ebca943472a9259816f007 Mon Sep 17 00:00:00 2001
From: Bill Hoffman <bill.hoffman@kitware.com>
Date: Thu, 2 Mar 2006 13:30:22 -0500
Subject: [PATCH] ENH: check in new find stuff

---
 Registry.cxx    |  1 +
 SystemTools.cxx | 60 ++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/Registry.cxx b/Registry.cxx
index 6e75c66e..7c298f7f 100644
--- a/Registry.cxx
+++ b/Registry.cxx
@@ -267,6 +267,7 @@ bool Registry::DeleteValue(const char *subkey, const char *key)
     if ( !this->Open(this->GetTopLevel(), subkey, 
         Registry::READWRITE) )
       {
+      std::cerr << "Failed to open\n";
       return res;
       }
     open = true;
diff --git a/SystemTools.cxx b/SystemTools.cxx
index 3293ccf6..da90a4d5 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -1923,21 +1923,35 @@ kwsys_stl::string SystemTools::FindProgram(
     {
     return "";
     }
+  std::string ext = SystemTools::GetExecutableExtension();
+  if(ext.size())
+    {
+    unsigned int len = strlen(name);
+    if(len > ext.size())
+      {
+      if(strcmp(name+(len-ext.size()), ext.c_str()) == 0)
+        {
+        ext = ""; // name already has Executable extension
+        }
+      }
+    }
   // See if the executable exists as written.
   if(SystemTools::FileExists(name) &&
       !SystemTools::FileIsDirectory(name))
     {
     return SystemTools::CollapseFullPath(name);
     }
-  kwsys_stl::string tryPath = name;
-  tryPath += SystemTools::GetExecutableExtension();
-  if(SystemTools::FileExists(tryPath.c_str()) &&
-     !SystemTools::FileIsDirectory(tryPath.c_str()))
+  if(ext.size())
     {
-    return SystemTools::CollapseFullPath(tryPath.c_str());
+    kwsys_stl::string tryPath = name;
+    tryPath += ext;
+    if(SystemTools::FileExists(tryPath.c_str()) &&
+       !SystemTools::FileIsDirectory(tryPath.c_str()))
+      {
+      return SystemTools::CollapseFullPath(tryPath.c_str());
+      }
     }
   kwsys_stl::vector<kwsys_stl::string> path;
-  SystemTools::GetPath(path, "CMAKE_PROGRAM_PATH");
   // Add the system search path to our path.
   if (!no_system_path)
     {
@@ -1954,9 +1968,10 @@ kwsys_stl::string SystemTools::FindProgram(
       p != path.end(); ++p)
     {
 #ifdef _WIN32
+    // Remove double quotes from the path on windows
     SystemTools::ReplaceString(*p, "\"", "");
 #endif
-    tryPath = *p;
+    kwsys_stl::string tryPath = *p;
     tryPath += "/";
     tryPath += name;
     if(SystemTools::FileExists(tryPath.c_str()) &&
@@ -1965,22 +1980,35 @@ kwsys_stl::string SystemTools::FindProgram(
       return SystemTools::CollapseFullPath(tryPath.c_str());
       }
 #ifdef _WIN32
-    tryPath += ".com";
-    if(SystemTools::FileExists(tryPath.c_str()) &&
-       !SystemTools::FileIsDirectory(tryPath.c_str()))
+    // on windows try .com before .exe
+    if(ext.size() == 0)
       {
-      return SystemTools::CollapseFullPath(tryPath.c_str());
+      SystemTools::ReplaceString(tryPath, ".exe", ".com");
+      SystemTools::ReplaceString(tryPath, ".EXE", ".com");
+      }
+    else
+      {
+      tryPath += ".com";
       }
-    tryPath = *p;
-    tryPath += "/";
-    tryPath += name;
-#endif
-    tryPath += SystemTools::GetExecutableExtension();
     if(SystemTools::FileExists(tryPath.c_str()) &&
        !SystemTools::FileIsDirectory(tryPath.c_str()))
       {
       return SystemTools::CollapseFullPath(tryPath.c_str());
       }
+#endif
+    // now try to add ext if it is different than name
+    if(ext.size())
+      {
+      tryPath = *p;
+      tryPath += "/";
+      tryPath += name;
+      tryPath += ext;
+      if(SystemTools::FileExists(tryPath.c_str()) &&
+         !SystemTools::FileIsDirectory(tryPath.c_str()))
+        {
+        return SystemTools::CollapseFullPath(tryPath.c_str());
+        }
+      }
     }
 
   // Couldn't find the program.
-- 
GitLab