]> 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   std::cout << "creating package src: " + path + "\n";
53   //set attributes
54   this->children.clear();
55   this->level = level;
56   this->CMakeLists = NULL;
57   this->length = 0;
58   this->name = name;
59   this->path = CDMUtilities::fixPath(path);
60   this->type = wxDIR_DIRS;
61
62   //check all folders
63   wxDir dir(crea::std2wx(path));
64   if (dir.IsOpened())
65     {
66       wxString fileName;
67       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
68       while (cont)
69         {
70           std::string stdfileName = crea::wx2std(fileName);
71
72           //if is an unknown folder, create folder
73           this->children.push_back(new modelCDMFolder(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
74
75           cont = dir.GetNext(&fileName);
76         }
77
78       cont = dir.GetFirst(&fileName, wxT("CMakeLists.txt"), wxDIR_FILES);
79       if (cont)
80         {
81           std::string stdfileName = crea::wx2std(fileName);
82           this->CMakeLists = new modelCDMCMakeListsFile(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
83           this->children.push_back(this->CMakeLists);
84         }
85
86       cont = dir.GetFirst(&fileName, wxT("*.h"), wxDIR_FILES);
87       while (cont)
88         {
89           std::string stdfileName = crea::wx2std(fileName);
90           modelCDMFile* file;
91
92           if(stdfileName.substr(0,2) == "bb")
93             {
94               file = new modelCDMFile(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
95               this->children.push_back(file);
96               modelCDMBlackBox* blackBox = new modelCDMBlackBox(path, stdfileName.substr(2,stdfileName.size()-4), level + 1);
97               blackBox->SetHeaderFile(file);
98               wxDir dir2(crea::std2wx(path));
99               cont = dir2.GetFirst(&fileName, crea::std2wx(stdfileName.substr(0,stdfileName.size()-2) + ".cxx"), wxDIR_FILES);
100               if (cont)
101                 {
102                   file = new modelCDMFile(path + CDMUtilities::SLASH + crea::wx2std(fileName), crea::wx2std(fileName), this->level + 1);
103                   this->children.push_back(file);
104                   blackBox->SetSourceFile(file);
105                 }
106               this->blackBoxes.push_back(blackBox);
107             }
108           cont = dir.GetNext(&fileName);
109         }
110     }
111
112   this->SortChildren();
113   std::sort(this->blackBoxes.begin(), this->blackBoxes.end(), CompareNodeItem);
114 }
115
116 modelCDMPackageSrc::~modelCDMPackageSrc()
117 {
118   for (int i = 0; i < this->blackBoxes.size(); i++)
119     {
120       if(this->blackBoxes[i] != NULL)
121         {
122           delete this->blackBoxes[i];
123           this->blackBoxes[i] = NULL;
124         }
125     }
126   this->blackBoxes.clear();
127 }
128
129 const std::vector<modelCDMBlackBox*>& modelCDMPackageSrc::GetBlackBoxes() const
130 {
131   return this->blackBoxes;
132 }
133
134 modelCDMBlackBox* modelCDMPackageSrc::CreateBlackBox(
135     std::string*& result,
136     const std::string& name,
137     const std::string& package,
138     const std::string& type,
139     const std::string& format,
140     const std::string& categories,
141     const std::string& authors,
142     const std::string& authorsEmail,
143     const std::string& description)
144 {
145   //parse name
146   std::vector<std::string> words;
147   CDMUtilities::splitter::split(words, name, " \n\",/\\", CDMUtilities::splitter::no_empties);
148   std::string bbName;
149   for (int i = 0; i < words.size(); i++)
150     {
151       bbName += words[i];
152     }
153
154   //parse categories
155   CDMUtilities::splitter::split(words, categories, " \n\",/\\", CDMUtilities::splitter::no_empties);
156   std::string bbCategories;
157   if(words.size() > 0)
158     {
159       bbCategories = words[0];
160       for (int i = 1; i < words.size(); i++)
161         {
162           bbCategories += "," + words[i];
163         }
164     }
165   if(bbCategories == "")
166     bbCategories = "empty";
167
168   //parse authors
169   CDMUtilities::splitter::split(words, authors, "\n\",/\\", CDMUtilities::splitter::no_empties);
170   std::string bbAuthors;
171   if(words.size() > 0)
172     {
173       bbAuthors = words[0];
174       for (int i = 1; i < words.size(); i++)
175         {
176           bbAuthors += "," + words[i];
177         }
178     }
179   if(bbAuthors == "")
180     bbAuthors = "Unknown";
181
182   //parse description
183   CDMUtilities::splitter::split(words, authorsEmail, " \n\"/\\", CDMUtilities::splitter::no_empties);
184   std::string bbDescription;
185   if(words.size() > 0)
186     {
187       bbDescription = words[0];
188       for (int i = 1; i < words.size(); i++)
189         {
190           bbDescription += "," + words[i];
191         }
192       bbDescription += " - ";
193     }
194   CDMUtilities::splitter::split(words, description, "\n\"/\\", CDMUtilities::splitter::no_empties);
195   if(words.size() > 0)
196     {
197       bbDescription += words[0];
198       for (int i = 1; i < words.size(); i++)
199         {
200           bbDescription += words[i];
201         }
202     }
203
204   if(bbDescription == "")
205     bbDescription = "No Description.";
206
207
208   //create command
209   std::string command = "bbCreateBlackBox";
210   command += " \"" + this->path + "\"";
211   command += " \"" + package + "\"";
212   command += " \"" + bbName + "\"";
213   command += " \"" + type + "\"";
214   command += " \"" + format + "\"";
215   command += " \"" + bbAuthors + "\"";
216   command += " \"" + bbDescription + "\"";
217   command += " \"" + bbCategories + "\"";
218
219   //excecute command
220   if(system(command.c_str()))
221     {
222       result = new std::string("Error executing command '" + command + "'");
223       return NULL;
224     }
225
226   //if command succeed
227
228   //create header
229   //create source
230   modelCDMFile* header = NULL;
231   modelCDMFile* source = NULL;
232   wxDir dir(crea::std2wx(path));
233   if (dir.IsOpened())
234     {
235       wxString fileName;
236       bool cont = dir.GetFirst(&fileName, crea::std2wx("bb"+package+bbName+".h"), wxDIR_FILES);
237       if (cont)
238         {
239           std::string stdfileName = crea::wx2std(fileName);
240           header = new modelCDMFile(this->path + stdfileName, stdfileName, this->level+1);
241         }
242       cont = dir.GetFirst(&fileName, crea::std2wx("bb"+package+bbName+".cxx"), wxDIR_FILES);
243       if (cont)
244         {
245           std::string stdfileName = crea::wx2std(fileName);
246           source = new modelCDMFile(this->path + stdfileName, stdfileName, this->level+1);
247         }
248     }
249   //if source and header exist
250   if (header != NULL && source != NULL)
251     {
252       //create black box
253       modelCDMBlackBox* blackBox = new modelCDMBlackBox(this->path, package+bbName);
254
255       //associate header and source
256       blackBox->SetHeaderFile(header);
257       blackBox->SetSourceFile(source);
258
259       this->children.push_back(header);
260       this->children.push_back(source);
261
262       this->blackBoxes.push_back(blackBox);
263
264       //sort children
265       std::sort(this->blackBoxes.begin(), this->blackBoxes.end(), CompareNodeItem);
266       this->SortChildren();
267
268       return blackBox;
269     }
270   else
271     {
272       result = new std::string("The header and source files were not found. Black box not created.");
273       return NULL;
274     }
275 }
276
277 const bool modelCDMPackageSrc::Refresh(std::string*& result)
278 {
279   std::cout << "refreshing package src" << std::endl;
280   //set attributes
281   this->type = wxDIR_DIRS;
282
283   //check children
284   std::vector<bool> checked(this->children.size(), false);
285   std::vector<bool> checkedBoxes(this->blackBoxes.size(), false);
286
287   //check all boxes
288   wxDir dir(crea::std2wx((this->path).c_str()));
289   if (dir.IsOpened())
290     {
291       wxString fileName;
292       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
293       while (cont)
294         {
295           std::string stdfileName = crea::wx2std(fileName);
296           std::string folderName = stdfileName;
297           //check if they already exist
298           bool found = false;
299           for (int i = 0; !found && i < this->children.size(); i++)
300             {
301               if (this->children[i]->GetName() == folderName)
302                 {
303                   found = true;
304                   checked[i] = true;
305                   if(!this->children[i]->Refresh(result))
306                     return false;
307                 }
308             }
309           if(!found)
310             {
311               modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
312               this->children.push_back(folder);
313             }
314           cont = dir.GetNext(&fileName);
315         }
316
317       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
318       while (cont)
319         {
320           std::string stdfileName = crea::wx2std(fileName);
321
322           //if CMakeLists, create CMakeLists
323           if(stdfileName == "CMakeLists.txt")
324             {
325               if (this->CMakeLists == NULL)
326                 {
327                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
328                   this->children.push_back(this->CMakeLists);
329                 }
330               else
331                 {
332                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
333                   checked[pos] = true;
334                   if(!this->CMakeLists->Refresh(result))
335                     return false;
336                 }
337             }
338           //if is an unknown file, create file
339           else
340             {
341               bool found = false;
342               for (int i = 0; !found && i < this->children.size(); i++)
343                 {
344                   if (this->children[i]->GetName() == stdfileName)
345                     {
346                       found = true;
347                       checked[i] = true;
348                       if(!this->children[i]->Refresh(result))
349                         return false;
350                     }
351                 }
352
353               if(!found)
354                 {
355                   modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
356                   this->children.push_back(file);
357                 }
358             }
359
360           //if is a Black Box header, check in black boxes
361           if(stdfileName.substr(stdfileName.size() - 2, 2) == ".h" && stdfileName.substr(0,2) == "bb")
362             {
363               bool found = false;
364               for (int i = 0; i < this->blackBoxes.size(); i++)
365                 {
366                   if(this->blackBoxes[i]->GetHeaderFile()->GetName() == stdfileName)
367                     {
368                       checkedBoxes[i] = true;
369                       found = true;
370                       if(!this->blackBoxes[i]->Refresh(result))
371                         return false;
372                       break;
373                     }
374                 }
375
376               if (!found)
377                 {
378                   modelCDMBlackBox* blackBox = new modelCDMBlackBox(path, stdfileName.substr(2,stdfileName.size()-4), level + 1);
379                   this->blackBoxes.push_back(blackBox);
380                 }
381
382             }
383
384           cont = dir.GetNext(&fileName);
385         }
386     }
387
388   for (int i = 0; i < checkedBoxes.size(); i++)
389     {
390       if(!checkedBoxes[i])
391         {
392           delete this->blackBoxes[i];
393           this->blackBoxes.erase(this->blackBoxes.begin()+i);
394           checkedBoxes.erase(checkedBoxes.begin()+i);
395           i--;
396         }
397     }
398
399   for (int i = 0; i < checked.size(); i++)
400     {
401       if(!checked[i])
402         {
403           delete this->children[i];
404           this->children.erase(this->children.begin()+i);
405           checked.erase(checked.begin()+i);
406           i--;
407         }
408     }
409   this->SortChildren();
410   std::sort(this->blackBoxes.begin(), this->blackBoxes.end(), CompareNodeItem);
411   return true;
412 }