]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMAppli.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMAppli.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  * modelCDMAppli.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMAppli.h"
36
37 #include <iostream>
38 #include <fstream>
39 #include <sstream>
40 #include <algorithm>
41
42 #include "CDMUtilities.h"
43 #include "creaWx.h"
44 #include "wx/dir.h"
45
46 modelCDMAppli::modelCDMAppli()
47 {
48 }
49
50 modelCDMAppli::modelCDMAppli(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
51 {
52   std::cout << "creating appli\n";
53   this->parent = parent;
54   this->type = wxDIR_DIRS;
55   this->name = name;
56   this->level = level;
57   this->path = path;
58
59
60
61   this->path = CDMUtilities::fixPath(path);
62   std::string pathFixed(CDMUtilities::fixPath(path));
63
64   this->applications.clear();
65   wxDir dir(crea::std2wx((pathFixed).c_str()));
66   if (dir.IsOpened())
67     {
68       wxString fileName;
69       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
70       while (cont)
71         {
72           std::string stdfileName = crea::wx2std(fileName);
73
74           if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
75             {
76               modelCDMApplication* application = new modelCDMApplication(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
77               this->applications.push_back(application);
78               this->children.push_back(application);
79             }
80           else
81             {
82               modelCDMFolder* folder = new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
83               this->children.push_back(folder);
84             }
85
86           cont = dir.GetNext(&fileName);
87         }
88       //files
89       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
90       while (cont)
91         {
92           std::string stdfileName = crea::wx2std(fileName);
93
94           //if CMakeLists, create CMakeLists
95           if(stdfileName == "CMakeLists.txt")
96             {
97               this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
98               this->children.push_back(this->CMakeLists);
99             }
100           //if is an unknown file, create file
101           else
102             {
103               this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
104             }
105
106           cont = dir.GetNext(&fileName);
107         }
108
109     }
110   this->SortChildren();
111   std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
112 }
113
114 modelCDMAppli::~modelCDMAppli()
115 {
116 }
117
118 const std::vector<modelCDMApplication*>& modelCDMAppli::GetApplications() const
119 {
120   return this->applications;
121 }
122
123 modelCDMApplication* modelCDMAppli::CreateApplication(
124     const std::string& namein,
125     std::string*& result
126 )
127 {
128   std::vector<std::string> words;
129   CDMUtilities::splitter::split(words,namein," '/\\*\"%",CDMUtilities::splitter::no_empties);
130   std::string name;
131   for (int i = 0; i < (int)(words.size()); i++)
132     {
133           name += words[i];
134     }
135   if (name == "")
136     {
137       result = new std::string("The given name is not valid:  '/\\*\"% are forbidden.");
138           return NULL;
139     }
140   //copy template application folder with new name
141 #ifdef _WIN32
142   std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
143 #else
144   std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
145 #endif
146
147   if(system(copyCommand.c_str()))
148     {
149       result = new std::string("An error occurred while running '" + copyCommand + "'.");
150       return NULL;
151     }
152   //set name of library in CMakeLists inside copied folder
153   std::string line;
154   std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
155   if( !in.is_open())
156     {
157       result = new std::string("CMakeLists.txt file failed to open.");
158       return NULL;
159     }
160   std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
161   while (getline(in, line))
162     {
163       if(line == "SET ( EXE_NAME   MyExe  )")
164         line = "SET ( EXE_NAME   " + name + "  )";
165       out << line << std::endl;
166     }
167   in.close();
168   out.close();
169   //delete old file and rename new file
170 #ifdef _WIN32
171   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
172 #else
173   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
174 #endif
175
176   if(system(renameCommand.c_str()))
177     {
178       result = new std::string("An error occurred while running '" + renameCommand + "'.");
179       return NULL;
180     }
181
182   //add application to model
183   modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
184   this->applications.push_back(application);
185   this->children.push_back(application);
186
187   this->SortChildren();
188
189   result = new std::string(this->path + CDMUtilities::SLASH + name);
190   return application;
191 }
192
193 const bool modelCDMAppli::Refresh(std::string*& result)
194 {
195   std::cout << "refreshing appli" << std::endl;
196   this->type = wxDIR_DIRS;
197
198   std::vector<bool> checked(this->children.size(), false);
199   std::vector<bool> checkedApplications(this->applications.size(), false);
200
201   //check all folders
202   wxDir dir(crea::std2wx((this->path).c_str()));
203   if (dir.IsOpened())
204     {
205       wxString fileName;
206       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
207       while (cont)
208         {
209           std::string stdfileName = crea::wx2std(fileName);
210
211           if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
212             {
213               std::string applicationName = stdfileName;
214               //check if application already exist
215               bool found = false;
216               for (int i = 0; !found && i < (int)(this->applications.size()); i++)
217                 {
218                   if (this->applications[i]->GetName() == applicationName)
219                     {
220                       found = true;
221                       int pos = std::find(this->children.begin(), this->children.end(), this->applications[i]) - this->children.begin();
222                       checked[pos] = true;
223                       checkedApplications[i] = true;
224                       if(!this->applications[i]->Refresh(result))
225                         return false;
226                     }
227                 }
228               if(!found)
229                 {
230                   modelCDMApplication* application= new modelCDMApplication(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
231                   this->applications.push_back(application);
232                   this->children.push_back(application);
233                 }
234             }
235           else
236             {
237               //check if folder already exist
238               bool found = false;
239               for (int i = 0; !found && i < (int)(this->children.size()); i++)
240                 {
241                   if (this->children[i]->GetName() == stdfileName)
242                     {
243                       found = true;
244                       checked[i] = true;
245
246                       if(!this->children[i]->Refresh(result))
247                         return false;
248                     }
249                 }
250               if(!found)
251                 {
252                   modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
253                   this->children.push_back(folder);
254                 }
255             }
256           cont = dir.GetNext(&fileName);
257         }
258
259       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
260       while (cont)
261         {
262           std::string stdfileName = crea::wx2std(fileName);
263
264           //if CMakeLists, create CMakeLists
265           if(stdfileName == "CMakeLists.txt")
266             {
267               if (this->CMakeLists == NULL)
268                 {
269                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
270                   this->children.push_back(this->CMakeLists);
271                 }
272               else
273                 {
274                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
275                   checked[pos] = true;
276                   if(!this->CMakeLists->Refresh(result))
277                     return false;
278                 }
279             }
280           //if is an unknown file, create file
281           else
282             {
283               bool found = false;
284               for (int i = 0; !found && i < (int)(this->children.size()); i++)
285                 {
286                   if (this->children[i]->GetName() == stdfileName)
287                     {
288                       found = true;
289                       checked[i] = true;
290                       if(!this->children[i]->Refresh(result))
291                         return false;
292                     }
293                 }
294
295               if(!found)
296                 {
297                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
298                   this->children.push_back(file);
299                 }
300             }
301
302           cont = dir.GetNext(&fileName);
303         }
304     }
305
306   for (int i = 0; i < (int)(checkedApplications.size()); i++)
307     {
308       if(!checkedApplications[i])
309         {
310           this->applications.erase(this->applications.begin()+i);
311           checkedApplications.erase(checkedApplications.begin()+i);
312           i--;
313         }
314     }
315   for (int i = 0; i < (int)(checked.size()); i++)
316     {
317       if(!checked[i])
318         {
319           delete this->children[i];
320           this->children.erase(this->children.begin()+i);
321           checked.erase(checked.begin()+i);
322           i--;
323         }
324     }
325   this->SortChildren();
326   std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
327   return true;
328 }
329
330 void modelCDMAppli::CheckStructure(std::map<std::string, bool>& properties)
331 {
332   //check cmake exist
333   if(this->CMakeLists != NULL)
334     {
335       //set properties parameters based on model
336       for (int i = 0; i < (int)(this->applications.size()); i++)
337         properties["appli add " + this->applications[i]->GetName()] = false;
338
339       //open cmakelists
340       std::ifstream confFile;
341       confFile.open((this->CMakeLists->GetPath()).c_str());
342
343       //take everything that is not commented
344       std::string fileContent;
345
346       std::string word;
347       std::vector<std::string> words;
348       while(confFile.is_open() && !confFile.eof())
349         {
350           std::getline(confFile,word, '\n');
351           if(word[0] != '#')
352             {
353               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
354               if (words.size() > 0)
355                 {
356                   word = words[0];
357                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
358                   for (int i = 0; i < (int)(words.size()); i++)
359                     {
360                       if(words[i].substr(0,2) == "//")
361                         break;
362                       fileContent += words[i] + " ";
363                     }
364                 }
365             }
366         }
367
368       //check every instruction
369       std::stringstream ss(fileContent);
370       while(!ss.eof())
371         {
372           std::getline(ss,word, '(');
373
374           //check instruction name
375           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
376
377           //add instructions
378           if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
379             {
380               std::getline(ss,word, ')');
381               //std::cout << word << std::endl;
382               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
383
384               if (words.size() > 0)
385                 {
386                   {
387                     properties["appli add " + words[0]] = true;
388                   }
389                 }
390             }
391         }
392
393     }
394
395   //check libraries' structure
396   for (int i = 0; i < (int)(this->applications.size()); i++)
397     {
398       properties["application " + this->applications[i]->GetName()] = true;
399       this->applications[i]->CheckStructure(properties);
400     }
401 }