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