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