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