]> Creatis software - crea.git/commitdiff
Feature #1711 CreaDevManager application implementation
authorDaniel Gonzalez <daniel.gonzalez@creatis.insa-lyon.fr>
Tue, 9 Apr 2013 16:39:56 +0000 (18:39 +0200)
committerDaniel Gonzalez <daniel.gonzalez@creatis.insa-lyon.fr>
Tue, 9 Apr 2013 16:39:56 +0000 (18:39 +0200)
Feature: Now including and excluding libraries in CMakeLists files for: Applications, Libraries.
Feature: Read included libraries in CMakeLists files for Packages. (Not writing, just reading)

lib/creaDevManagerLib/modelCDMAppli.cpp
lib/creaDevManagerLib/modelCDMApplication.cpp
lib/creaDevManagerLib/modelCDMLib.cpp
lib/creaDevManagerLib/modelCDMLibrary.cpp
lib/creaDevManagerLib/modelCDMPackage.cpp
lib/creaDevManagerLib/modelCDMProject.cpp

index 7bcc4a26a88ca7882d126ca2758004ebf862b50f..c66cbb871beb6deabfe5d74d36f31cbf7ee1d203 100644 (file)
@@ -486,118 +486,92 @@ void modelCDMAppli::CheckStructure(std::map<std::string, bool>& properties)
 
 bool modelCDMAppli::IsApplicationIncluded(const std::string& application_name)
 {
-  if(this->HasCMakeLists())
-    {
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
-        {
-          std::string line;
-          while(!CMFile.eof())
-            {
-              std::getline(CMFile, line);
-              while(line[0]==' ')
-                line.erase(0,1);
-              if(line[0] != '#')
-                {
-                  std::vector<std::string> lineSeg;
-                  CDMUtilities::splitter::split(lineSeg,line,"()",CDMUtilities::splitter::no_empties);
-                  if(lineSeg.size() > 0 && lineSeg[0] == "ADD_SUBDIRECTORY" && lineSeg[1] == application_name)
-                    {
-                      CMFile.close();
-                      return true;
-                    }
-                }
-            }
-          CMFile.close();
-        }
-    }
-  return false;
+  if (this->HasCMakeLists())
+      {
+        CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+        for (int i = 0; i < cmlFile.size(); ++i)
+          {
+            if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
+              {
+                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] != '#')
+                      {
+                        if (application_name == cmlFile[i].second[pos])
+                          return true;
+                        break;
+                      }
+                    pos++;
+                  }
+              }
+          }
+      }
+    return false;
 }
 
 bool modelCDMAppli::SetApplicationInclude(const std::string& application_name, const bool& toInclude)
 {
   if (this->HasCMakeLists())
     {
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+
+      bool found = false;
+
+      for (int i = 0; i < cmlFile.size(); ++i)
         {
-          std::stringstream outs;
-          std::string line;
-          bool found = false;
-          while(!CMFile.eof())
+          if(toInclude && cmlFile[i].first == "comment")
             {
-              std::getline(CMFile, line);
-              if (CMFile.eof()) {
-                break;
-              }
-              if(line != "")
+              std::vector<std::string> segments;
+              std::string line = cmlFile[i].second[0];
+              while(line[0] == '#')
+                line.erase(0,1);
+
+              CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
+              if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == application_name)
                 {
-                  std::vector<std::string> segs;
-                  CDMUtilities::splitter::split(segs, line, " ", CDMUtilities::splitter::no_empties);
-                  //is comment
-                  if(segs.size() > 0 && segs[0][0] == '#')
-                    {
-                      if(toInclude)
-                        {
-                          CDMUtilities::splitter::split(segs, line, " #()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == application_name)
-                            {
-                              found = true;
-                              outs << "ADD_SUBDIRECTORY(" << application_name << ")\n";
-                            }
-                          else
-                            outs << line << "\n";
-                        }
-                      else
-                        {
-                          outs << line << "\n";
-                        }
-                    }
-                  //is not comment
-                  else
+                  found = true;
+                  while(cmlFile[i].second[0][0] == '#')
+                    cmlFile[i].second[0].erase(0,1);
+                }
+            }
+          else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
+            {
+              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] != '#')
                     {
-                      if (segs.size() > 0 && !toInclude)
-                        {
-                          CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == application_name)
-                            {
-                              outs << "#" << line << "\n";
-                            }
-                          else
-                            {
-                              outs << line << "\n";
-                            }
-                        }
-                      else
+                      if (application_name == cmlFile[i].second[pos])
                         {
-                          CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == application_name)
+                          found = true;
+                          if (!toInclude)
                             {
-                              found = true;
+                              cmlFile[i].first = "comment";
+                              cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
+                              while (cmlFile[i].second.size() > 1)
+                                {
+                                  cmlFile[i].second[0] += cmlFile[i].second[1];
+                                  cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
+                                }
+
                             }
-                          outs << line << "\n";
                         }
+                      break;
                     }
