]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/ModelCreaDevManagerTree.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / ModelCreaDevManagerTree.cpp
diff --git a/lib/creaDevManagerLib/ModelCreaDevManagerTree.cpp b/lib/creaDevManagerLib/ModelCreaDevManagerTree.cpp
new file mode 100644 (file)
index 0000000..e4bcc71
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * ModelCreaDevManagerTree.cpp
+ *
+ *  Created on: 22/10/2012
+ *      Author: daniel
+ */
+
+#include "ModelCreaDevManagerTree.h"
+
+#include <stddef.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+#include <string>
+#include <cstring>
+
+using namespace std;
+
+ModelCreaDevManagerTree::ModelCreaDevManagerTree()
+{
+}
+
+ModelCreaDevManagerTree::~ModelCreaDevManagerTree()
+{
+}
+
+bool ModelCreaDevManagerTree::CompareNodeItem(ModelCreaDevManagerTreeNode x, ModelCreaDevManagerTreeNode y)
+{
+  bool returnValue;
+  bool noWinner = true;
+  unsigned int i = 0;
+  string xName = x.GetName();
+  string yName = y.GetName();
+  unsigned char xType = x.GetType();
+  unsigned char yType = y.GetType();
+
+  while ((i < xName.length()) && (i < yName.length()))
+  {
+    if (tolower (xName[i]) < tolower (yName[i]))
+    {
+      noWinner = false;
+      returnValue = true;
+      break;
+    }
+    else if (tolower (xName[i]) > tolower (yName[i]))
+    {
+      noWinner = false;
+      returnValue = false;
+      break;
+    }
+    i++;
+  }
+
+  if(noWinner)
+  {
+    if (xName.length() < yName.length())
+      returnValue = true;
+    else
+      returnValue = false;
+  }
+
+  if(xType != yType)
+  {
+    if(xType == DT_DIR)
+      returnValue = true;
+    else
+      returnValue = false;
+  }
+
+  return returnValue;
+}
+
+void ModelCreaDevManagerTree::addRoot(string path)
+{
+  stringstream p(path);
+  vector<string> breadcrumbs;
+  string name;
+
+  while(!p.eof())
+  {
+    getline(p,name,'/');
+    if(name != "")
+      breadcrumbs.push_back(name);
+  }
+
+  path = "/";
+  for (int i = 0; i < breadcrumbs.size()-1; i++)
+  {
+    path += breadcrumbs[i] + "/";
+  }
+  name = breadcrumbs[breadcrumbs.size()-1];
+  this->projectRoots.push_back(ModelCreaDevManagerTreeNode(path,name,DT_DIR,0));
+
+  cout << "project root added: " << name << " in " << path << endl;
+}
+
+void ModelCreaDevManagerTree::populateNode(ModelCreaDevManagerTreeNode& node)
+{
+  //cout << "populating " << node.GetName() << " path " << node.GetPath() << "..." << endl;
+  vector <ModelCreaDevManagerTreeNode>*  nodes = new vector <ModelCreaDevManagerTreeNode>;
+
+  DIR *dp;
+  struct dirent *ep;
+
+  string path = node.GetPath()+node.GetName()+"/";
+
+  dp = opendir(path.c_str());
+  if (dp != NULL)
+  {
+    while ((ep = readdir (dp)) != NULL)
+    {
+      //cout << ep->d_name << endl;
+      if(strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 )
+      {
+        ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, ep->d_name, ep->d_type, node.GetLevel()+1);
+        if (ep->d_type == DT_DIR)
+        {
+          this->populateNode(innerNode);
+        }
+
+        nodes->push_back(innerNode);
+      }
+
+    }
+
+    (void) closedir (dp);
+
+    sort (nodes->begin(), nodes->end(), CompareNodeItem);
+
+    node.SetChildren(*nodes);
+  }
+  else
+  {
+    cerr << "Couldn't open the directory" << endl;
+  }
+}
+
+void ModelCreaDevManagerTree::populateNode(string path)
+{
+  if(path[path.size()-1] != '/')
+    path+="/";
+  cout << "searching " << path << endl;
+  for (int i = 0; i < this->projectRoots.size(); i++)
+  {
+    //cout << (this->projectRoots[i].GetPath() + this->projectRoots[i].GetName() + "/") << "..." << endl;
+    if(this->projectRoots[i].GetPath()+this->projectRoots[i].GetName()+"/" == path)
+    {
+      cout << "Populating Project: " << path << "..." << endl;
+      this->populateNode(this->projectRoots[i]);
+      break;
+    }
+  }
+}