]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMLib.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMLib.cpp
index 50b32f5e76c509fec9252a648bfc27286dcb46db..5cfc702a11d4aa945ac1b3940d5d323cd053641e 100644 (file)
 
 #include "modelCDMLib.h"
 
+#include <fstream>
+#include <sstream>
+#include <algorithm>
+
 #include "CDMUtilities.h"
 #include "creaWx.h"
 #include "wx/dir.h"
@@ -42,10 +46,12 @@ modelCDMLib::modelCDMLib()
 {
 }
 
-modelCDMLib::modelCDMLib(const std::string& path, const int& level)
+modelCDMLib::modelCDMLib(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
 {
+  std::cout << "creating lib\n";
+  this->parent = parent;
   this->type = wxDIR_DIRS;
-  this->name = "lib";
+  this->name = name;
   this->level = level;
   this->path = path;
 
@@ -56,56 +62,467 @@ modelCDMLib::modelCDMLib(const std::string& path, const int& level)
   std::string pathFixed(CDMUtilities::fixPath(path));
 
   this->libraries.clear();
-    wxDir dir(crea::std2wx((pathFixed).c_str()));
-    if (dir.IsOpened())
-      {
-        wxString fileName;
-        bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
-        while (cont)
-          {
-            std::string stdfileName = crea::wx2std(fileName);
-
-            modelCDMLibrary* library = new modelCDMLibrary(pathFixed + "/" + stdfileName, this->level + 1);
-            this->libraries.push_back(library);
-            this->children.push_back(library);
-
-            cont = dir.GetNext(&fileName);
-          }
-
-      }
-    this->SortChildren();
+  wxDir dir(crea::std2wx((pathFixed).c_str()));
+  if (dir.IsOpened())
+    {
+      wxString fileName;
+      //folders
+      bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
+      while (cont)
+        {
+          std::string stdfileName = crea::wx2std(fileName);
+
+          if(stdfileName != "template_lib")
+            {
+              modelCDMLibrary* library = new modelCDMLibrary(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
+              this->libraries.push_back(library);
+              this->children.push_back(library);
+            }
+          else
+            {
+              modelCDMFolder* folder = new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
+              this->children.push_back(folder);
+            }
+
+          cont = dir.GetNext(&fileName);
+        }
+      //files
+      cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
+      while (cont)
+        {
+          std::string stdfileName = crea::wx2std(fileName);
+
+          //if CMakeLists, create CMakeLists
+          if(stdfileName == "CMakeLists.txt")
+            {
+              this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
+              this->children.push_back(this->CMakeLists);
+            }
+          //if is an unknown file, create file
+          else
+            {
+              this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
+            }
+
+          cont = dir.GetNext(&fileName);
+        }
+    }
+  this->SortChildren();
+  std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
 }
 
 modelCDMLib::~modelCDMLib()
 {
-  for (int i = 0; i < this->libraries.size(); i++)
+}
+
+const std::vector<modelCDMLibrary*>& modelCDMLib::GetLibraries() const
+{
+  return this->libraries;
+}
+
+modelCDMLibrary* modelCDMLib::CreateLibrary(
+    const std::string& namein,
+    std::string*& result
+)
+{
+  std::vector<std::string> words;
+  CDMUtilities::splitter::split(words,namein," '/\\*\"%",CDMUtilities::splitter::no_empties);
+  std::string name;
+  for (int i = 0; i < (int)(words.size()); i++)
+    {
+         name += words[i];
+    }
+  if (name == "")
+    {
+      result = new std::string("The given name is not valid:  '/\\*\"% are forbidden.");
+         return NULL;
+    }
+  //copy template library folder with new name
+#ifdef _WIN32
+       std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_lib\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
+#else
+  std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_lib\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
+#endif
+  if(system(copyCommand.c_str()))
     {
-      if(this->libraries[i] != NULL)
+      result = new std::string("An error occurred while running '" + copyCommand + "'.");
+      return NULL;
+    }
+  //set name of library in CMakeLists inside copied folder
+  std::string line;
+  std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
+  if( !in.is_open())
+    {
+      result = new std::string("CMakeLists.txt file failed to open.");
+      return NULL;
+    }
+  std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
+  while (getline(in, line))
+    {
+      if(line == "SET ( LIBRARY_NAME   MyLib  )")
+        line = "SET ( LIBRARY_NAME   " + name + "  )";
+      out << line << std::endl;
+    }
+  in.close();
+  out.close();
+  //delete old file and rename new file
+#ifdef _WIN32
+  std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
+#else
+  std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
+#endif
+  if(system(renameCommand.c_str()))
+    {
+      result = new std::string("An error occurred while running '" + renameCommand + "'.");
+      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);
+  this->children.push_back(library);
+
+  this->SortChildren();
+
+  result = new std::string(this->path + CDMUtilities::SLASH + name);
+  return library;
+}
+
+const bool modelCDMLib::Refresh(std::string*& result)
+{
+  std::cout << "refreshing lib" << std::endl;
+  this->type = wxDIR_DIRS;
+
+
+
+  std::vector<bool> checked(this->children.size(), false);
+  std::vector<bool> checkedLibraries(this->libraries.size(), false);
+
+  //check all folders
+  wxDir dir(crea::std2wx((this->path).c_str()));
+  if (dir.IsOpened())
+    {
+      wxString fileName;
+      bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
+      while (cont)
         {
-          delete this->libraries[i];
-          this->libraries[i] = NULL;
+          std::string stdfileName = crea::wx2std(fileName);
+
+          if(stdfileName != "template_lib")
+            {
+              std::string libraryName = stdfileName;
+              //check if library already exist
+              bool found = false;
+              for (int i = 0; !found && i < (int)(this->libraries.size()); i++)
+                {
+                  if (this->libraries[i]->GetName() == libraryName)
+                    {
+                      found = true;
+                      int pos = std::find(this->children.begin(), this->children.end(), this->libraries[i]) - this->children.begin();
+                      checked[pos] = true;
+                      checkedLibraries[i] = true;
+                      if(!this->libraries[i]->Refresh(result))
+                        return false;
+                    }
+                }
+              if(!found)
+                {
+                  modelCDMLibrary* library = new modelCDMLibrary(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
+                  this->libraries.push_back(library);
+                  this->children.push_back(library);
+                }
+            }
+          else
+            {
+              //check if folder already exist
+              bool found = false;
+              for (int i = 0; !found && i < (int)(this->children.size()); i++)
+                {
+                  if (this->children[i]->GetName() == stdfileName)
+                    {
+                      found = true;
+                      checked[i] = true;
+                      if(!this->children[i]->Refresh(result))
+                        return false;
+                    }
+                }
+              if(!found)
+                {
+                  modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
+                  this->children.push_back(folder);
+                }
+            }
+          cont = dir.GetNext(&fileName);
+        }
+
+      cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
+      while (cont)
+        {
+          std::string stdfileName = crea::wx2std(fileName);
+
+          //if CMakeLists, create CMakeLists
+          if(stdfileName == "CMakeLists.txt")
+            {
+              if (this->CMakeLists == NULL)
+                {
+                  this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
+                  this->children.push_back(this->CMakeLists);
+                }
+              else
+                {
+                  int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
+                  checked[pos] = true;
+                  if(!this->CMakeLists->Refresh(result))
+                    return false;
+                }
+            }
+          //if is an unknown file, create file
+          else
+            {
+              bool found = false;
+              for (int i = 0; !found && i < (int)(this->children.size()); i++)
+                {
+                  if (this->children[i]->GetName() == stdfileName)
+                    {
+                      found = true;
+                      checked[i] = true;
+                      if(!this->children[i]->Refresh(result))
+                        return false;
+                    }
+                }
+
+              if(!found)
+                {
+                  modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
+                  this->children.push_back(file);
+                }
+            }
+
+          cont = dir.GetNext(&fileName);
+        }
+    }
+
+  for (int i = 0; i < (int)(checkedLibraries.size()); i++)
+    {
+      if(!checkedLibraries[i])
+        {
+          this->libraries.erase(this->libraries.begin()+i);
+          checkedLibraries.erase(checkedLibraries.begin()+i);
+          i--;
+        }
+    }
+  for (int i = 0; i < (int)(checked.size()); i++)
+    {
+      if(!checked[i])
+        {
+          delete this->children[i];
+          this->children.erase(this->children.begin()+i);
+          checked.erase(checked.begin()+i);
+          i--;
         }
     }
+  this->SortChildren();
+  std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
+  return true;
 }
 
-bool modelCDMLib::CreateLibrary(
-    const std::string& name,
-    std::string*& result,
-    const std::string& path
-)
+void modelCDMLib::CheckStructure(std::map<std::string, bool>& properties)
 {
-  //TODO: implement method
-  return true;
+  //check cmake exist
+  if(this->CMakeLists != NULL)
+    {
+      //set properties parameters based on model
+      for (int i = 0; i < (int)(this->libraries.size()); i++)
+        properties["lib add " + libraries[i]->GetName()] = false;
+
+      //open cmakelists
+      std::ifstream confFile;
+      confFile.open((this->CMakeLists->GetPath()).c_str());
+
+      //take everything that is not commented
+      std::string fileContent;
+
+      std::string word;
+      std::vector<std::string> words;
+      while(confFile.is_open() && !confFile.eof())
+        {
+          std::getline(confFile,word, '\n');
+          if(word[0] != '#')
+            {
+              CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
+              if (words.size() > 0)
+                {
+                  word = words[0];
+                  CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
+                  for (int i = 0; i < (int)(words.size()); i++)
+                    {
+                      if(words[i].substr(0,2) == "//")
+                        break;
+                      fileContent += words[i] + " ";
+                    }
+                }
+            }
+        }
+
+      //check every instruction
+      std::stringstream ss(fileContent);
+      while(!ss.eof())
+        {
+          std::getline(ss,word, '(');
+
+          //check instruction name
+          CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
+
+          //add instructions
+          if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
+            {
+              std::getline(ss,word, ')');
+              //std::cout << word << std::endl;
+              CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
+
+              if (words.size() > 0)
+                {
+                  {
+                    properties["lib add " + words[0]] = true;
+                  }
+                }
+            }
+        }
+
+    }
+
+  //check libraries' structure
+  for (int i = 0; i < (int)(this->libraries.size()); i++)
+    {
+      properties["library " + this->libraries[i]->GetName()] = true;
+      this->libraries[i]->CheckStructure(properties);
+    }
 }
 
-bool modelCDMLib::OpenCMakeListsFile(std::string*& result)
+bool modelCDMLib::IsLibraryIncluded(const std::string& library_name)
 {
-  //TODO: implement method
-  return true;
+  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);
+              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] == library_name)
+                    {
+                      CMFile.close();
+                      return true;
+                    }
+                }
+            }
+          CMFile.close();
+        }
+    }
+  return false;
 }
 
-const bool modelCDMLib::Refresh(std::string*& result)
+bool modelCDMLib::SetLibraryInclude(const std::string& library_name, const bool& toInclude)
 {
-  //TODO: implement method
-  return true;
+  if (this->HasCMakeLists())
+    {
+      std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
+      if (CMFile.is_open())
+        {
+          std::stringstream outs;
+          std::string line;
+          bool found = false;
+          while(!CMFile.eof())
+            {
+              std::getline(CMFile, line);
+              if (CMFile.eof()) {
+                break;
+              }
+              if(line != "")
+                {
+                  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
+                    {
+                      if (segs.size() > 0 && !toInclude)
+                        {
+                          CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
+                          if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name)
+                            {
+                              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;
+                            }
+                          outs << line << "\n";
+                        }
+                    }
+                }
+              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;
+            }
+        }
+    }
+  return false;
 }