Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
index 54c12d1d51e7119136e2452db9d966f274fb72e9..f2fcda4b717278ae83d5787a0d497623b47bcb95 100644 (file)
@@ -6,20 +6,26 @@
  */
 
 #include <Config_Keywords.h>
+#include <Config_Common.h>
 #include <Config_ModuleReader.h>
 #include <Config_FeatureReader.h>
+#include <Events_Error.h>
 
-#include <libxml\parser.h>
-#include <libxml\tree.h>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
 
-#ifdef _DEBUG
+//Necessary for cerr
 #include <iostream>
-#endif
-
 
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif
 
 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
-    : Config_XMLReader("plugins.xml"), myIsAutoImport(false), myEventGenerated(theEventGenerated)
+    : Config_XMLReader("plugins.xml"),
+      myEventGenerated(theEventGenerated)
 {
 }
 
@@ -27,6 +33,11 @@ Config_ModuleReader::~Config_ModuleReader()
 {
 }
 
+const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
+{
+  return myFeaturesInFiles;
+}
+
 /*
  * Get module name from plugins.xml
  * (property "module")
@@ -45,9 +56,11 @@ void Config_ModuleReader::processNode(xmlNodePtr theNode)
   if (isNode(theNode, NODE_PLUGIN, NULL)) {
     std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
-    if (myIsAutoImport)
-      importPlugin(aPluginConf, aPluginLibrary);
-    myPluginsMap[aPluginLibrary] = aPluginConf;
+    std::list<std::string> aFeatures = importPlugin(aPluginLibrary, aPluginConf);
+    std::list<std::string>::iterator it = aFeatures.begin();
+    for (; it != aFeatures.end(); it++) {
+      myFeaturesInFiles[*it] = aPluginConf;
+    }
   }
 }
 
@@ -56,24 +69,38 @@ bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
   return isNode(theNode, NODE_PLUGINS, NULL);
 }
 
-void Config_ModuleReader::importPlugin(const std::string& thePluginName,
-                                       const std::string& thePluginLibrary)
+std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
+                                                         const std::string& thePluginFile)
 {
-  Config_FeatureReader* aReader;
-  if(thePluginLibrary.empty()) {
-    aReader = new Config_FeatureReader(thePluginName);
-  } else {
-    aReader = new Config_FeatureReader(thePluginName, thePluginLibrary, myEventGenerated);
+  if (thePluginFile.empty()) {  //probably a third party library
+    loadLibrary(thePluginLibrary);
+    return std::list<std::string>();
   }
-  aReader->readAll();
-}
 
-void Config_ModuleReader::setAutoImport(bool theEnabled)
-{
-  myIsAutoImport = theEnabled;
+  Config_FeatureReader aReader = Config_FeatureReader(thePluginFile, thePluginLibrary,
+                                                      myEventGenerated);
+  aReader.readAll();
+  return aReader.features();
 }
 
-const std::map<std::string, std::string>& Config_ModuleReader::plugins() const
+void Config_ModuleReader::loadLibrary(const std::string theLibName)
 {
-  return myPluginsMap;
+  std::string aFileName = library(theLibName);
+  if (aFileName.empty())
+    return;
+
+#ifdef WIN32
+  HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
+  if (!aModLib && theLibName != "DFBrowser") {  // don't shor error for internal debugging tool
+    std::string errorMsg = "Failed to load " + aFileName;
+    std::cerr << errorMsg << std::endl;
+    Events_Error::send(errorMsg);
+  }
+#else
+  void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
+  if ( !aModLib && theLibName != "DFBrowser") {  // don't shor error for internal debugging tool
+    std::cerr << "Failed to load " << aFileName.c_str() << std::endl;
+  }
+#endif
 }
+