]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMProject.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMProject.cpp
index dc9ac63d5f5752d2f830fae4db656bbf0cb77201..1c4b89845ba44fb7ce80aeed277ecebd1660d521 100644 (file)
@@ -36,6 +36,7 @@
 
 #include <iostream>
 #include <vector>
+#include <algorithm>
 #include <fstream>
 
 #include "CDMUtilities.h"
@@ -204,14 +205,6 @@ modelCDMProject::modelCDMProject(
 
 modelCDMProject::~modelCDMProject()
 {
-  for (int i = 0; i < this->children.size(); i++)
-    {
-      if(this->children[i] != NULL)
-        {
-          delete this->children[i];
-          this->children[i] = NULL;
-        }
-    }
 }
 
 const std::string& modelCDMProject::GetNameProject() const
@@ -246,7 +239,7 @@ bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result
   return true;
 }
 
-bool modelCDMProject::CreatePackage(
+modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
     const std::string& name,
     std::string*& result,
     const std::string& authors,
@@ -256,30 +249,34 @@ bool modelCDMProject::CreatePackage(
 )
 {
   //TODO: implement method
-  return true;
+  return NULL;
 }
 
-bool modelCDMProject::CreateLibrary(
+modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
     const std::string& name,
     std::string*& result,
     const std::string& path
 )
 {
-  //TODO: implement method
-  return true;
+  if(this->lib != NULL)
+    {
+      return this->lib->CreateLibrary(name, result);
+    }
+  result = new std::string("there is no lib folder in this project.");
+  return NULL;
 }
 
-bool modelCDMProject::CreateApplication(
+modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
     const std::string& name,
     std::string*& result,
     const std::string& path
 )
 {
   //TODO: implement method
-  return true;
+  return NULL;
 }
 
-bool modelCDMProject::CreateBlackBox(
+modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
     const std::string& name,
     const std::string& package,
     const std::string& authors,
@@ -289,7 +286,7 @@ bool modelCDMProject::CreateBlackBox(
 )
 {
   //TODO: implement method
-  return true;
+  return NULL;
 }
 
 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
@@ -300,7 +297,236 @@ bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
 
 const bool modelCDMProject::Refresh(std::string*& result)
 {
-  //TODO: implement method
+
+  //open makelists file
+  //TODO: set pathMakeLists for windows
+  std::string pathMakeLists = this->path + "/CMakeLists.txt";
+
+  std::ifstream confFile;
+  confFile.open((pathMakeLists).c_str());
+
+  std::string word;
+  while(confFile.is_open() && !confFile.eof())
+    {
+      //std::cout << "leyendo " << word << std::endl;
+      //get project name
+      std::getline(confFile,word,'(');
+      std::vector<std::string> wordBits;
+      CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
+
+      if(wordBits[wordBits.size()-1] == "PROJECT")
+        {
+          std::getline(confFile,word,')');
+          std::vector<std::string> nameBits;
+          CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
+
+          this->name = this->nameProject = "";
+          for (int i = 0; i < nameBits.size(); i++)
+            {
+              if(i != 0)
+                this->name += " ";
+              this->name += nameBits[i];
+            }
+          this->nameProject = this->name;
+
+        }
+
+
+      if(wordBits[wordBits.size()-1] == "SET")
+        {
+          //get project version
+          std::getline(confFile,word,')');
+          CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
+          if(wordBits[0] == "PROJECT_MAJOR_VERSION")
+            {
+              version = wordBits[1];
+            }
+          if(wordBits[0] == "PROJECT_MINOR_VERSION")
+            {
+              version += "." + wordBits[1];
+            }
+          if(wordBits[0] == "PROJECT_BUILD_VERSION")
+            {
+              version += "." + wordBits[1];
+            }
+
+          //get project versionDate
+          if(wordBits[0] == "PROJECT_VERSION_DATE")
+            {
+              std::vector<std::string> versionBits;
+              CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
+              versionDate = versionBits[0];
+            }
+        }
+    }
+  confFile.close();
+
+  this->type = wxDIR_DIRS;
+  this->level = 0;
+
+  std::vector<bool> checked(this->children.size(), false);
+  std::vector<bool> checkedPackages(this->packages.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)
+        {
+          std::string stdfileName = crea::wx2std(fileName);
+
+          //if appli, create appli
+          if(stdfileName == "appli")
+            {
+              if (this->appli == NULL)
+                {
+                  this->appli = new modelCDMAppli(this->path + "/appli", this->level + 1);
+                  this->children.push_back(this->appli);
+                }
+              else
+                {
+                  int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
+                  checked[pos] = true;
+                  if(!this->appli->Refresh(result))
+                    return false;
+                }
+            }
+          //if lib, create lib
+          else if(stdfileName == "lib")
+            {
+              if (this->lib == NULL)
+                {
+                  this->lib = new modelCDMLib(this->path + "/lib", this->level + 1);
+                  this->children.push_back(this->lib);
+                }
+              else
+                {
+                  int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
+                  checked[pos] = true;
+                  if(!this->lib->Refresh(result))
+                    return false;
+                }
+
+            }
+          //if package , create package
+          else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
+            {
+              std::string packageName = stdfileName.substr(5, stdfileName.size()-9);
+              bool found = false;
+              for (int i = 0;!found && i < this->packages.size(); i++)
+                {
+                  if (this->packages[i]->GetName() == packageName)
+                    {
+                      found = true;
+                      int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
+                      checked[pos] = true;
+                      checkedPackages[i] = true;
+                      if(!this->packages[i]->Refresh(result))
+                        return false;
+                    }
+                }
+              if(!found)
+                {
+                  modelCDMPackage* package = new modelCDMPackage(this->path + "/" + stdfileName, this->level + 1);
+                  this->packages.push_back(package);
+                  this->children.push_back(package);
+                }
+
+            }
+          //if is an unknown folder, create folder
+          else
+            {
+              bool found = false;
+              for (int i = 0; !found && i < 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->path + "/" + 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->path + "/" + 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; i <!found && 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->path + "/" + stdfileName, this->level + 1);
+                  this->children.push_back(file);
+                }
+            }
+
+          cont = dir.GetNext(&fileName);
+        }
+    }
+
+  for (int i = 0; i < checkedPackages.size(); i++)
+    {
+      if(!checkedPackages[i])
+        {
+          this->packages.erase(this->packages.begin()+i);
+                    checkedPackages.erase(checkedPackages.begin()+i);
+                    i--;
+        }
+    }
+  for (int i = 0; i < 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();
   return true;
 }