]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/modelCDMLib.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMLib.cpp
index 50b32f5e76c509fec9252a648bfc27286dcb46db..2201891d72615c8ca1b01f935eced1ed2c3712fd 100644 (file)
@@ -34,6 +34,8 @@
 
 #include "modelCDMLib.h"
 
+#include <fstream>
+
 #include "CDMUtilities.h"
 #include "creaWx.h"
 #include "wx/dir.h"
@@ -56,46 +58,75 @@ 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();
-}
-
-modelCDMLib::~modelCDMLib()
-{
-  for (int i = 0; i < this->libraries.size(); i++)
+  wxDir dir(crea::std2wx((pathFixed).c_str()));
+  if (dir.IsOpened())
     {
-      if(this->libraries[i] != NULL)
+      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);
+
+          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();
+}
+
+modelCDMLib::~modelCDMLib()
+{
 }
 
-bool modelCDMLib::CreateLibrary(
+modelCDMIProjectTreeNode* modelCDMLib::CreateLibrary(
     const std::string& name,
     std::string*& result,
     const std::string& path
 )
 {
-  //TODO: implement method
-  return true;
+  //copy template library folder with new name
+  std::string copyCommand = "cp -r " + this->path + "/template_lib " + this->path + "/" + name;
+  if(system(copyCommand.c_str()))
+    {
+      result = new std::string("An error occurred while running '" + copyCommand + "'.");
+      return NULL;
+    }
+  //TODO: set name of library in CMakeLists inside copied folder
+  std::string line;
+  std::ifstream in((this->path + "/" + name + "/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 + "/" + name + "/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
+  std::string renameCommand = "mv " + this->path + "/" + name + "/CMakeLists.txt.tmp " + this->path + "/" + name + "/CMakeLists.txt";
+  if(system(renameCommand.c_str()))
+    {
+      result = new std::string("An error occurred while running '" + renameCommand + "'.");
+      return NULL;
+    }
+
+  //add library to model
+  modelCDMLibrary* library = new modelCDMLibrary(this->path + "/" + name, this->level + 1);
+  this->libraries.push_back(library);
+  this->children.push_back(library);
+
+  result = new std::string(this->path + "/" + name);
+  return library;
 }
 
 bool modelCDMLib::OpenCMakeListsFile(std::string*& result)