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