]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMPackageSrc.cpp
b758cc4795d561c49e968d6282f01a87615ed7ed
[crea.git] / lib / creaDevManagerLib / modelCDMPackageSrc.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sant�)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and
11 #  abiding by the rules of distribution of free software. You can  use,
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B
13 #  license as circulated by CEA, CNRS and INRIA at the following URL
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability.
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26  */
27
28 /*
29  * modelCDMFolder.cpp
30  *
31  *  Created on: Nov 28, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMPackageSrc.h"
36
37 #include <fstream>
38 #include <algorithm>
39
40 #include <creaWx.h>
41 #include <wx/dir.h>
42
43 #include "CDMUtilities.h"
44
45 modelCDMPackageSrc::modelCDMPackageSrc()
46 {
47   this->CMakeLists = NULL;
48 }
49
50 modelCDMPackageSrc::modelCDMPackageSrc(const std::string& path, const int& level)
51 {
52   //set attributes
53   this->children.clear();
54   this->level = level;
55   this->CMakeLists = NULL;
56   this->length = 0;
57   this->name = "src";
58   this->path = CDMUtilities::fixPath(path);
59   this->type = wxDIR_DIRS;
60
61   //check all folders
62   wxDir dir(crea::std2wx(path));
63   if (dir.IsOpened())
64     {
65       wxString fileName;
66       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
67       while (cont)
68         {
69           std::string stdfileName = crea::wx2std(fileName);
70
71           //if is an unknown folder, create folder
72           this->children.push_back(new modelCDMFolder(path + "/" + stdfileName, this->level + 1));
73
74           cont = dir.GetNext(&fileName);
75         }
76
77       cont = dir.GetFirst(&fileName, wxT("CMakeLists.txt"), wxDIR_FILES);
78       if (cont)
79         {
80           std::string stdfileName = crea::wx2std(fileName);
81           this->CMakeLists = new modelCDMCMakeListsFile(path + "/" + stdfileName, this->level + 1);
82           this->children.push_back(this->CMakeLists);
83         }
84
85       cont = dir.GetFirst(&fileName, wxT("*.h"), wxDIR_FILES);
86       while (cont)
87         {
88           std::string stdfileName = crea::wx2std(fileName);
89           modelCDMFile* file;
90
91           if(stdfileName.substr(0,2) == "bb")
92             {
93               file = new modelCDMFile(path + "/" + stdfileName, this->level + 1);
94               this->children.push_back(file);
95               modelCDMBlackBox* blackBox = new modelCDMBlackBox(stdfileName, path, level + 1);
96               blackBox->SetHeaderFile(file);
97               cont = dir.GetFirst(&fileName, crea::std2wx(stdfileName.substr(0,stdfileName.size()-2) + ".cxx"), wxDIR_FILES);
98               if (cont)
99                 {
100                   file = new modelCDMFile(path + "/" + crea::wx2std(fileName), this->level + 1);
101                   this->children.push_back(file);
102                   blackBox->SetSourceFile(file);
103                 }
104               this->blackBoxes.push_back(blackBox);
105             }
106           cont = dir.GetNext(&fileName);
107         }
108     }
109
110   this->SortChildren();
111 }
112
113 modelCDMPackageSrc::~modelCDMPackageSrc()
114 {
115   for (int i = 0; i < this->blackBoxes.size(); i++)
116     {
117       if(this->blackBoxes[i] != NULL)
118         {
119           delete this->blackBoxes[i];
120           this->blackBoxes[i] = NULL;
121         }
122     }
123   this->blackBoxes.clear();
124 }
125
126 const std::vector<modelCDMBlackBox*>& modelCDMPackageSrc::GetBlackBoxes() const
127 {
128   return this->blackBoxes;
129 }
130
131 const bool modelCDMPackageSrc::Refresh(std::string*& result)
132 {
133   //set attributes
134   this->type = wxDIR_DIRS;
135
136   //check children
137   std::vector<bool> checked(this->children.size(), false);
138   std::vector<bool> checkedBoxes(this->blackBoxes.size(), false);
139
140   //check all boxes
141   wxDir dir(crea::std2wx((this->path).c_str()));
142   if (dir.IsOpened())
143     {
144       wxString fileName;
145       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
146       while (cont)
147         {
148           std::string stdfileName = crea::wx2std(fileName);
149           std::string folderName = stdfileName;
150           //check if they already exist
151           bool found = false;
152           for (int i = 0;!found && i < this->children.size(); i++)
153             {
154               if (this->children[i]->GetName() == folderName)
155                 {
156                   found = true;
157                   checked[i] = true;
158                   if(!this->children[i]->Refresh(result))
159                     return false;
160                 }
161             }
162           if(!found)
163             {
164               modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
165               this->children.push_back(folder);
166             }
167           cont = dir.GetNext(&fileName);
168         }
169
170       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
171       while (cont)
172         {
173           std::string stdfileName = crea::wx2std(fileName);
174
175           //if CMakeLists, create CMakeLists
176           if(stdfileName == "CMakeLists.txt")
177             {
178               if (this->CMakeLists == NULL)
179                 {
180                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
181                   this->children.push_back(this->CMakeLists);
182                 }
183               else
184                 {
185                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
186                   checked[pos] = true;
187                   if(!this->CMakeLists->Refresh(result))
188                     return false;
189                 }
190             }
191           //if is an unknown file, create file
192           else
193             {
194               bool found = false;
195               for (int i = 0; i <!found && this->children.size(); i++)
196                 {
197                   if (this->children[i]->GetName() == stdfileName)
198                     {
199                       found = true;
200                       checked[i] = true;
201                       if(!this->children[i]->Refresh(result))
202                         return false;
203                     }
204                 }
205
206               if(!found)
207                 {
208                   modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
209                   this->children.push_back(file);
210                 }
211             }
212
213           //if is a Black Box header, check in black boxes
214           if(stdfileName.substr(stdfileName.size() - 2, 2) == ".h" && stdfileName.substr(0,2) == "bb")
215             {
216               bool found = false;
217               for (int i = 0; i < this->blackBoxes.size(); i++)
218                 {
219                   if(this->blackBoxes[i]->GetHeaderFile()->GetName() == stdfileName)
220                     {
221                       checkedBoxes[i] = true;
222                       found = true;
223                       if(!this->blackBoxes[i]->Refresh(result))
224                         return false;
225                       break;
226                     }
227                 }
228
229               if (!found)
230                 {
231                   modelCDMBlackBox* blackBox = new modelCDMBlackBox(stdfileName, path + "/" + stdfileName, level + 1);
232                   this->blackBoxes.push_back(blackBox);
233                 }
234
235             }
236
237           cont = dir.GetNext(&fileName);
238         }
239     }
240
241   for (int i = 0; i < checkedBoxes.size(); i++)
242     {
243       if(!checkedBoxes[i])
244         {
245           delete this->blackBoxes[i];
246           this->blackBoxes.erase(this->blackBoxes.begin()+i);
247           checkedBoxes.erase(checkedBoxes.begin()+i);
248           i--;
249         }
250     }
251
252   for (int i = 0; i < checked.size(); i++)
253     {
254       if(!checked[i])
255         {
256           delete this->children[i];
257           this->children.erase(this->children.begin()+i);
258           checked.erase(checked.begin()+i);
259           i--;
260         }
261     }
262   this->SortChildren();
263   return true;
264 }