-                }
-              else
-                {
-                  outs << "\n";
+                  pos++;
                 }
             }
-
-          CMFile.close();
-
-          if(!found && toInclude)
-            outs << "ADD_SUBDIRECTORY(" << application_name << ")\n";
-
-          std::ofstream CMFileOut(this->CMakeLists->GetPath().c_str());
-          if (CMFileOut.is_open())
-            {
-              CMFileOut << outs.rdbuf();
-              CMFileOut.close();
-              return true;
-            }
         }
+      if (!found && toInclude)
+        {
+          CDMUtilities::syntaxElement element;
+          element.first = "command";
+          element.second.push_back("ADD_SUBDIRECTORY(" + application_name + ")");
+          cmlFile.push_back(element);
+        }
+
+      return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
     }
   return false;
 }
index c15bedf6c2cb04bdf49cd95282d5a03209b1be4e..0ee18a913fa037ed71e1065488b54f6cb5785d57 100644 (file)
@@ -534,57 +534,41 @@ std::map<std::string, bool> modelCDMApplication::Get3rdPartyLibraries()
   res["GDCM"] = false;
   res["Boost"] = false;
 
-  if(this->HasCMakeLists())
+  if (this->HasCMakeLists())
     {
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+      // look at every syntax element
+      for (int i = 0; i < cmlFile.size(); ++i)
         {
-          std::string line;
-          while(!CMFile.eof())
+          // if the element is a command and is a SET command
+          if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET")
             {
-              std::getline(CMFile, line, '(');
-
-              std::vector<std::string> lineSeg;
-              CDMUtilities::splitter::split(lineSeg,line,"\n ",CDMUtilities::splitter::no_empties);
-
-              if(lineSeg.size() > 0 && lineSeg[lineSeg.size()-1] == "SET")
+              // search first parameter
+              int pos = 1;
+              while (pos < cmlFile[i].second.size())
                 {
-                  //std::cout << "found set" << std::endl;
-                  std::getline(CMFile, line, ')');
-                  //std::cout << line << std::endl;
-                  CDMUtilities::splitter::split(lineSeg,line,"\n",CDMUtilities::splitter::no_empties);
-                  if(lineSeg.size() > 0)
+                  // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES
+                  if (cmlFile[i].second[pos] == "${EXE_NAME}_LINK_LIBRARIES")
                     {
-                      //std::cout << lineSeg[0] << std::endl;
-                      for(int i = 0; i < lineSeg[0].size(); ++i)
-                        if (lineSeg[0][i] == ' ')
-                          {
-                            lineSeg[0].erase(i,1);
-                            --i;
-                          }
-                      //std::cout << lineSeg[0] << std::endl;
-                      if (lineSeg[0] == "${EXE_NAME}_LINK_LIBRARIES")
+                      pos++;
+                      // look for all the third party libraries included
+                      while (pos < cmlFile[i].second.size())
                         {
-                          //std::cout << "link" << std::endl;
-                          for (int l = 1; l < lineSeg.size(); ++l)
+                          if (cmlFile[i].second[pos][0] == '$' && correspondence.find(cmlFile[i].second[pos]) != correspondence.end())
                             {
-                              for(int i = 0;i < lineSeg[l].size(); i++)
-                                if (lineSeg[l][i] == ' ')
-                                  {
-                                    lineSeg[l].erase(i,1);
-                                    i--;
-                                  }
-                              if(lineSeg[l].size() > 0 && lineSeg[l][0] == '$')
-                                {
-                                  //std::cout << "found " << lineSeg[l] << std::endl;
-                                  res[correspondence[lineSeg[l]]] = true;
-                                }
+                              res[correspondence[cmlFile[i].second[pos]]] = true;
                             }
+                          pos++;
                         }
                     }
+                  // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command
+                  else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')')
+                    {
+                      break;
+                    }
+                  pos++;
                 }
             }
-          CMFile.close();
         }
     }
   return res;
