]> Creatis software - crea.git/blob - lib/creaDevManagerLib/ModelCreaDevManagerTree.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / ModelCreaDevManagerTree.cpp
1 /*
2  * ModelCreaDevManagerTree.cpp
3  *
4  *  Created on: 22/10/2012
5  *      Author: daniel
6  */
7
8 #include "ModelCreaDevManagerTree.h"
9
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <dirent.h>
14 #include <iostream>
15 #include <sstream>
16 #include <algorithm>
17 #include <string>
18 #include <cstring>
19
20 using namespace std;
21
22 ModelCreaDevManagerTree::ModelCreaDevManagerTree()
23 {
24 }
25
26 ModelCreaDevManagerTree::~ModelCreaDevManagerTree()
27 {
28 }
29
30 bool ModelCreaDevManagerTree::CompareNodeItem(ModelCreaDevManagerTreeNode x, ModelCreaDevManagerTreeNode y)
31 {
32   bool returnValue;
33   bool noWinner = true;
34   unsigned int i = 0;
35   string xName = x.GetName();
36   string yName = y.GetName();
37   unsigned char xType = x.GetType();
38   unsigned char yType = y.GetType();
39
40   while ((i < xName.length()) && (i < yName.length()))
41   {
42     if (tolower (xName[i]) < tolower (yName[i]))
43     {
44       noWinner = false;
45       returnValue = true;
46       break;
47     }
48     else if (tolower (xName[i]) > tolower (yName[i]))
49     {
50       noWinner = false;
51       returnValue = false;
52       break;
53     }
54     i++;
55   }
56
57   if(noWinner)
58   {
59     if (xName.length() < yName.length())
60       returnValue = true;
61     else
62       returnValue = false;
63   }
64
65   if(xType != yType)
66   {
67     if(xType == DT_DIR)
68       returnValue = true;
69     else
70       returnValue = false;
71   }
72
73   return returnValue;
74 }
75
76 void ModelCreaDevManagerTree::addRoot(string path)
77 {
78   stringstream p(path);
79   vector<string> breadcrumbs;
80   string name;
81
82   while(!p.eof())
83   {
84     getline(p,name,'/');
85     if(name != "")
86       breadcrumbs.push_back(name);
87   }
88
89   path = "/";
90   for (int i = 0; i < breadcrumbs.size()-1; i++)
91   {
92     path += breadcrumbs[i] + "/";
93   }
94   name = breadcrumbs[breadcrumbs.size()-1];
95   this->projectRoots.push_back(ModelCreaDevManagerTreeNode(path,name,DT_DIR,0));
96
97   cout << "project root added: " << name << " in " << path << endl;
98 }
99
100 void ModelCreaDevManagerTree::populateNode(ModelCreaDevManagerTreeNode& node)
101 {
102   //cout << "populating " << node.GetName() << " path " << node.GetPath() << "..." << endl;
103   vector <ModelCreaDevManagerTreeNode>*  nodes = new vector <ModelCreaDevManagerTreeNode>;
104
105   DIR *dp;
106   struct dirent *ep;
107
108   string path = node.GetPath()+node.GetName()+"/";
109
110   dp = opendir(path.c_str());
111   if (dp != NULL)
112   {
113     while ((ep = readdir (dp)) != NULL)
114     {
115       //cout << ep->d_name << endl;
116       if(strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 )
117       {
118         ModelCreaDevManagerTreeNode innerNode = ModelCreaDevManagerTreeNode(path, ep->d_name, ep->d_type, node.GetLevel()+1);
119         if (ep->d_type == DT_DIR)
120         {
121           this->populateNode(innerNode);
122         }
123
124         nodes->push_back(innerNode);
125       }
126
127     }
128
129     (void) closedir (dp);
130
131     sort (nodes->begin(), nodes->end(), CompareNodeItem);
132
133     node.SetChildren(*nodes);
134   }
135   else
136   {
137     cerr << "Couldn't open the directory" << endl;
138   }
139 }
140
141 void ModelCreaDevManagerTree::populateNode(string path)
142 {
143   if(path[path.size()-1] != '/')
144     path+="/";
145   cout << "searching " << path << endl;
146   for (int i = 0; i < this->projectRoots.size(); i++)
147   {
148     //cout << (this->projectRoots[i].GetPath() + this->projectRoots[i].GetName() + "/") << "..." << endl;
149     if(this->projectRoots[i].GetPath()+this->projectRoots[i].GetName()+"/" == path)
150     {
151       cout << "Populating Project: " << path << "..." << endl;
152       this->populateNode(this->projectRoots[i]);
153       break;
154     }
155   }
156 }