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