@@ -592,6 +576,104 @@ std::map<std::string, bool> modelCDMApplication::Get3rdPartyLibraries()
 
 bool modelCDMApplication::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude)
 {
+  std::map<std::string, std::string> correspondence;
+
+  correspondence["Crea"] = "${crea_LIBRARIES}";
+  correspondence["WxWidgets"] = "${WXWIDGETS_LIBRARIES}";
+  correspondence["KWWidgets"] = "${KWWidgets_LIBRARIES}";
+  correspondence["VTK"] = "${VTK_LIBRARIES}";
+  correspondence["ITK"] = "${ITK_LIBRARIES}";
+  correspondence["GDCM"] = "${GDCM_LIBRARIES}";
+  correspondence["Boost"] = "${BOOST_LIBRARIES}";
+
+  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());
+
+          // 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())
+                    {
+                      // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES
+                      if (cmlFile[i].second[pos] == "${EXE_NAME}_LINK_LIBRARIES")
+                        {
+                          bool found = false;
+                          pos++;
+                          // look for all the third party libraries included
+                          while (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] != ")")
+                            {
+                              // if is library_command then found
+                              if (cmlFile[i].second[pos] == library_command)
+                                {
+                                  found = true;
+                                  //if toInclude is false the make it a comment
+                                  if (!toInclude)
+                                    {
+                                      cmlFile[i].second[pos] = "#" + cmlFile[i].second[pos];
+                                      std::cout << "library commented: " << library_name << std::endl;
+                                    }
+                                  break;
+                                }
+                              else if (toInclude && cmlFile[i].second[pos][0] == '#')
+                                {
+                                  std::vector<std::string> segments;
+                                  CDMUtilities::splitter::split(segments, cmlFile[i].second[pos], "#", CDMUtilities::splitter::no_empties);
+                                  if(segments.size())
+                                    {
+                                      for (int j = 0; j < segments[0].size(); ++j)
+                                        {
+                                          if(isspace(segments[0][j]))
+                                            {
+                                              segments[0].erase(j,1);
+                                              j--;
+                                            }
+                                        }
+
+                                      if(segments[0].size() && segments[0] == library_command)
+                                        {
+                                          found = true;
+                                          while(cmlFile[i].second[pos][0] == '#')
+                                            {
+                                              cmlFile[i].second[pos].erase(0,1);
+                                            }
+                                          std::cout << "library uncommented: " << library_name << std::endl;
+                                          break;
+                                        }
+                                    }
+                                }
+                              pos++;
+                            }
+
+                          //if the library was not found and is an inclusion then include it
+                          if (!found && toInclude && pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == ")")
+                            {
+                              cmlFile[i].second.insert(cmlFile[i].second.begin()+pos, library_command);
+                              cmlFile[i].second.insert(cmlFile[i].second.begin()+pos+1, "\n");
+                              std::cout << "library included: " << library_name << std::endl;
+                            }
+                        }
+                      // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command
+                      else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')')
+                        {
+                          break;
+                        }
+                      pos++;
+                    }
+                }
+            }
+
+          return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath(), cmlFile);
+        }
+    }
   return false;
 }
 
