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