]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMAppli.cpp
586c550dcf01e0e1263297853403956d31839671
[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& name,
125     std::string*& result
126 )
127 {
128   //copy template application folder with new name
129   std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
130   if(system(copyCommand.c_str()))
131     {
132       result = new std::string("An error occurred while running '" + copyCommand + "'.");
133       return NULL;
134     }
135   //set name of library in CMakeLists inside copied folder
136   std::string line;
137   std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
138   if( !in.is_open())
139     {
140       result = new std::string("CMakeLists.txt file failed to open.");
141       return NULL;
142     }
143   std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
144   while (getline(in, line))
145     {
146       if(line == "SET ( EXE_NAME   MyExe  )")
147         line = "SET ( EXE_NAME   " + name + "  )";
148       out << line << std::endl;
149     }
150   in.close();
151   out.close();
152   //delete old file and rename new file
153   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
154   if(system(renameCommand.c_str()))
155     {
156       result = new std::string("An error occurred while running '" + renameCommand + "'.");
157       return NULL;
158     }
159
160   //add application to model
161   modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
162   this->applications.push_back(application);
163   this->children.push_back(application);
164
165   this->SortChildren();
166
167   result = new std::string(this->path + CDMUtilities::SLASH + name);
168   return application;
169 }
170
171 const bool modelCDMAppli::Refresh(std::string*& result)
172 {
173   std::cout << "refreshing appli" << std::endl;
174   this->type = wxDIR_DIRS;
175
176   std::vector<bool> checked(this->children.size(), false);
177   std::vector<bool> checkedApplications(this->applications.size(), false);
178
179   //check all folders
180   wxDir dir(crea::std2wx((this->path).c_str()));
181   if (dir.IsOpened())
182     {
183       wxString fileName;
184       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
185       while (cont)
186         {
187           std::string stdfileName = crea::wx2std(fileName);
188
189           if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
190             {
191               std::string applicationName = stdfileName;
192               //check if application already exist
193               bool found = false;
194               for (int i = 0; !found && i < this->applications.size(); i++)
195                 {
196                   if (this->applications[i]->GetName() == applicationName)
197                     {
198                       found = true;
199                       int pos = std::find(this->children.begin(), this->children.end(), this->applications[i]) - this->children.begin();
200                       checked[pos] = true;
201                       checkedApplications[i] = true;
202                       if(!this->applications[i]->Refresh(result))
203                         return false;
204                     }
205                 }
206               if(!found)
207                 {
208                   modelCDMApplication* application= new modelCDMApplication(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
209                   this->applications.push_back(application);
210                   this->children.push_back(application);
211                 }
212             }
213           else
214             {
215               //check if folder already exist
216               bool found = false;
217               for (int i = 0; !found && i < this->children.size(); i++)
218                 {
219                   if (this->children[i]->GetName() == stdfileName)
220                     {
221                       found = true;
222                       checked[i] = true;
223
224                       if(!this->children[i]->Refresh(result))
225                         return false;
226                     }
227                 }
228               if(!found)
229                 {
230                   modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
231                   this->children.push_back(folder);
232                 }
233             }
234           cont = dir.GetNext(&fileName);
235         }
236
237       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
238       while (cont)
239         {
240           std::string stdfileName = crea::wx2std(fileName);
241
242           //if CMakeLists, create CMakeLists
243           if(stdfileName == "CMakeLists.txt")
244             {
245               if (this->CMakeLists == NULL)
246                 {
247                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
248                   this->children.push_back(this->CMakeLists);
249                 }
250               else
251                 {
252                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
253                   checked[pos] = true;
254                   if(!this->CMakeLists->Refresh(result))
255                     return false;
256                 }
257             }
258           //if is an unknown file, create file
259           else
260             {
261               bool found = false;
262               for (int i = 0; !found && i < this->children.size(); i++)
263                 {
264                   if (this->children[i]->GetName() == stdfileName)
265                     {
266                       found = true;
267                       checked[i] = true;
268                       if(!this->children[i]->Refresh(result))
269                         return false;
270                     }
271                 }
272
273               if(!found)
274                 {
275                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
276                   this->children.push_back(file);
277                 }
278             }
279
280           cont = dir.GetNext(&fileName);
281         }
282     }
283
284   for (int i = 0; i < checkedApplications.size(); i++)
285     {
286       if(!checkedApplications[i])
287         {
288           this->applications.erase(this->applications.begin()+i);
289           checkedApplications.erase(checkedApplications.begin()+i);
290           i--;
291         }
292     }
293   for (int i = 0; i < checked.size(); i++)
294     {
295       if(!checked[i])
296         {
297           delete this->children[i];
298           this->children.erase(this->children.begin()+i);
299           checked.erase(checked.begin()+i);
300           i--;
301         }
302     }
303   this->SortChildren();
304   std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
305   return true;
306 }
307
308 void modelCDMAppli::CheckStructure(std::map<std::string, bool>& properties)
309 {
310   //check cmake exist
311   if(this->CMakeLists != NULL)
312     {
313       //set properties parameters based on model
314       for (int i = 0; i < this->applications.size(); i++)
315         properties["appli add " + this->applications[i]->GetName()] = false;
316
317       //open cmakelists
318       std::ifstream confFile;
319       confFile.open((this->CMakeLists->GetPath()).c_str());
320
321       //take everything that is not commented
322       std::string fileContent;
323
324       std::string word;
325       std::vector<std::string> words;
326       while(confFile.is_open() && !confFile.eof())
327         {
328           std::getline(confFile,word, '\n');
329           if(word[0] != '#')
330             {
331               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
332               if (words.size() > 0)
333                 {
334                   word = words[0];
335                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
336                   for (int i = 0; i < words.size(); i++)
337                     {
338                       if(words[i].substr(0,2) == "//")
339                         break;
340                       fileContent += words[i] + " ";
341                     }
342                 }
343             }
344         }
345
346       //check every instruction
347       std::stringstream ss(fileContent);
348       while(!ss.eof())
349         {
350           std::getline(ss,word, '(');
351
352           //check instruction name
353           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
354
355           //add instructions
356           if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
357             {
358               std::getline(ss,word, ')');
359               //std::cout << word << std::endl;
360               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
361
362               if (words.size() > 0)
363                 {
364                   {
365                     properties["appli add " + words[0]] = true;
366                   }
367                 }
368             }
369         }
370
371     }
372
373   //check libraries' structure
374   for (int i = 0; i < this->applications.size(); i++)
375     {
376       properties["application " + this->applications[i]->GetName()] = true;
377       this->applications[i]->CheckStructure(properties);
378     }
379 }