index 38cd30df188cdf8a06faf7c326f9272d7ad5380c..3dcf9b8786766ce64d0681718d4dc22f16a65f6b 100644 (file)
@@ -410,29 +410,25 @@ void modelCDMLib::CheckStructure(std::map<std::string, bool>& properties)
 
 bool modelCDMLib::IsLibraryIncluded(const std::string& library_name)
 {
-  if(this->HasCMakeLists())
+  if (this->HasCMakeLists())
     {
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+      for (int i = 0; i < cmlFile.size(); ++i)
         {
-          std::string line;
-          while(!CMFile.eof())
+          if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
             {
-              std::getline(CMFile, line);
-              while(line[0]==' ')
-                line.erase(0,1);
-              if(line[0] != '#')
+              int pos = 1;
+              while (pos < cmlFile[i].second.size())
                 {
-                  std::vector<std::string> lineSeg;
-                  CDMUtilities::splitter::split(lineSeg,line,"()",CDMUtilities::splitter::no_empties);
-                  if(lineSeg.size() > 0 && lineSeg[0] == "ADD_SUBDIRECTORY" && lineSeg[1] == library_name)
+                  if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
                     {
-                      CMFile.close();
-                      return true;
+                      if (library_name == cmlFile[i].second[pos])
+                        return true;
+                      break;
                     }
+                  pos++;
                 }
             }
-          CMFile.close();
         }
     }
   return false;
@@ -442,87 +438,64 @@ bool modelCDMLib::SetLibraryInclude(const std::string& library_name, const bool&
 {
   if (this->HasCMakeLists())
     {
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+
+      bool found = false;
+
+      for (int i = 0; i < cmlFile.size(); ++i)
         {
-          std::stringstream outs;
-          std::string line;
-          bool found = false;
-          while(!CMFile.eof())
+          if(toInclude && cmlFile[i].first == "comment")
             {
-              std::getline(CMFile, line);
-              if (CMFile.eof()) {
-                break;
-              }
-              if(line != "")
+              std::vector<std::string> segments;
+              std::string line = cmlFile[i].second[0];
+              while(line[0] == '#')
+                line.erase(0,1);
+
+              CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
+              if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == library_name)
                 {
-                  std::vector<std::string> segs;
-                  CDMUtilities::splitter::split(segs, line, " ", CDMUtilities::splitter::no_empties);
-                  //is comment
-                  if(segs.size() > 0 && segs[0][0] == '#')
-                    {
-                      if(toInclude)
-                        {
-                          CDMUtilities::splitter::split(segs, line, " #()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name)
-                            {
-                              found = true;
-                              outs << "ADD_SUBDIRECTORY(" << library_name << ")\n";
-                            }
-                          else
-                            outs << line << "\n";
-                        }
-                      else
-                        {
-                          outs << line << "\n";
-                        }
-                    }
-                  //is not comment
-                  else
+                  found = true;
+                  while(cmlFile[i].second[0][0] == '#')
+                    cmlFile[i].second[0].erase(0,1);
+                }
+            }
+          else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
+            {
+              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] != '#')
                     {
-                      if (segs.size() > 0 && !toInclude)
+                      if (library_name == cmlFile[i].second[pos])
                         {
-                          CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name)
+                          found = true;
+                          if (!toInclude)
                             {
-                              outs << "#" << line << "\n";
-                            }
-                          else
-                            {
-                              outs << line << "\n";
-                            }
-                        }
-                      else
-                        {
-                          CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name)
-                            {
-                              found = true;
+                              cmlFile[i].first = "comment";
+                              cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
+                              while (cmlFile[i].second.size() > 1)
+                                {
+                                  cmlFile[i].second[0] += cmlFile[i].second[1];
+                                  cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
+                                }
+
                             }
-                          outs << line << "\n";
                         }
+                      break;
                     }
+                  pos++;
                 }
-              else
-                {
-                  outs << "\n";
-                }
-
-            }
-
-          CMFile.close();
-
-          if(!found && toInclude)
-            outs << "ADD_SUBDIRECTORY(" << library_name << ")\n";
-
-          std::ofstream CMFileOut(this->CMakeLists->GetPath().c_str());
-          if (CMFileOut.is_open())
-            {
-              CMFileOut << outs.rdbuf();
-              CMFileOut.close();
-              return true;
             }
         }
