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