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