+      if (!found && toInclude)
+        {
+          CDMUtilities::syntaxElement element;
+          element.first = "command";
+          element.second.push_back("ADD_SUBDIRECTORY(" + library_name + ")");
+          cmlFile.push_back(element);
+        }
+
+      return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
     }
   return false;
 }
index 9bac184430df0e36052796f23797df89e84b10da..fd0e08420b0236889bc8bfbea6ded9d2d3b181c0 100644 (file)
@@ -453,58 +453,41 @@ std::map<std::string, bool> modelCDMLibrary::Get3rdPartyLibraries()
   res["GDCM"] = false;
   res["Boost"] = false;
 
-  if(this->HasCMakeLists())
+  if (this->HasCMakeLists())
     {
-      CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+      // look at every syntax element
+      for (int i = 0; i < cmlFile.size(); ++i)
         {
-          std::string line;
-          while(!CMFile.eof())
+          // if the element is a command and is a SET command
+          if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET")
             {
-              std::getline(CMFile, line, '(');
-
-              std::vector<std::string> lineSeg;
-              CDMUtilities::splitter::split(lineSeg,line,"\n ",CDMUtilities::splitter::no_empties);
-
-              if(lineSeg.size() > 0 && lineSeg[lineSeg.size()-1] == "SET")
+              // search first parameter
+              int pos = 1;
+              while (pos < cmlFile[i].second.size())
                 {
-                  //std::cout << "found set" << std::endl;
-                  std::getline(CMFile, line, ')');
-                  //std::cout << line << std::endl;
-                  CDMUtilities::splitter::split(lineSeg,line,"\n",CDMUtilities::splitter::no_empties);
-                  if(lineSeg.size() > 0)
+                  // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES
+                  if (cmlFile[i].second[pos] == "${LIBRARY_NAME}_LINK_LIBRARIES")
                     {
-                      //std::cout << lineSeg[0] << std::endl;
-                      for(int i = 0; i < lineSeg[0].size(); ++i)
-                        if (lineSeg[0][i] == ' ')
-                          {
-                            lineSeg[0].erase(i,1);
-                            --i;
-                          }
-                      //std::cout << lineSeg[0] << std::endl;
-                      if (lineSeg[0] == "${LIBRARY_NAME}_LINK_LIBRARIES")
+                      pos++;
+                      // look for all the third party libraries included
+                      while (pos < cmlFile[i].second.size())
                         {
-                          //std::cout << "link" << std::endl;
-                          for (int l = 1; l < lineSeg.size(); ++l)
+                          if (cmlFile[i].second[pos][0] == '$' && correspondence.find(cmlFile[i].second[pos]) != correspondence.end())
                             {
-                              for(int i = 0;i < lineSeg[l].size(); i++)
-                                if (lineSeg[l][i] == ' ')
-                                  {
-                                    lineSeg[l].erase(i,1);
-                                    i--;
-                                  }
-                              if(lineSeg[l].size() > 0 && lineSeg[l][0] == '$')
-                                {
-                                  //std::cout << "found " << lineSeg[l] << std::endl;
-                                  res[correspondence[lineSeg[l]]] = true;
-                                }
+                              res[correspondence[cmlFile[i].second[pos]]] = true;
                             }
+                          pos++;
                         }
                     }
+                  // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command
+                  else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')')
+                    {
+                      break;
+                    }
+                  pos++;
                 }
             }
-          CMFile.close();
         }
     }
   return res;
