]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMLib.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMLib.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  * modelCDMLib.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMLib.h"
36
37 #include <fstream>
38 #include <algorithm>
39
40 #include "CDMUtilities.h"
41 #include "creaWx.h"
42 #include "wx/dir.h"
43
44 modelCDMLib::modelCDMLib()
45 {
46 }
47
48 modelCDMLib::modelCDMLib(const std::string& path, const std::string& name, const int& level)
49 {
50   std::cout << "creating lib\n";
51   this->type = wxDIR_DIRS;
52   this->name = name;
53   this->level = level;
54   this->path = path;
55
56
57
58   this->path = CDMUtilities::fixPath(path);
59   //open makelists file
60   std::string pathFixed(CDMUtilities::fixPath(path));
61
62   this->libraries.clear();
63   wxDir dir(crea::std2wx((pathFixed).c_str()));
64   if (dir.IsOpened())
65     {
66       wxString fileName;
67       //folders
68       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
69       while (cont)
70         {
71           std::string stdfileName = crea::wx2std(fileName);
72
73           if(stdfileName != "template_lib")
74             {
75               modelCDMLibrary* library = new modelCDMLibrary(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
76               this->libraries.push_back(library);
77               this->children.push_back(library);
78             }
79           else
80             {
81               modelCDMFolder* folder = new modelCDMFolder(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
82               this->children.push_back(folder);
83             }
84
85           cont = dir.GetNext(&fileName);
86         }
87       //files
88       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
89       while (cont)
90         {
91           std::string stdfileName = crea::wx2std(fileName);
92
93           //if CMakeLists, create CMakeLists
94           if(stdfileName == "CMakeLists.txt")
95             {
96               this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
97               this->children.push_back(this->CMakeLists);
98             }
99           //if is an unknown file, create file
100           else
101             {
102               this->children.push_back(new modelCDMFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
103             }
104
105           cont = dir.GetNext(&fileName);
106         }
107     }
108   this->SortChildren();
109   std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
110 }
111
112 modelCDMLib::~modelCDMLib()
113 {
114 }
115
116 const std::vector<modelCDMLibrary*>& modelCDMLib::GetLibraries() const
117 {
118   return this->libraries;
119 }
120
121 modelCDMLibrary* modelCDMLib::CreateLibrary(
122     const std::string& name,
123     std::string*& result
124 )
125 {
126   //copy template library folder with new name
127   std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_lib\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
128   if(system(copyCommand.c_str()))
129     {
130       result = new std::string("An error occurred while running '" + copyCommand + "'.");
131       return NULL;
132     }
133   //set name of library in CMakeLists inside copied folder
134   std::string line;
135   std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
136   if( !in.is_open())
137     {
138       result = new std::string("CMakeLists.txt file failed to open.");
139       return NULL;
140     }
141   std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
142   while (getline(in, line))
143     {
144       if(line == "SET ( LIBRARY_NAME   MyLib  )")
145         line = "SET ( LIBRARY_NAME   " + name + "  )";
146       out << line << std::endl;
147     }
148   in.close();
149   out.close();
150   //delete old file and rename new file
151   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
152   if(system(renameCommand.c_str()))
153     {
154       result = new std::string("An error occurred while running '" + renameCommand + "'.");
155       return NULL;
156     }
157
158   //add library to model
159   modelCDMLibrary* library = new modelCDMLibrary(this->path + CDMUtilities::SLASH + name, name, this->level + 1);
160   this->libraries.push_back(library);
161   this->children.push_back(library);
162
163   this->SortChildren();
164
165   result = new std::string(this->path + CDMUtilities::SLASH + name);
166   return library;
167 }
168
169 const bool modelCDMLib::Refresh(std::string*& result)
170 {
171   std::cout << "refreshing lib" << std::endl;
172   this->type = wxDIR_DIRS;
173
174
175
176   std::vector<bool> checked(this->children.size(), false);
177   std::vector<bool> checkedLibraries(this->libraries.size(), false);
178
179   //check all folders
180   wxDir dir(crea::std2wx((this->path).c_str()));
181   if (dir.IsOpened())
182     {
183       wxString fileName;
184       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
185       while (cont)
186         {
187           std::string stdfileName = crea::wx2std(fileName);
188
189           if(stdfileName != "template_lib")
190             {
191               std::string libraryName = stdfileName;
192               //check if library already exist
193               bool found = false;
194               for (int i = 0; !found && i < this->libraries.size(); i++)
195                 {
196                   if (this->libraries[i]->GetName() == libraryName)
197                     {
198                       found = true;
199                       int pos = std::find(this->children.begin(), this->children.end(), this->libraries[i]) - this->children.begin();
200                       checked[pos] = true;
201                       checkedLibraries[i] = true;
202                       if(!this->libraries[i]->Refresh(result))
203                         return false;
204                     }
205                 }
206               if(!found)
207                 {
208                   modelCDMLibrary* library = new modelCDMLibrary(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
209                   this->libraries.push_back(library);
210                   this->children.push_back(library);
211                 }
212             }
213           else
214             {
215               //check if folder already exist
216               bool found = false;
217               for (int i = 0; !found && i < this->children.size(); i++)
218                 {
219                   if (this->children[i]->GetName() == stdfileName)
220                     {
221                       found = true;
222                       checked[i] = true;
223                       if(!this->children[i]->Refresh(result))
224                         return false;
225                     }
226                 }
227               if(!found)
228                 {
229                   modelCDMFolder* folder= new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
230                   this->children.push_back(folder);
231                 }
232             }
233           cont = dir.GetNext(&fileName);
234         }
235
236       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
237       while (cont)
238         {
239           std::string stdfileName = crea::wx2std(fileName);
240
241           //if CMakeLists, create CMakeLists
242           if(stdfileName == "CMakeLists.txt")
243             {
244               if (this->CMakeLists == NULL)
245                 {
246                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
247                   this->children.push_back(this->CMakeLists);
248                 }
249               else
250                 {
251                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
252                   checked[pos] = true;
253                   if(!this->CMakeLists->Refresh(result))
254                     return false;
255                 }
256             }
257           //if is an unknown file, create file
258           else
259             {
260               bool found = false;
261               for (int i = 0; !found && i < this->children.size(); i++)
262                 {
263                   if (this->children[i]->GetName() == stdfileName)
264                     {
265                       found = true;
266                       checked[i] = true;
267                       if(!this->children[i]->Refresh(result))
268                         return false;
269                     }
270                 }
271
272               if(!found)
273                 {
274                   modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
275                   this->children.push_back(file);
276                 }
277             }
278
279           cont = dir.GetNext(&fileName);
280         }
281     }
282
283   for (int i = 0; i < checkedLibraries.size(); i++)
284     {
285       if(!checkedLibraries[i])
286         {
287           this->libraries.erase(this->libraries.begin()+i);
288           checkedLibraries.erase(checkedLibraries.begin()+i);
289           i--;
290         }
291     }
292   for (int i = 0; i < checked.size(); i++)
293     {
294       if(!checked[i])
295         {
296           delete this->children[i];
297           this->children.erase(this->children.begin()+i);
298           checked.erase(checked.begin()+i);
299           i--;
300         }
301     }
302   this->SortChildren();
303   std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
304   return true;
305 }