]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMPackageSrc.cpp
Feature #1711
[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 std::string& name, 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 = name;
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 + CDMUtilities::SLASH + stdfileName, 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 + CDMUtilities::SLASH + stdfileName, 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 + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
94               this->children.push_back(file);
95               modelCDMBlackBox* blackBox = new modelCDMBlackBox(path, stdfileName.substr(2,stdfileName.size()-4), level + 1);
96               blackBox->SetHeaderFile(file);
97               wxDir dir2(crea::std2wx(path));
98               cont = dir2.GetFirst(&fileName, crea::std2wx(stdfileName.substr(0,stdfileName.size()-2) + ".cxx"), wxDIR_FILES);
99               if (cont)
100                 {
101                   file = new modelCDMFile(path + CDMUtilities::SLASH + crea::wx2std(fileName), crea::wx2std(fileName), this->level + 1);
102                   this->children.push_back(file);
103                   blackBox->SetSourceFile(file);
104                 }
105               this->blackBoxes.push_back(blackBox);
106             }
107           cont = dir.GetNext(&fileName);
108         }
109     }
110
111   this->SortChildren();
112 }
113
114 modelCDMPackageSrc::~modelCDMPackageSrc()
115 {
116   for (int i = 0; i < this->blackBoxes.size(); i++)
117     {
118       if(this->blackBoxes[i] != NULL)
119         {
120           delete this->blackBoxes[i];
121           this->blackBoxes[i] = NULL;
122         }
123     }
124   this->blackBoxes.clear();
125 }
126
127 const std::vector<modelCDMBlackBox*>& modelCDMPackageSrc::GetBlackBoxes() const
128 {
129   return this->blackBoxes;
130 }
131
132 modelCDMBlackBox* modelCDMPackageSrc::CreateBlackBox(
133     const std::string& name,
134     const std::string& package,
135     const std::string& type,
136     const std::string& format,
137     const std::string& authors,
138     const std::string& authorsEmail,
139     const std::string& categories,
140     const std::string& description)
141 {
142   //TODO: implement method
143   return NULL;
144 }
145
146 const bool modelCDMPackageSrc::Refresh(std::string*& result)
147 {
148   //set attributes
149   this->type = wxDIR_DIRS;
150
151   //check children
152   std::vector<bool> checked(this->children.size(), false);
153   std::vector<bool> checkedBoxes(this->blackBoxes.size(), false);
154
155   //check all boxes
156   wxDir dir(crea::std2wx((this->path).c_str()));
157   if (dir.IsOpened())
158     {
159       wxString fileName;
160       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
161       while (cont)
162         {
163           std::string stdfileName = crea::wx2std(fileName);
164           std::string folderName = stdfileName;
165           //check if they already exist
166           bool found = false;
167           for (int i = 0;!found && i < this->children.size(); i++)
168             {
169               if (this->children[i]->GetName() == folderName)
170                 {
171                   found = true;
172                   checked[i] = true;
173                   if(!this->children[i]->Refresh(result))
174                     return false;
175                 }
176             }
177           if(!found)
178             {
179               modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
180               this->children.push_back(folder);
181             }
182           cont = dir.GetNext(&fileName);
183         }
184
185       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
186       while (cont)
187         {
188           std::string stdfileName = crea::wx2std(fileName);
189
190           //if CMakeLists, create CMakeLists
191           if(stdfileName == "CMakeLists.txt")
192             {
193               if (this->CMakeLists == NULL)
194                 {
195                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
196                   this->children.push_back(this->CMakeLists);
197                 }
198               else
199                 {
200                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
201                   checked[pos] = true;
202                   if(!this->CMakeLists->Refresh(result))
203                     return false;
204                 }
205             }
206           //if is an unknown file, create file
207           else
208             {
209               bool found = false;
210               for (int i = 0; i <!found && this->children.size(); i++)
211                 {
212                   if (this->children[i]->GetName() == stdfileName)
213                     {
214                       found = true;
215                       checked[i] = true;
216                       if(!this->children[i]->Refresh(result))
217                         return false;
218                     }
219                 }
220
221               if(!found)
222                 {
223                   modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
224                   this->children.push_back(file);
225                 }
226             }
227
228           //if is a Black Box header, check in black boxes
229           if(stdfileName.substr(stdfileName.size() - 2, 2) == ".h" && stdfileName.substr(0,2) == "bb")
230             {
231               bool found = false;
232               for (int i = 0; i < this->blackBoxes.size(); i++)
233                 {
234                   if(this->blackBoxes[i]->GetHeaderFile()->GetName() == stdfileName)
235                     {
236                       checkedBoxes[i] = true;
237                       found = true;
238                       if(!this->blackBoxes[i]->Refresh(result))
239                         return false;
240                       break;
241                     }
242                 }
243
244               if (!found)
245                 {
246                   modelCDMBlackBox* blackBox = new modelCDMBlackBox(path, stdfileName.substr(2,stdfileName.size()-4), level + 1);
247                   this->blackBoxes.push_back(blackBox);
248                 }
249
250             }
251
252           cont = dir.GetNext(&fileName);
253         }
254     }
255
256   for (int i = 0; i < checkedBoxes.size(); i++)
257     {
258       if(!checkedBoxes[i])
259         {
260           delete this->blackBoxes[i];
261           this->blackBoxes.erase(this->blackBoxes.begin()+i);
262           checkedBoxes.erase(checkedBoxes.begin()+i);
263           i--;
264         }
265     }
266
267   for (int i = 0; i < checked.size(); i++)
268     {
269       if(!checked[i])
270         {
271           delete this->children[i];
272           this->children.erase(this->children.begin()+i);
273           checked.erase(checked.begin()+i);
274           i--;
275         }
276     }
277   this->SortChildren();
278   return true;
279 }