@@ -512,6 +495,104 @@ std::map<std::string, bool> modelCDMLibrary::Get3rdPartyLibraries()
 
 bool modelCDMLibrary::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude)
 {
+  std::map<std::string, std::string> correspondence;
+
+  correspondence["Crea"] = "${crea_LIBRARIES}";
+  correspondence["WxWidgets"] = "${WXWIDGETS_LIBRARIES}";
+  correspondence["KWWidgets"] = "${KWWidgets_LIBRARIES}";
+  correspondence["VTK"] = "${VTK_LIBRARIES}";
+  correspondence["ITK"] = "${ITK_LIBRARIES}";
+  correspondence["GDCM"] = "${GDCM_LIBRARIES}";
+  correspondence["Boost"] = "${BOOST_LIBRARIES}";
+
+  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());
+
+          // 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())
+                    {
+                      // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES
+                      if (cmlFile[i].second[pos] == "${LIBRARY_NAME}_LINK_LIBRARIES")
+                        {
+                          bool found = false;
+                          pos++;
+                          // look for all the third party libraries included
+                          while (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] != ")")
+                            {
+                              // if is library_command then found
+                              if (cmlFile[i].second[pos] == library_command)
+                                {
+                                  found = true;
+                                  //if toInclude is false the make it a comment
+                                  if (!toInclude)
+                                    {
+                                      cmlFile[i].second[pos] = "#" + cmlFile[i].second[pos];
+                                      std::cout << "library commented: " << library_name << std::endl;
+                                    }
+                                  break;
+                                }
+                              else if (toInclude && cmlFile[i].second[pos][0] == '#')
+                                {
+                                  std::vector<std::string> segments;
+                                  CDMUtilities::splitter::split(segments, cmlFile[i].second[pos], "#", CDMUtilities::splitter::no_empties);
+                                  if(segments.size())
+                                    {
+                                      for (int j = 0; j < segments[0].size(); ++j)
+                                        {
+                                          if(isspace(segments[0][j]))
+                                            {
+                                              segments[0].erase(j,1);
+                                              j--;
+                                            }
+                                        }
+
+                                      if(segments[0].size() && segments[0] == library_command)
+                                        {
+                                          std::cout << "library uncommented: " << library_name << std::endl;
+                                          found = true;
+                                          while(cmlFile[i].second[pos][0] == '#')
+                                            {
+                                              cmlFile[i].second[pos].erase(0,1);
+                                            }
+                                          break;
+                                        }
+                                    }
+                                }
+                              pos++;
+                            }
+
+                          // if the library was not found and is an inclusion then include it.
+                          if (!found && toInclude && pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == ")")
+                            {
+                              cmlFile[i].second.insert(cmlFile[i].second.begin()+pos, library_command);
+                              cmlFile[i].second.insert(cmlFile[i].second.begin()+pos+1, "\n");
+                              std::cout << "library included: " << library_name << std::endl;
+                            }
+                        }
+                      // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command
+                      else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')')
+                        {
+                          break;
+                        }
+                      pos++;
+                    }
+                }
+            }
+
+          return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath(), cmlFile);
+        }
+    }
   return false;
 }
 
index 81fb90610b5ac2b5e23877905ac06d17e61c51e6..c6ea443d6d516e0e00b72c65cefe38f3c7f4ef79 100644 (file)
@@ -693,13 +693,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 +709,46 @@ 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())
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+      // look at every syntax element
+      for (int i = 0; i < cmlFile.size(); ++i)
         {
-          std::string line;
-          while(!CMFile.eof())
+          // if the element is a command and is a SET command
+          if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET")
             {
-              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")
+              // search first parameter
+              int pos = 1;
+              while (pos < cmlFile[i].second.size())
                 {
-                  std::getline(CMFile, line, ')');
-                  CDMUtilities::splitter::split(lineSeg,line,"\t\n",CDMUtilities::splitter::no_empties);
-                  if(lineSeg.size() > 0)
+                  if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][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())
+                      break;
+                    }
+                  pos++;
+                }
+              // if the first parameter is a third party statement
+              if (pos < cmlFile[i].second.size() && correspondence.find(cmlFile[i].second[pos]) != correspondence.end())
+                {
+                  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] != ')')
                         {
-                          res[correspondence[lineSeg[0]]] = true;
+                          break;
                         }
+                      pos++;
+                    }
+                  // if the second parameter is ON
+                  if (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == "ON")
+                    {
+                      res[correspondence[foundLibrary]] = true;
                     }
                 }
             }
-          CMFile.close();
         }
     }
   return res;
@@ -756,6 +757,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;
 }
 
