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