]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMProject.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMProject.cpp
index c39463376e247080cecea63f49a45723ac5e4de9..dff1b63728b2fe054d5514cd4d4e9d430b717a06 100644 (file)
@@ -40,6 +40,7 @@
 #include <algorithm>
 #include <fstream>
 #include <ctime>
+#include <boost/regex.hpp>
 
 #include "CDMUtilities.h"
 #include "creaWx.h"
@@ -1100,3 +1101,206 @@ bool modelCDMProject::SetPackageInclude(const std::string& package_name, const b
     }
   return false;
 }
+
+std::map<std::string, bool> modelCDMProject::Get3rdPartyLibraries()
+{
+  std::map<std::string, std::string> correspondence;
+  correspondence["USE_CREA"] = "Crea";
+  correspondence["USE_GDCM"] = "GDCM";
+  correspondence["USE_GDCM_VTK"] = "GDCM_VTK";
+  correspondence["USE_GDCM2"] = "GDCM2";
+  correspondence["USE_WXWIDGETS"] = "WxWidgets";
+  correspondence["USE_KWWIDGETS"] = "KWWidgets";
+  correspondence["USE_VTK"] = "VTK";
+  correspondence["USE_ITK"] = "ITK";
+  correspondence["USE_BOOST"] = "Boost";
+
+  std::map<std::string, bool> res;
+  res["Crea"] = false;
+  res["GDCM"] = false;
+  res["GDCM_VTK"] = false;
+  res["GDCM2"] = false;
+  res["WxWidgets"] = false;
+  res["KWWidgets"] = false;
+  res["VTK"] = false;
+  res["ITK"] = false;
+  res["Boost"] = false;
+
+  if (this->HasCMakeLists())
+    {
+      std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
+
+      boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*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::cout << what[0].str() << std::endl;
+          boost::regex expression1 = boost::regex("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::string dete = what1.str();
+              CDMUtilities::normalizeStr(dete);
+              std::cout << dete << std::endl;
+              if(correspondence.find(dete) != correspondence.end())
+                res[correspondence[dete]] = true;
+            }
+          start = what[0].second;
+        }
+    }
+
+  return res;
+}
+
+bool modelCDMProject::Set3rdPartyLibrary(const std::string& library_name,
+    const bool& toInclude)
+{
+  std::map<std::string, std::string> correspondence;
+  correspondence["Crea"] = "USE_CREA";
+  correspondence["GDCM"] = "USE_GDCM";
+  correspondence["GDCM_VTK"] = "USE_GDCM_VTK";
+  correspondence["GDCM2"] = "USE_GDCM2";
+  correspondence["WxWidgets"] = "USE_WXWIDGETS";
+  correspondence["KWWidgets"] = "USE_KWWIDGETS";
+  correspondence["VTK"] = "USE_VTK";
+  correspondence["ITK"] = "USE_ITK";
+  correspondence["Boost"] = "USE_BOOST";
+
+  if (correspondence.find(library_name) != correspondence.end())
+    {
+      std::string library_command = correspondence[library_name];
+
+      if (this->HasCMakeLists())
+        {
+          std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
+          std::string resCMfile = "";
+
+          std::string::const_iterator start, end;
+          boost::match_results<std::string::const_iterator> what, tmp;
+          boost::match_flag_type flags = boost::match_default;
+          bool found = false;
+
+          start = CMfile.begin();
+          end = CMfile.end();
+
+          //search for existing inclusions
+          boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
+          while(boost::regex_search(start, end, what, expression, flags))
+            {
+              found = true;
+              resCMfile += what.prefix().str();
+              if(!toInclude)
+                {
+                  std::string dete = what.str();
+                  int pos = dete.find("ON",0);
+                  dete.replace(pos, 2, "OFF");
+                  resCMfile += dete;
+                }
+              else
+                resCMfile += what.str();
+              tmp = what;
+              start = what[0].second;
+            }
+
+          if (found)
+            resCMfile += tmp.suffix().str();
+          else
+            {
+              start = CMfile.begin();
+              end = CMfile.end();
+
+              //search for existing exclusions
+              boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+OFF\\s*\\)");
+              while(boost::regex_search(start, end, what, expression, flags))
+                {
+                  found = true;
+                  resCMfile += what.prefix().str();
+                  if(toInclude)
+                    {
+                      std::string dete = what.str();
+                      int pos = dete.find("OFF",0);
+                      dete.replace(pos, 3, "ON");
+                      resCMfile += dete;
+                    }
+                  else
+                    resCMfile += what.str();
+                  tmp = what;
+                  start = what[0].second;
+                }
+
+              if (found)
+                resCMfile += tmp.suffix().str();
+              else
+                {
+                  start = CMfile.begin();
+                  end = CMfile.end();
+
+                  //search for existing commented inclusions
+                  expression = boost::regex("^\\h*#+\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
+                  while(boost::regex_search(start, end, what, expression, flags))
+                    {
+                      found = true;
+                      resCMfile += what.prefix().str();
+                      if(toInclude)
+                        {
+                          std::string dete = what.str();
+                          for (int i = 0; i < dete.size(); ++i) {
+                            if(dete[i] == '#')
+                              {
+                                dete.erase(i,1);
+                                i--;
+                              }
+                          }
+                          resCMfile += dete;
+                        }
+                      else
+                        resCMfile += what.str();
+
+                      tmp = what;
+                      start = what[0].second;
+                    }
+
+                  if (found)
+                    resCMfile += tmp.suffix().str();
+                  else
+                    {
+                      if(toInclude)
+                        {
+                          start = CMfile.begin();
+                          end = CMfile.end();
+
+                          //search for position to insert
+                          expression = boost::regex("^\\h*#\\h*Libraries\\/tools used\\h*$");
+                          if(boost::regex_search(start, end, what, expression, flags))
+                            {
+                              found = true;
+                              resCMfile += what.prefix().str();
+                              resCMfile += what.str();
+                              resCMfile += "\nSET(" + library_command + " ON)";
+                              resCMfile += what.suffix().str();
+                            }
+                        }
+                      else
+                        {
+                          found = true;
+                        }
+                    }
+                }
+            }
+          if (!found) {
+            return false;
+          }
+          else
+            return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
+        }
+    }
+
+  return false;
+}