]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
Adding a new env var: USER_CATALOG_RESOURCES_FILE that permits to have
authorribes <ribes>
Wed, 1 Apr 2009 14:22:28 +0000 (14:22 +0000)
committerribes <ribes>
Wed, 1 Apr 2009 14:22:28 +0000 (14:22 +0000)
a user Catalog resources file

src/ResourcesManager/ResourcesManager.cxx
src/ResourcesManager/ResourcesManager.hxx
src/ResourcesManager/SALOME_ResourcesCatalog_Handler.cxx
src/ResourcesManager/SALOME_ResourcesCatalog_Parser.hxx

index 5bcf6d8437d7fb61d098b20a525053cc85241400..5b068ea31689ce72f899e87a8a6544538a30caf8 100644 (file)
@@ -45,9 +45,9 @@ using namespace std;
 //=============================================================================
 
 ResourcesManager_cpp::
-ResourcesManager_cpp(const char *xmlFilePath) :
-    _path_resources(xmlFilePath)
+ResourcesManager_cpp(const char *xmlFilePath)
 {
+  _path_resources.push_back(xmlFilePath);
 #if defined(_DEBUG_) || defined(_DEBUG)
   cerr << "ResourcesManager_cpp constructor" << endl;
 #endif
@@ -69,25 +69,33 @@ ResourcesManager_cpp::ResourcesManager_cpp() throw(ResourcesException)
 #if defined(_DEBUG_) || defined(_DEBUG)
   cerr << "ResourcesManager_cpp constructor" << endl;
 #endif
-  _isAppliSalomeDefined = (getenv("APPLI") != 0);
-  if(!getenv("KERNEL_ROOT_DIR"))
-    throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!!");
 
-  if (_isAppliSalomeDefined)
+  std::string default_file("");
+  if (getenv("APPLI") != 0)
     {
-      _path_resources = getenv("HOME");
-      _path_resources += "/";
-      _path_resources += getenv("APPLI");
-      _path_resources += "/CatalogResources.xml";
+      default_file += getenv("HOME");
+      default_file += "/";
+      default_file += getenv("APPLI");
+      default_file += "/CatalogResources.xml";
+      _path_resources.push_back(default_file);
     }
-
   else
     {
-      _path_resources = getenv("KERNEL_ROOT_DIR");
-      _path_resources += "/share/salome/resources/kernel/CatalogResources.xml";
+      if(!getenv("KERNEL_ROOT_DIR"))
+       throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!! -> cannot load a CatalogResources.xml");
+      default_file = getenv("KERNEL_ROOT_DIR");
+      default_file += "/share/salome/resources/kernel/CatalogResources.xml";
+      _path_resources.push_back(default_file);
     }
 
-  ParseXmlFile();
+  if (getenv("USER_CATALOG_RESOURCES_FILE") != 0)
+  {
+    std::string user_file("");
+    user_file = getenv("USER_CATALOG_RESOURCES_FILE");
+    _path_resources.push_back(user_file);
+  }
+
+  ParseXmlFiles();
 #if defined(_DEBUG_) || defined(_DEBUG)
   cerr << "ResourcesManager_cpp constructor end";
 #endif
@@ -126,7 +134,7 @@ ResourcesManager_cpp::GetFittingResources(const machineParams& params,
 {
   vector <std::string> vec;
 
-  ParseXmlFile();
+  ParseXmlFiles();
 
   const char *hostname = params.hostname.c_str();
 #if defined(_DEBUG_) || defined(_DEBUG)
@@ -283,19 +291,19 @@ void ResourcesManager_cpp::DeleteResourceInCatalog(const char *hostname)
  */ 
 //=============================================================================
 
-void ResourcesManager_cpp::WriteInXmlFile()
+void ResourcesManager_cpp::WriteInXmlFile(std::string & xml_file)
 {
-  const char* aFilePath = _path_resources.c_str();
-  
+#if defined(_DEBUG_) || defined(_DEBUG)
+  std::cerr << "WriteInXmlFile : start" << std::endl;
+#endif
+  const char* aFilePath = xml_file.c_str();
   FILE* aFile = fopen(aFilePath, "w");
 
   if (aFile == NULL)
-    {
-#if defined(_DEBUG_) || defined(_DEBUG)
-      cerr << "Error opening file !"  << endl;
-#endif
-      return;
-    }
+  {
+    std::cerr << "Error opening file in WriteInXmlFile : " << xml_file << std::endl;
+    return;
+  }
   
   xmlDocPtr aDoc = xmlNewDoc(BAD_CAST "1.0");
   xmlNewDocComment(aDoc, BAD_CAST "ResourcesCatalog");
@@ -305,20 +313,15 @@ void ResourcesManager_cpp::WriteInXmlFile()
   handler->PrepareDocToXmlFile(aDoc);
   delete handler;
 
-#if defined(_DEBUG_) || defined(_DEBUG)
   int isOk = xmlSaveFile(aFilePath, aDoc);
-  if (!isOk) cerr << "Error while XML file saving." << endl;
-#else
-  xmlSaveFile(aFilePath, aDoc);
-#endif
+  if (!isOk) 
+     std::cerr << "Error while XML file saving : " << xml_file << std::endl;
   
   // Free the document
   xmlFreeDoc(aDoc);
-
   fclose(aFile);
-  
 #if defined(_DEBUG_) || defined(_DEBUG)
-  cerr << "WRITING DONE!" << endl;
+  std::cerr << "WriteInXmlFile : WRITING DONE!" << std::endl;
 #endif
 }
 
@@ -328,37 +331,66 @@ void ResourcesManager_cpp::WriteInXmlFile()
  */ 
 //=============================================================================
 
-const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFile()
+const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFiles()
 {
-  SALOME_ResourcesCatalog_Handler* handler =
-    new SALOME_ResourcesCatalog_Handler(_resourcesList, _resourcesBatchList);
+  // Nettoyage des variables (est-ce vraiment utile ?)
+  _resourcesList.clear();
+  _resourcesBatchList.clear();
 
-  const char* aFilePath = _path_resources.c_str();
-  FILE* aFile = fopen(aFilePath, "r");
-  
-  if (aFile != NULL)
+  // On parse tous les fichiers
+  for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it)
+  {
+    MapOfParserResourcesType _resourcesList_tmp;
+    MapOfParserResourcesType _resourcesBatchList_tmp;
+    SALOME_ResourcesCatalog_Handler* handler =
+      new SALOME_ResourcesCatalog_Handler(_resourcesList_tmp, _resourcesBatchList_tmp);
+    const char* aFilePath = (*_path_resources_it).c_str();
+    FILE* aFile = fopen(aFilePath, "r");
+    if (aFile != NULL)
     {
       xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
-      
       if (aDoc != NULL)
+      {
        handler->ProcessXmlDocument(aDoc);
-#if defined(_DEBUG_) || defined(_DEBUG)
+
+       // adding new resources to the file
+       for (MapOfParserResourcesType_it i = _resourcesList_tmp.begin(); i != _resourcesList_tmp.end(); ++i)
+       {
+         MapOfParserResourcesType_it j = _resourcesList.find(i->first);
+         if (j == _resourcesList.end())
+         {
+           _resourcesList[i->first] = i->second;
+         }
+         else
+         {
+           std::cerr << "ParseXmlFiles Warning, to resource with the same name was found, taking the first declaration : " << i->first << std::endl;
+         }
+       }
+       for (MapOfParserResourcesType_it i = _resourcesBatchList_tmp.begin(); i != _resourcesBatchList_tmp.end(); ++i)
+       {
+         MapOfParserResourcesType_it j = _resourcesBatchList.find(i->first);
+         if (j == _resourcesBatchList.end())
+         {
+           _resourcesBatchList[i->first] = i->second;
+         }
+         else
+         {
+           std::cerr << "ParseXmlFiles Warning, to resource with the same name was found, taking the first declaration : " << i->first << std::endl;
+         }
+       }
+      }
       else
-       cerr << "ResourcesManager_cpp: could not parse file "<< aFilePath << endl;
-#endif
-      
+       std::cerr << "ResourcesManager_cpp: could not parse file " << aFilePath << std::endl;
       // Free the document
       xmlFreeDoc(aDoc);
-
       fclose(aFile);
     }
-#if defined(_DEBUG_) || defined(_DEBUG)
-  else
-    cerr << "ResourcesManager_cpp: file "<<aFilePath<<" is not readable." << endl;
-#endif
-  
-  delete handler;
+    else
+      std::cerr << "ResourcesManager_cpp: file " << aFilePath << " is not readable." << std::endl;
 
+    delete handler;
+  }
   return _resourcesList;
 }
 
index cc065929c26f611bbba1691a19cb6055e31bdf1e..d736036aa8e961a6a6cf5b2b7fa52644ccff315b 100644 (file)
@@ -27,6 +27,7 @@
 #include <string>
 #include <fstream>
 #include <vector>
+#include <list>
 #include "SALOME_ResourcesCatalog_Parser.hxx"
 #include "SALOME_ResourcesCatalog_Handler.hxx"
 #include "SALOME_LoadRateManager.hxx"
@@ -82,9 +83,9 @@ class RESOURCESMANAGER_EXPORT ResourcesManager_cpp
 
     void DeleteResourceInCatalog(const char *hostname);
 
-    void WriteInXmlFile();
+    void WriteInXmlFile(std::string & xml_file);
 
-    const MapOfParserResourcesType& ParseXmlFile();
+    const MapOfParserResourcesType& ParseXmlFiles();
 
     const MapOfParserResourcesType& GetList() const;
 
@@ -101,13 +102,14 @@ class RESOURCESMANAGER_EXPORT ResourcesManager_cpp
       throw(ResourcesException);
 
     //! will contain the path to the ressources catalog
-    std::string _path_resources;
+    std::list<std::string> _path_resources;
+    std::list<std::string>::iterator _path_resources_it;
 
     //! will contain the informations on the data type catalog(after parsing)
     MapOfParserResourcesType _resourcesList;
 
     //! will contain the informations on the data type catalog(after parsing)
-    MapOfParserResourcesType _resourcesBatchList;
+MapOfParserResourcesType _resourcesBatchList;
 
     SALOME_LoadRateManager _dynamicResourcesSelecter;
 
index a80bc83bfe72d3fa5f97dcaa75fb8b1298648b60..1032e8fa4aef7c9902dfb4153f8e56252c3f7c17 100755 (executable)
@@ -106,7 +106,7 @@ void SALOME_ResourcesCatalog_Handler::ProcessXmlDocument(xmlDocPtr theDoc)
   xmlNodePtr aCurNode = xmlDocGetRootElement(theDoc);
 
   aCurNode = aCurNode->xmlChildrenNode;
-  
   // Processing the document nodes
   while(aCurNode != NULL)
     {
index 2d723b5800b01545316fdb0eaa5d29c712d889c9..b8767a6da2d56141b66eeee6425917fd86c9d852 100755 (executable)
@@ -92,5 +92,6 @@ struct RESOURCESMANAGER_EXPORT ParserResourcesType
   };
 
 typedef std::map<std::string, ParserResourcesType> MapOfParserResourcesType;
+typedef std::map<std::string, ParserResourcesType>::iterator MapOfParserResourcesType_it;
 
 #endif //SALOME_RESOURCES_CATALOG_PARSER