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