index d26932d61504c65033393978a1ccdf5d69173be6..08cafc74f530d554663117f56d05106ee6c34d43 100644 (file)
@@ -1012,118 +1012,93 @@ void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
 
 bool modelCDMProject::IsPackageIncluded(const std::string& package_name)
 {
-  if(this->HasCMakeLists())
+  if (this->HasCMakeLists())
     {
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+      for (int i = 0; i < cmlFile.size(); ++i)
         {
-          std::string line;
-          while(!CMFile.eof())
+          if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
             {
-              std::getline(CMFile, line);
-              while(line[0]==' ')
-                line.erase(0,1);
-              if(line[0] != '#')
+              int pos = 1;
+              while (pos < cmlFile[i].second.size())
                 {
-                  std::vector<std::string> lineSeg;
-                  CDMUtilities::splitter::split(lineSeg,line,"()",CDMUtilities::splitter::no_empties);
-                  if(lineSeg.size() > 0 && lineSeg[0] == "ADD_SUBDIRECTORY" && lineSeg[1] == package_name)
+                  if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
                     {
-                      CMFile.close();
-                      return true;
+                      if (package_name == cmlFile[i].second[pos])
+                        return true;
+                      break;
                     }
+                  pos++;
                 }
             }
-          CMFile.close();
         }
     }
   return false;
+
 }
 
 bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude)
 {
   if (this->HasCMakeLists())
     {
-      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
-      if (CMFile.is_open())
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+
+      bool found = false;
+
+      for (int i = 0; i < cmlFile.size(); ++i)
         {
-          std::stringstream outs;
-          std::string line;
-          bool found = false;
-          while(!CMFile.eof())
+          if(toInclude && cmlFile[i].first == "comment")
             {
-              std::getline(CMFile, line);
-              if (CMFile.eof()) {
-                break;
-              }
-              if(line != "")
+              std::vector<std::string> segments;
+              std::string line = cmlFile[i].second[0];
+              while(line[0] == '#')
+                line.erase(0,1);
+
+              CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
+              if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name)
                 {
-                  std::vector<std::string> segs;
-                  CDMUtilities::splitter::split(segs, line, " ", CDMUtilities::splitter::no_empties);
-                  //is comment
-                  if(segs.size() > 0 && segs[0][0] == '#')
-                    {
-                      if(toInclude)
-                        {
-                          CDMUtilities::splitter::split(segs, line, " #()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == package_name)
-                            {
-                              found = true;
-                              outs << "ADD_SUBDIRECTORY(" << package_name << ")\n";
-                            }
-                          else
-                            outs << line << "\n";
-                        }
-                      else
-                        {
-                          outs << line << "\n";
-                        }
-                    }
-                  //is not comment
-                  else
+                  found = true;
+                  while(cmlFile[i].second[0][0] == '#')
+                    cmlFile[i].second[0].erase(0,1);
+                }
+            }
+          else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
+            {
+              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] != '#')
                     {
-                      if (segs.size() > 0 && !toInclude)
-                        {
-                          CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == package_name)
-                            {
-                              outs << "#" << line << "\n";
-                            }
-                          else
-                            {
-                              outs << line << "\n";
-                            }
-                        }
-                      else
+                      if (package_name == cmlFile[i].second[pos])
                         {
-                          CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
-                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == package_name)
+                          found = true;
+                          if (!toInclude)
                             {
-                              found = true;
+                              cmlFile[i].first = "comment";
+                              cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
+                              while (cmlFile[i].second.size() > 1)
+                                {
+                                  cmlFile[i].second[0] += cmlFile[i].second[1];
+                                  cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
+                                }
+
                             }
-                          outs << line << "\n";
                         }
+                      break;
                     }
-                }
-              else
-                {
-                  outs << "\n";
+                  pos++;
                 }
             }
-
-          CMFile.close();
-
-          if(!found && toInclude)
-            outs << "ADD_SUBDIRECTORY(" << package_name << ")\n";
-
-          std::ofstream CMFileOut(this->CMakeLists->GetPath().c_str());
-          if (CMFileOut.is_open())
-            {
-              CMFileOut << outs.rdbuf();
-              CMFileOut.close();
-              return true;
-            }
         }
+      if (!found && toInclude)
+        {
+          CDMUtilities::syntaxElement element;
+          element.first = "command";
+          element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")");
+          cmlFile.push_back(element);
+        }
+
+      return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
     }
   return false;
 }