]> 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   //wxMessageBox(crea::std2wx("Command: ->" + command + "<-"),_T("Creating Black Box"),wxOK | wxICON_INFORMATION);
229   if(system(command.c_str()))
230     {
231       result = new std::string("Error executing command '" + command + "'");
232       return NULL;
233     }
234
235   //if command succeed
236
237   //create header
238   //create source
239   modelCDMFile* header = NULL;
240   modelCDMFile* source = NULL;
241   wxDir dir(crea::std2wx(path));
242   if (dir.IsOpened())
243     {
244       wxString fileName;
245       bool cont = dir.GetFirst(&fileName, crea::std2wx("bb"+package+bbName+".h"), wxDIR_FILES);
246       if (cont)
247         {
248           std::string stdfileName = crea::wx2std(fileName);
249           header = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level+1);
250         }
251       cont = dir.GetFirst(&fileName, crea::std2wx("bb"+package+bbName+".cxx"), wxDIR_FILES);
252       if (cont)
253         {
254           std::string stdfileName = crea::wx2std(fileName);
255           source = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level+1);
256         }
257     }
258   //if source and header exist
259   if (header != NULL && source != NULL)
260     {
261       //create black box
262       modelCDMBlackBox* blackBox = new modelCDMBlackBox(this, this->path, package+bbName);
263
264       //associate header and source
265       blackBox->SetHeaderFile(header);
266       blackBox->SetSourceFile(source);
267
268       this->children.push_back(header);
269       this->children.push_back(source);
270
271       this->blackBoxes.push_back(blackBox);
272
273       //sort children
274       std::sort(this->blackBoxes.begin(), this->blackBoxes.end(), CompareNodeItem);
275       this->SortChildren();
276
277       return blackBox;
278     }
279   else
280     {
281       result = new std::string("The header and source files were not found. Black box not created.");
282       return NULL;
283     }
284 }
285
286 const bool modelCDMPackageSrc::Refresh(std::string*& result)
287 {
288   std::cout << "refreshing package src" << std::endl;
289   //set attributes
290   this->type = wxDIR_DIRS;
291
292   //check children
293   std::vector<bool> checked(this->children.size(), false);
294   std::vector<bool> checkedBoxes(this->blackBoxes.size(), false);
295
296   //check all boxes
297   wxDir dir(crea::std2wx((this->path).c_str()));
298   if (dir.IsOpened())
299     {
300       wxString fileName;
301       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
302       while (cont)
303         {
304           std::string stdfileName = crea::wx2std(fileName);
305           std::string folderName = stdfileName;
306           //check if they already exist
307           bool found = false;
308           for (int i = 0; !found && i < (int)(this->children.size()); i++)
309             {
310               if (this->children[i]->GetName() == folderName)
311                 {
312                   found = true;
313                   checked[i] = true;
314                   if(!this->children[i]->Refresh(result))
315                     return false;
316                 }
317             }
318           if(!found)
319             {
320               modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
321               this->children.push_back(folder);
322             }
323           cont = dir.GetNext(&fileName);
324         }
325
326       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
327       while (cont)
328         {
329           std::string stdfileName = crea::wx2std(fileName);
330
331           //if CMakeLists, create CMakeLists
332           if(stdfileName == "CMakeLists.txt")
333             {
334               if (this->CMakeLists == NULL)
335                 {
336                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
337                   this->children.push_back(this->CMakeLists);
338                 }
339               else
340                 {
341                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
342                   checked[pos] = true;
343                   if(!this->CMakeLists->Refresh(result))
344                     return false;
345                 }
346             }
347           //if is an unknown file, create file
348           else
349             {
350               bool found = false;
351               for (int i = 0; !found && i < (int)(this->children.size()); i++)
352                 {
353                   if (this->children[i]->GetName() == stdfileName)
354                     {
355                       found = true;
356                       checked[i] = true;
357                       if(!this->children[i]->Refresh(result))
358                         return false;
359                     }
360                 }
361
362               if(!found)
363                 {
364                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
365                   this->children.push_back(file);
366                 }
367             }
368
369           //if is a Black Box header, check in black boxes
370           if(stdfileName.substr(stdfileName.size() - 2, 2) == ".h" && stdfileName.substr(0,2) == "bb")
371             {
372               bool found = false;
373               for (int i = 0; i < (int)(this->blackBoxes.size()); i++)
374                 {
375                   if(this->blackBoxes[i]->GetHeaderFile()->GetName() == stdfileName)
376                     {
377                       checkedBoxes[i] = true;
378                       found = true;
379                       if(!this->blackBoxes[i]->Refresh(result))
380                         return false;
381                       break;
382                     }
383                 }
384
385               if (!found)
386                 {
387                   modelCDMBlackBox* blackBox = new modelCDMBlackBox(this, path, stdfileName.substr(2,stdfileName.size()-4), level + 1);
388                   this->blackBoxes.push_back(blackBox);
389                 }
390
391             }
392
393           cont = dir.GetNext(&fileName);
394         }
395     }
396
397   for (int i = 0; i < (int)(checkedBoxes.size()); i++)
398     {
399       if(!checkedBoxes[i])
400         {
401           delete this->blackBoxes[i];
402           this->blackBoxes.erase(this->blackBoxes.begin()+i);
403           checkedBoxes.erase(checkedBoxes.begin()+i);
404           i--;
405         }
406     }
407
408   for (int i = 0; i < (int)(checked.size()); i++)
409     {
410       if(!checked[i])
411         {
412           delete this->children[i];
413           this->children.erase(this->children.begin()+i);
414           checked.erase(checked.begin()+i);
415           i--;
416         }
417     }
418   this->SortChildren();
419   std::sort(this->blackBoxes.begin(), this->blackBoxes.end(), CompareNodeItem);
420   return true;
421 }