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