From e3aad42b9eefbeb55ff80f8e37f0a283cec6389b Mon Sep 17 00:00:00 2001
From: Cyrille Valladeau <cyrille.valladeau@c-s.fr>
Date: Wed, 21 Sep 2011 14:31:00 +0200
Subject: [PATCH] ENH: use ApplicationRegistry for path loading

---
 .../otbApplicationLauncherQt.cxx              | 12 +------
 .../otbWrapperCommandLineLauncher.cxx         | 31 +++++++++----------
 .../otbWrapperCommandLineLauncher.h           |  2 +-
 .../otbWrapperCommandLineParser.cxx           |  1 +
 4 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx b/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
index 50e7dafd79..ee59694ae1 100644
--- a/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
+++ b/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
@@ -50,21 +50,11 @@ int main(int argc, char* argv[])
     std::list<std::string>::const_iterator it = modulePathList.begin();
     while( it != modulePathList.end() )
       {
-      std::string modulePath = *it;
-
-      specificEnv += *it;
+      ApplicationRegistry::AddApplicationPath( *(it) );
       ++it;
-      if (it != modulePathList.end())
-        specificEnv += ":";
       }
-
-    // do NOT use putenv() directly, since the string memory must be managed carefully
-    itksys::SystemTools::PutEnv(specificEnv.c_str());
     }
 
-  // Reload factories to take into account new path
-  itk::ObjectFactoryBase::ReHash();
-
   // Create module
   Application::Pointer app = ApplicationRegistry::CreateApplication(moduleName);
   if (app.IsNull())
diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
index 38db567cdb..eaefbd445e 100644
--- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
+++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
@@ -105,7 +105,12 @@ CommandLineLauncher::Load()
     return false;
     }
 
-  this->LoadPath();
+  if( this->LoadPath() == false )
+    {
+    std::cerr << "ERROR: At least one specifed path within \""<< m_Parser->GetAttributAsString(m_Parser->GetModulePathKey(), m_Expression)<<"\" is invalid..." <<std::endl;
+    return false;
+    }
+
   this->LoadApplication();
 
   return true;
@@ -225,31 +230,25 @@ CommandLineLauncher::BeforeExecute()
   return true;
 }
 
-void
+bool
 CommandLineLauncher::LoadPath()
 {
   std::vector<std::string> pathList;
   // If users has set path...
   if( m_Parser->GetPaths( pathList, m_Expression ) == CommandLineParser::OK )
     {
-    // Contain paths into a string, separating each path with ":"
-    m_Path = std::string("");
     for( unsigned i=0; i<pathList.size(); i++)
       {
-      m_Path.append(pathList[i]);
-      if( i < pathList.size()-1 )
-        {
-        m_Path.append(":");
-        }
+      std::cout<<pathList[i]<<std::endl;
+      ApplicationRegistry::AddApplicationPath(pathList[i]);
       }
-
-    std::string specificEnv("ITK_AUTOLOAD_PATH=");
-    specificEnv.append(m_Path);
-    // do NOT use putenv() directly, since the string memory must be managed carefully
-    itksys::SystemTools::PutEnv(specificEnv.c_str());
-    // Reload factories to take into account new path
-    itk::ObjectFactoryBase::ReHash();
     }
+  else
+    {
+    return false;
+    }
+
+  return true;
 }
 
 
diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h
index d137dee04e..7ea9cbeb68 100644
--- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h
+++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h
@@ -106,7 +106,7 @@ protected:
   virtual ~CommandLineLauncher();
 
   /** Load the executable path. It looks for the key --modulePath, extract and interpret as path the following strings. */
-  void LoadPath();
+  bool LoadPath();
 
  /** Load the application calling the CreateApplication method of the ApplicationRegistry classes.
   * Pay attention, the executable paths have to be loaded or set in the environment before calling the function. */
diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx
index 436c4f3d72..fc3a57f325 100644
--- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx
+++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx
@@ -62,6 +62,7 @@ CommandLineParser::GetPaths( std::vector<std::string> & paths, const std::string
     std::string fullPath = itksys::SystemTools::CollapseFullPath(pathAttribut[i].c_str());
     if( !itksys::SystemTools::FileIsDirectory(fullPath.c_str()) )
       {
+  std::cout<<"module path INVALIDMODULEPATH"<<std::endl;
       return INVALIDMODULEPATH;
       }
     paths.push_back(fullPath);
-- 
GitLab