]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMPackage.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMPackage.cpp
index 0f001bbde823b9733887cb11a3a282dc994b29ef..9a296e93cdbb090ff5247a37143b41afbfaa3337 100644 (file)
@@ -37,6 +37,7 @@
 #include <fstream>
 #include <sstream>
 #include <algorithm>
+#include <boost/regex.hpp>
 
 #include "creaWx.h"
 #include "wx/dir.h"
@@ -76,7 +77,7 @@ modelCDMPackage::modelCDMPackage(modelCDMIProjectTreeNode* parent, const std::st
           if(wordBits[0] == "BBTK_PACKAGE_NAME")
             {
               word = wordBits[1];
-              for (int i = 2; i < wordBits.size(); i++)
+              for (int i = 2; i < (int)(wordBits.size()); i++)
                 {
                   word += " " + wordBits[i];
                 }
@@ -88,7 +89,7 @@ modelCDMPackage::modelCDMPackage(modelCDMIProjectTreeNode* parent, const std::st
           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
             {
               word = wordBits[1];
-              for (int i = 2; i < wordBits.size(); i++)
+              for (int i = 2; i < (int)(wordBits.size()); i++)
                 {
                   word += " " + wordBits[i];
                 }
@@ -100,7 +101,7 @@ modelCDMPackage::modelCDMPackage(modelCDMIProjectTreeNode* parent, const std::st
           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
             {
               word = wordBits[1];
-              for (int i = 2; i < wordBits.size(); i++)
+              for (int i = 2; i < (int)(wordBits.size()); i++)
                 {
                   word += " " + wordBits[i];
                 }
@@ -214,7 +215,7 @@ bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& resul
   std::vector<std::string> words;
   CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
   std::string authorsReal = words[0];
-  for (int i = 1; i < words.size(); i++)
+  for (int i = 1; i < (int)(words.size()); i++)
     {
       authorsReal += "/" + words[i];
     }
@@ -244,7 +245,12 @@ bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& resul
   in.close();
   out.close();
   //delete old file and rename new file
+#ifdef _WIN32
+  std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
+#else
   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
+#endif
+  
   if(system(renameCommand.c_str()))
     {
       result = new std::string("An error occurred while running '" + renameCommand + "'.");
@@ -296,7 +302,12 @@ bool modelCDMPackage::SetVersion(const std::string& version, std::string*& resul
   in.close();
   out.close();
   //delete old file and rename new file
+#ifdef _WIN32
+  std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
+#else
   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
+#endif
+  
   if(system(renameCommand.c_str()))
     {
       result = new std::string("An error occurred while running '" + renameCommand + "'.");
@@ -312,7 +323,7 @@ bool modelCDMPackage::SetDescription(const std::string& description, std::string
   std::vector<std::string> words;
   CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
   std::string descriptionReal = words[0];
-  for (int i = 1; i < words.size(); i++)
+  for (int i = 1; i < (int)(words.size()); i++)
     {
       descriptionReal += " " + words[i];
     }
@@ -342,7 +353,12 @@ bool modelCDMPackage::SetDescription(const std::string& description, std::string
   in.close();
   out.close();
   //delete old file and rename new file
+#ifdef _WIN32
+  std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
+#else
   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
+#endif
+  
   if(system(renameCommand.c_str()))
     {
       result = new std::string("An error occurred while running '" + renameCommand + "'.");
@@ -395,7 +411,7 @@ const bool modelCDMPackage::Refresh(std::string*& result)
           if(wordBits[0] == "BBTK_PACKAGE_NAME")
             {
               word = wordBits[1];
-              for (int i = 2; i < wordBits.size(); i++)
+              for (int i = 2; i < (int)(wordBits.size()); i++)
                 {
                   word += " " + wordBits[i];
                 }
@@ -407,7 +423,7 @@ const bool modelCDMPackage::Refresh(std::string*& result)
           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
             {
               word = wordBits[1];
-              for (int i = 2; i < wordBits.size(); i++)
+              for (int i = 2; i < (int)(wordBits.size()); i++)
                 {
                   word += " " + wordBits[i];
                 }
@@ -419,7 +435,7 @@ const bool modelCDMPackage::Refresh(std::string*& result)
           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
             {
               word = wordBits[1];
-              for (int i = 2; i < wordBits.size(); i++)
+              for (int i = 2; i < (int)(wordBits.size()); i++)
                 {
                   word += " " + wordBits[i];
                 }
@@ -484,7 +500,7 @@ const bool modelCDMPackage::Refresh(std::string*& result)
 
               //check if folder already exist
               bool found = false;
-              for (int i = 0; !found && i < this->children.size(); i++)
+              for (int i = 0; !found && i < (int)(this->children.size()); i++)
                 {
                   if (this->children[i]->GetName() == stdfileName)
                     {
@@ -529,7 +545,7 @@ const bool modelCDMPackage::Refresh(std::string*& result)
           else
             {
               bool found = false;
-              for (int i = 0; !found && i < this->children.size(); i++)
+              for (int i = 0; !found && i < (int)(this->children.size()); i++)
                 {
                   if (this->children[i]->GetName() == stdfileName)
                     {
@@ -556,7 +572,7 @@ const bool modelCDMPackage::Refresh(std::string*& result)
       this->src = NULL;
     }
 
-  for (int i = 0; i < checked.size(); i++)
+  for (int i = 0; i < (int)(checked.size()); i++)
     {
       if(!checked[i])
         {
@@ -594,7 +610,7 @@ void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
                 {
                   word = words[0];
                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
-                  for (int i = 0; i < words.size(); i++)
+                  for (int i = 0; i < (int)(words.size()); i++)
                     {
                       if(words[i].substr(0,2) == "//")
                         break;
@@ -655,7 +671,7 @@ void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
                     }
                   else if (words[0] == "${BBTK_PACKAGE_NAME}_INCLUDE_DIRS")
                     {
-                      for (int i = 1; i < words.size(); i++)
+                      for (int i = 1; i < (int)(words.size()); i++)
                         {
                           if(words[i].substr(0,2) == "${" || words[i].substr(0,2) == "..")
                           properties["package " + this->name + " dir " + words[i]] = true;
@@ -663,7 +679,7 @@ void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
                     }
                   else if (words[0] == "${BBTK_PACKAGE_NAME}_LIBS")
                     {
-                      for (int i = 1; i < words.size(); i++)
+                      for (int i = 1; i < (int)(words.size()); i++)
                         {
                           properties["package " + this->name + " lib " + words[i]] = true;
                         }
@@ -674,3 +690,174 @@ 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_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;
+  res["GDCM"] = false;
+  res["GDCM_VTK"] = false;
+  res["GSMIS"] = false;
+  res["WxWidgets"] = false;
+  res["KWWidgets"] = 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)*\\$\\{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::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::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 modelCDMPackage::Set3rdPartyLibrary(const std::string& library_name,
+    const bool& toInclude)
+{
+  std::map<std::string, std::string> correspondence;
+
+  correspondence["VTK"] = "_USE_VTK";
+  correspondence["ITK"] = "_USE_ITK";
+  correspondence["GDCM"] = "_USE_GDCM";
+  correspondence["GDCM_VTK"] = "_USE_GDCM_VTK";
+  correspondence["GSMIS"] = "_USE_GSMIS";
+  correspondence["WxWidgets"] = "_USE_WXWIDGETS";
+  correspondence["KWWidgets"] = "_USE_KWWIDGETS";
+
+  if (correspondence.find(library_name) != correspondence.end())
+    {
+      std::string library_command = correspondence[library_name];
+//      std::cout << "found correspondence " << library_command << std::endl;
+//      std::cout.flush();
+      if (this->HasCMakeLists())
+        {
+          std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
+          std::string resCMfile = "";
+          bool found = false;
+
+//          std::cout << "found cmakefile: " << CMfile << std::endl;
+//          std::cout.flush();
+
+          try {
+//            std::cout << "first regex" << std::endl;
+//            std::cout.flush();
+            boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
+
+            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;
+            if(boost::regex_search(start, end, what, expression, flags))
+              {
+//                std::cout << "found " << what.str() << std::endl;
+//                std::cout.flush();
+                found = true;
+                resCMfile += what.prefix().str();
+                if (toInclude)
+                  resCMfile += what.str();
+                else
+                  resCMfile += "#" + what.str();
+                resCMfile += what.suffix().str();
+
+                return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
+              }
+            else
+              {
+//                std::cout << "second regex" << std::endl;
+//                std::cout.flush();
+                boost::regex expression("^\\h*#\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
+                if(boost::regex_search(start, end, what, expression, flags))
+                  {
+                    found = true;
+                    resCMfile += what.prefix().str();
+                    if(toInclude)
+                      {
+                        std::string dete = what[0].str();
+                        for (int i = 0; i < dete.size(); ++i) {
+                          if (dete[i] != '#')
+                            resCMfile.push_back(dete[i]);
+                          if (dete[i] == 'S')
+                            {
+                              resCMfile += dete.substr(i+1);
+                              break;
+                            }
+                        }
+                      }
+                    else
+                      resCMfile += what.str();
+
+                    resCMfile += what.suffix().str();
+                    return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
+                  }
+                else
+                  {
+//                    std::cout << "third regex" << std::endl;
+//                    std::cout.flush();
+                    boost::regex expression("^\\h*#\\h*UNCOMMENT EACH LIBRARY NEEDED \\(WILL BE FOUND AND USED AUTOMATICALLY\\)[^\\n]*\\n");
+                    if(boost::regex_search(start, end, what, expression, flags))
+                      {
+                        found = true;
+                        resCMfile += what.prefix().str();
+                        resCMfile += what.str();
+                        if(toInclude)
+                          {
+                            resCMfile += "SET(${BBTK_PACKAGE_NAME}"+ library_command +"  ON)\n";
+                          }
+                        resCMfile += what.suffix().str();
+                        return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
+                      }
+                  }
+              }
+          } catch (boost::bad_expression& e) {
+            std::cout << "bad regex: " << e.what() << std::endl;
+            std::cout.flush();
+          }
+        }
+    }
+  return false;
+}
+
+std::map<std::string, bool> modelCDMPackage::GetCustomLibraries()
+{
+  std::map<std::string, bool> res;
+  res["Test"] = false;
+  return res;
+}
+
+bool modelCDMPackage::SetCustomLibrary(const std::string& library_name,
+    const bool& toInclude)
+{
+  return false;
+}