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