]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMPackage.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMPackage.cpp
index 81fb90610b5ac2b5e23877905ac06d17e61c51e6..c080ddd34c6abc857280395537e084344d34ca7f 100644 (file)
@@ -37,6 +37,7 @@
 #include <fstream>
 #include <sstream>
 #include <algorithm>
+#include <boost/regex.hpp>
 
 #include "creaWx.h"
 #include "wx/dir.h"
@@ -693,13 +694,13 @@ void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
 std::map<std::string, bool> modelCDMPackage::Get3rdPartyLibraries()
 {
   std::map<std::string, std::string> correspondence;
-  correspondence["${BBTK_PACKAGE_NAME}_USE_VTKON"] = "VTK";
-  correspondence["${BBTK_PACKAGE_NAME}_USE_ITKON"] = "ITK";
-  correspondence["${BBTK_PACKAGE_NAME}_USE_GDCMON"] = "GDCM";
-  correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM_VTKON"] = "GDCM_VTK";
-  correspondence["${BBTK_PACKAGE_NAME}_USE_GSMISON"] = "GSMIS";
-  correspondence["${BBTK_PACKAGE_NAME}_USE_WXWIDGETSON"] = "WxWidgets";
-  correspondence["${BBTK_PACKAGE_NAME}_USE_KWWIDGETSON"] = "KWWidgets";
+  correspondence["${BBTK_PACKAGE_NAME}_USE_VTK"] = "VTK";
+  correspondence["${BBTK_PACKAGE_NAME}_USE_ITK"] = "ITK";
+  correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM"] = "GDCM";
+  correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM_VTK"] = "GDCM_VTK";
+  correspondence["${BBTK_PACKAGE_NAME}_USE_GSMIS"] = "GSMIS";
+  correspondence["${BBTK_PACKAGE_NAME}_USE_WXWIDGETS"] = "WxWidgets";
+  correspondence["${BBTK_PACKAGE_NAME}_USE_KWWIDGETS"] = "KWWidgets";
   std::map<std::string, bool> res;
   res["VTK"] = false;
   res["ITK"] = false;
@@ -709,45 +710,33 @@ std::map<std::string, bool> modelCDMPackage::Get3rdPartyLibraries()
   res["WxWidgets"] = false;
   res["KWWidgets"] = false;
 
-  if(this->HasCMakeLists())
+  if (this->HasCMakeLists())
     {
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
+      std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
+
+      boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+\\s+ON");
+      std::string::const_iterator start, end;
+      start = CMfile.begin();
+      end = CMfile.end();
+      boost::match_results<std::string::const_iterator> what;
+      boost::match_flag_type flags = boost::match_default;
+      while(boost::regex_search(start, end, what, expression, flags))
         {
-          std::string line;
-          while(!CMFile.eof())
+          //std::cout << what[0].str() << std::endl;
+          boost::regex expression1 = boost::regex("\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+");
+          std::string::const_iterator start1, end1;
+          start1 = what[0].first;
+          end1 = what[0].second;
+          boost::match_results<std::string::const_iterator> what1;
+          if(boost::regex_search(start1, end1, what1, expression1, flags))
             {
-              std::getline(CMFile, line, '(');
-
-              std::vector<std::string> lineSeg;
-              CDMUtilities::splitter::split(lineSeg,line,"\n",CDMUtilities::splitter::no_empties);
-              for(int i = 0; lineSeg.size() > 0 && i < lineSeg[lineSeg.size()-1].size(); ++i)
-                if (lineSeg[lineSeg.size()-1][i] == ' ')
-                  {
-                    lineSeg[lineSeg.size()-1].erase(i,1);
-                    --i;
-                  }
-              if(lineSeg.size() > 0 && lineSeg[lineSeg.size()-1][0]!='#' && lineSeg[lineSeg.size()-1] == "SET")
-                {
-                  std::getline(CMFile, line, ')');
-                  CDMUtilities::splitter::split(lineSeg,line,"\t\n",CDMUtilities::splitter::no_empties);
-                  if(lineSeg.size() > 0)
-                    {
-                      for(int i = 0; i < lineSeg[0].size(); ++i)
-                        if (lineSeg[0][i] == ' ')
-                          {
-                            lineSeg[0].erase(i,1);
-                            --i;
-                          }
-
-                      if(lineSeg[0].size() > 0 && lineSeg[0][0] == '$' && correspondence.find(lineSeg[0]) != correspondence.end())
-                        {
-                          res[correspondence[lineSeg[0]]] = true;
-                        }
-                    }
-                }
+              std::string dete = what1.str();
+              CDMUtilities::normalizeStr(dete);
+              //std::cout << dete << std::endl;
+              if(correspondence.find(dete) != correspondence.end())
+                res[correspondence[dete]] = true;
             }
-          CMFile.close();
+          start = what[0].second;
         }
     }
   return res;
@@ -756,6 +745,89 @@ std::map<std::string, bool> modelCDMPackage::Get3rdPartyLibraries()
 bool modelCDMPackage::Set3rdPartyLibrary(const std::string& library_name,
     const bool& toInclude)
 {
+  std::map<std::string, std::string> correspondence;
+
+  correspondence["VTK"] = "${BBTK_PACKAGE_NAME}_USE_VTK";
+  correspondence["ITK"] = "${BBTK_PACKAGE_NAME}_USE_ITK";
+  correspondence["GDCM"] = "${BBTK_PACKAGE_NAME}_USE_GDCM";
+  correspondence["GDCM_VTK"] = "${BBTK_PACKAGE_NAME}_USE_GDCM_VTK";
+  correspondence["GSMIS"] = "${BBTK_PACKAGE_NAME}_USE_GSMIS";
+  correspondence["WxWidgets"] = "${BBTK_PACKAGE_NAME}_USE_WXWIDGETS";
+  correspondence["KWWidgets"] = "${BBTK_PACKAGE_NAME}_USE_KWWIDGETS";
+/*
+  if(correspondence.find(library_name) != correspondence.end())
+    {
+      std::string library_command = correspondence[library_name];
+      if (this->HasCMakeLists())
+        {
+          CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+          bool found = false;
+          // look at every syntax element
+          for (int i = 0; i < cmlFile.size(); ++i)
+            {
+              // if the element is a command and is a SET command
+              if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET")
+                {
+                  // search first parameter
+                  int pos = 1;
+                  while (pos < cmlFile[i].second.size())
+                    {
+                      if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')')
+                        {
+                          break;
+                        }
+                      pos++;
+                    }
+                  // if the first parameter is a third party statement
+                  if (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == library_command)
+                    {
+                      std::string foundLibrary = cmlFile[i].second[pos];
+                      // search for second parameter
+                      pos++;
+                      while (pos < cmlFile[i].second.size())
+                        {
+                          if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')')
+                            {
+                              break;
+                            }
+                          pos++;
+                        }
+                      // if the second parameter is ON
+                      if (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == "ON")
+                        {
+                          found = true;
+                          if (toInclude)
+                            {
+                              std::cout << "library already included: " << library_name << std::endl;
+                            }
+                          else
+                            {
+                              cmlFile[i].first = "comment";
+                              cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
+                              std::cout << "library include commented: " << library_name << std::endl;
+                            }
+                        }
+                      else if (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == "OFF")
+                        {
+                          found = true;
+                          if (toInclude)
+                            {
+                              cmlFile[i].second[pos] = "ON";
+                              std::cout << "library include activated: " << library_name << std::endl;
+                            }
+                          else
+                            {
+                              std::cout << "library include already disabled: " << library_name << std::endl;
+                            }
+                        }
+                    }
+                } else if (cmlFile[i].first == "comment") {
+                    //TODO: check if it is commented
+                }
+            }
+        }
+    }
+*/
   return false;
 }