]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMLib.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMLib.cpp
index e3c29023fffc04797962496f86d7f948d1fb2a00..48df51b85c29581095b42db809baa4ab4449cbe0 100644 (file)
@@ -91,6 +91,12 @@ modelCDMLib::modelCDMLib(modelCDMIProjectTreeNode* parent, const std::string& pa
       while (cont)
         {
           std::string stdfileName = crea::wx2std(fileName);
+          std::size_t fileTypePos = stdfileName.find_last_of(".");
+          std::string fileType;
+          if(fileTypePos != std::string::npos)
+            fileType = stdfileName.substr(fileTypePos);
+          else
+            fileType = "";
 
           //if CMakeLists, create CMakeLists
           if(stdfileName == "CMakeLists.txt")
@@ -98,6 +104,17 @@ modelCDMLib::modelCDMLib(modelCDMIProjectTreeNode* parent, const std::string& pa
               this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
               this->children.push_back(this->CMakeLists);
             }
+          //if is a code file, create modelCDMCodeFile
+          else if(
+              fileType == ".c" ||
+              fileType == ".cxx" ||
+              fileType == ".h" ||
+              fileType == ".cpp" ||
+              fileType == ".txx" ||
+              fileType == ".cmake" )
+            {
+              this->children.push_back(new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
+            }
           //if is an unknown file, create file
           else
             {
@@ -177,6 +194,15 @@ modelCDMLibrary* modelCDMLib::CreateLibrary(
       return NULL;
     }
 
+  //add library to lib CMakeLists
+  std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
+  if (out1.is_open())
+    {
+      out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
+      out1.close();
+    }
+
+
   //add library to model
   modelCDMLibrary* library = new modelCDMLibrary(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
   this->libraries.push_back(library);
@@ -259,6 +285,13 @@ const bool modelCDMLib::Refresh(std::string*& result)
       while (cont)
         {
           std::string stdfileName = crea::wx2std(fileName);
+          std::size_t fileTypePos = stdfileName.find_last_of(".");
+          std::string fileType;
+          if(fileTypePos != std::string::npos)
+            fileType = stdfileName.substr(fileTypePos);
+          else
+            fileType = "";
+
 
           //if CMakeLists, create CMakeLists
           if(stdfileName == "CMakeLists.txt")
@@ -293,8 +326,22 @@ const bool modelCDMLib::Refresh(std::string*& result)
 
               if(!found)
                 {
-                  modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
-                  this->children.push_back(file);
+                  //if is a code file, create modelCDMCodeFile
+                  if(
+                      fileType == ".c" ||
+                      fileType == ".cxx" ||
+                      fileType == ".h" ||
+                      fileType == ".cpp" ||
+                      fileType == ".txx" ||
+                      fileType == ".cmake" )
+                    {
+                      this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
+                    }
+                  else
+                    {
+                      modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
+                      this->children.push_back(file);
+                    }
                 }
             }
 
@@ -398,3 +445,95 @@ void modelCDMLib::CheckStructure(std::map<std::string, bool>& properties)
       this->libraries[i]->CheckStructure(properties);
     }
 }
+
+bool modelCDMLib::IsLibraryIncluded(const std::string& library_name)
+{
+  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 (library_name == cmlFile[i].second[pos])
+                        return true;
+                      break;
+                    }
+                  pos++;
+                }
+            }
+        }
+    }
+  return false;
+}
+
+bool modelCDMLib::SetLibraryInclude(const std::string& library_name, const bool& toInclude)
+{
+  if (this->HasCMakeLists())
+    {
+      CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
+
+      bool found = false;
+
+      for (int i = 0; i < cmlFile.size(); ++i)
+        {
+          if(toInclude && cmlFile[i].first == "comment")
+            {
+              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)
+                {
+                  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 (library_name == cmlFile[i].second[pos])
+                        {
+                          found = true;
+                          if (!toInclude)
+                            {
+                              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);
+                                }
+
+                            }
+                        }
+                      break;
+                    }
+                  pos++;
+                }
+            }
+        }
+      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;
+}