]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMLibrary.cpp
7b99318b58333425375949bdd6d8b689dd4d5fe8
[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 <sstream>
39 #include <algorithm>
40
41 #include "CDMUtilities.h"
42 #include "creaWx.h"
43 #include "wx/dir.h"
44
45 modelCDMLibrary::modelCDMLibrary()
46 {
47 }
48
49 modelCDMLibrary::modelCDMLibrary(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
50 {
51   std::cout << "creating library: " + path + "\n";
52   this->parent = parent;
53   //folder name
54   this->name = name;
55   //path
56   this->path = CDMUtilities::fixPath(path);
57   //type
58   this->type = wxDIR_DIRS;
59   //level
60   this->level = level;
61
62   //open CMakeList
63   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
64
65   std::ifstream confFile;
66   confFile.open((pathMakeLists).c_str());
67
68   std::string word;
69   while(confFile.is_open() && !confFile.eof())
70     {
71       //get sets
72       std::getline(confFile,word,'(');
73       std::vector<std::string> wordBits;
74       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
75
76       if(wordBits[wordBits.size()-1] == "SET")
77         {
78           //get library name
79           std::getline(confFile,word,')');
80           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
81           if(wordBits[0] == "LIBRARY_NAME")
82             {
83               word = wordBits[1];
84               for (int i = 2; i < (int)(wordBits.size()); i++)
85                 {
86                   word += " " + wordBits[i];
87                 }
88
89               this->nameLibrary = word;
90             }
91         }
92     }
93
94   confFile.close();
95
96   //add library contents
97
98   this->children.clear();
99   wxDir dir(crea::std2wx((this->path).c_str()));
100   if (dir.IsOpened())
101     {
102       wxString fileName;
103       //folders
104       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
105       while (cont)
106         {
107           std::string stdfileName = crea::wx2std(fileName);
108
109           modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
110           this->folders.push_back(folder);
111           this->children.push_back(folder);
112
113           cont = dir.GetNext(&fileName);
114         }
115       //files
116       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
117       while (cont)
118         {
119           std::string stdfileName = crea::wx2std(fileName);
120
121           //if CMakeLists, create CMakeLists
122           if(stdfileName == "CMakeLists.txt")
123             {
124               this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
125               this->children.push_back(this->CMakeLists);
126             }
127           //if is an unknown file, create file
128           else
129             {
130               this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
131             }
132
133           cont = dir.GetNext(&fileName);
134         }
135     }
136   this->SortChildren();
137   std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
138 }
139
140 modelCDMLibrary::~modelCDMLibrary()
141 {
142 }
143
144 const std::string& modelCDMLibrary::GetNameLibrary() const
145 {
146   return this->nameLibrary;
147 }
148
149 bool modelCDMLibrary::SetNameLibrary(const std::string& fileName, std::string*& result)
150 {
151   std::vector<std::string> words;
152   CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
153   std::string fileNameReal = words[0];
154   for (int i = 1; i < (int)(words.size()); i++)
155     {
156       fileNameReal += "-" + words[i];
157     }
158
159   std::string line;
160   //opening original cmakelists
161   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
162   if( !in.is_open())
163     {
164       result = new std::string("CMakeLists.txt file failed to open.");
165       return false;
166     }
167   //opening temporal cmakelists
168   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
169   if( !out.is_open())
170     {
171       result = new std::string("CMakeLists.txt.tmp file failed to open.");
172       return false;
173     }
174   //copying contents from original to temporal and making changes
175   while (getline(in, line))
176     {
177       if(line.find("SET ( LIBRARY_NAME") != std::string::npos)
178         line = "SET ( LIBRARY_NAME  " + fileNameReal + "  )";
179       out << line << std::endl;
180     }
181   in.close();
182   out.close();
183   //delete old file and rename new file
184 #ifdef _WIN32
185   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
186 #else
187   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
188 #endif
189   if(system(renameCommand.c_str()))
190     {
191       result = new std::string("An error occurred while running '" + renameCommand + "'.");
192       return false;
193     }
194
195   this->nameLibrary = fileNameReal;
196   return true;
197 }
198
199 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
200 {
201   //TODO:: mkdir depending on OS
202   std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
203   if(system(command.c_str()))
204     {
205       result = new std::string("Error executing: " + command + ".");
206       return NULL;
207     }
208   modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
209   this->folders.push_back(folder);
210   this->children.push_back(folder);
211
212   return folder;
213 }
214
215 const bool modelCDMLibrary::Refresh(std::string*& result)
216 {
217   std::cout << "refreshing library: " << this->nameLibrary << std::endl;
218   //set attributes
219   this->type = wxDIR_DIRS;
220
221   //open CMakeList
222   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
223
224   std::ifstream confFile;
225   confFile.open((pathMakeLists).c_str());
226
227   std::string word;
228   while(confFile.is_open() && !confFile.eof())
229     {
230       //get sets
231       std::getline(confFile,word,'(');
232       std::vector<std::string> wordBits;
233       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
234
235       if(wordBits[wordBits.size()-1] == "SET")
236         {
237           //get library name
238           std::getline(confFile,word,')');
239           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
240           if(wordBits[0] == "LIBRARY_NAME")
241             {
242               word = wordBits[1];
243               for (int i = 2; i < (int)(wordBits.size()); i++)
244                 {
245                   word += " " + wordBits[i];
246                 }
247
248               this->nameLibrary = word;
249             }
250         }
251     }
252
253   confFile.close();
254
255   //check children
256   std::vector<bool> checked(this->children.size(), false);
257   std::vector<bool> checkedFolders(this->folders.size(), false);
258
259   //check all folders
260   wxDir dir(crea::std2wx((this->path).c_str()));
261   if (dir.IsOpened())
262     {
263       wxString fileName;
264       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
265       while (cont)
266         {
267           std::string stdfileName = crea::wx2std(fileName);
268           //check if they already exist
269           bool found = false;
270           for (int i = 0; !found && i < (int)(this->folders.size()); i++)
271             {
272               if (this->folders[i]->GetName() == stdfileName)
273                 {
274                   found = true;
275                   int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
276                   checked[pos] = true;
277                   checkedFolders[i] = true;
278                   if(!this->folders[i]->Refresh(result))
279                     return false;
280                 }
281             }
282           if(!found)
283             {
284               modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
285               this->folders.push_back(folder);
286               this->children.push_back(folder);
287             }
288           cont = dir.GetNext(&fileName);
289         }
290
291       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
292       while (cont)
293         {
294           std::string stdfileName = crea::wx2std(fileName);
295
296           //if CMakeLists, create CMakeLists
297           if(stdfileName == "CMakeLists.txt")
298             {
299               if (this->CMakeLists == NULL)
300                 {
301                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
302                   this->children.push_back(this->CMakeLists);
303                 }
304               else
305                 {
306                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
307                   checked[pos] = true;
308                   if(!this->CMakeLists->Refresh(result))
309                     return false;
310                 }
311             }
312           //if is an unknown file, check if exist in children
313           else
314             {
315               bool found = false;
316               for (int i = 0; !found && i < (int)(this->children.size()); i++)
317                 {
318                   if (this->children[i]->GetName() == stdfileName)
319                     {
320                       found = true;
321                       checked[i] = true;
322                       if(!this->children[i]->Refresh(result))
323                         return false;
324                     }
325                 }
326
327               if(!found)
328                 {
329                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
330                   this->children.push_back(file);
331                 }
332             }
333
334           cont = dir.GetNext(&fileName);
335         }
336     }
337
338   for (int i = 0; i < (int)(checkedFolders.size()); i++)
339     {
340       if(!checkedFolders[i])
341         {
342           this->folders.erase(this->folders.begin()+i);
343           checkedFolders.erase(checkedFolders.begin()+i);
344           i--;
345         }
346     }
347   for (int i = 0; i < (int)(checked.size()); i++)
348     {
349       if(!checked[i])
350         {
351           delete this->children[i];
352           this->children.erase(this->children.begin()+i);
353           checked.erase(checked.begin()+i);
354           i--;
355         }
356     }
357   this->SortChildren();
358   std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
359   return true;
360 }
361
362 void modelCDMLibrary::CheckStructure(std::map<std::string, bool>& properties)
363 {
364   //check cmake exist
365   if(this->CMakeLists != NULL)
366     {
367       //set default values
368       properties["library " + this->name + " lib ${crea_LIBRARIES}"] = false;
369       properties["library " + this->name + " lib ${WXWIDGETS_LIBRARIES}"] = false;
370       properties["library " + this->name + " lib ${KWWidgets_LIBRARIES}"] = false;
371       properties["library " + this->name + " lib ${VTK_LIBRARIES}"] = false;
372       properties["library " + this->name + " lib ${ITK_LIBRARIES}"] = false;
373       properties["library " + this->name + " lib ${GDCM_LIBRARIES}"] = false;
374       properties["library " + this->name + " lib ${BOOST_LIBRARIES}"] = false;
375
376
377       //open cmakelists
378       std::ifstream confFile;
379       confFile.open((this->CMakeLists->GetPath()).c_str());
380
381       //take everything that is not commented
382       std::string fileContent;
383
384       std::string word;
385       std::vector<std::string> words;
386       while(confFile.is_open() && !confFile.eof())
387         {
388           std::getline(confFile,word, '\n');
389           if(word[0] != '#')
390             {
391               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
392               if (words.size() > 0)
393                 {
394                   word = words[0];
395                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
396                   for (int i = 0; i < (int)(words.size()); i++)
397                     {
398                       if(words[i].substr(0,2) == "//")
399                         break;
400                       fileContent += words[i] + " ";
401                     }
402                 }
403             }
404         }
405
406       //check every instruction
407       std::stringstream ss(fileContent);
408       while(!ss.eof())
409         {
410           std::getline(ss,word, '(');
411
412           //check instruction name
413           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
414
415           //set instructions
416           if (words.size() > 0 && words[words.size()-1] == "SET")
417             {
418               std::getline(ss,word, ')');
419
420               CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
421               if (words.size() > 1)
422                 {
423                   if (words[0] == "${LIBRARY_NAME}_LINK_LIBRARIES")
424                     {
425                       for (int i = 1; i < (int)(words.size()); i++)
426                         {
427                           properties["library " + this->name + " lib " + words[i]] = true;
428                         }
429                     }
430                 }
431             }
432         }
433
434     }
435 }