]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMLibrary.cpp
f67999c06748b6441a43c538d07b7cbeb3a2c45c
[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 < 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 < 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   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
185   if(system(renameCommand.c_str()))
186     {
187       result = new std::string("An error occurred while running '" + renameCommand + "'.");
188       return false;
189     }
190
191   this->nameLibrary = fileNameReal;
192   return true;
193 }
194
195 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
196 {
197   //TODO:: mkdir depending on OS
198   std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
199   if(system(command.c_str()))
200     {
201       result = new std::string("Error executing: " + command + ".");
202       return NULL;
203     }
204   modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
205   this->folders.push_back(folder);
206   this->children.push_back(folder);
207
208   return folder;
209 }
210
211 const bool modelCDMLibrary::Refresh(std::string*& result)
212 {
213   std::cout << "refreshing library: " << this->nameLibrary << std::endl;
214   //set attributes
215   this->type = wxDIR_DIRS;
216
217   //open CMakeList
218   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
219
220   std::ifstream confFile;
221   confFile.open((pathMakeLists).c_str());
222
223   std::string word;
224   while(confFile.is_open() && !confFile.eof())
225     {
226       //get sets
227       std::getline(confFile,word,'(');
228       std::vector<std::string> wordBits;
229       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
230
231       if(wordBits[wordBits.size()-1] == "SET")
232         {
233           //get library name
234           std::getline(confFile,word,')');
235           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
236           if(wordBits[0] == "LIBRARY_NAME")
237             {
238               word = wordBits[1];
239               for (int i = 2; i < wordBits.size(); i++)
240                 {
241                   word += " " + wordBits[i];
242                 }
243
244               this->nameLibrary = word;
245             }
246         }
247     }
248
249   confFile.close();
250
251   //check children
252   std::vector<bool> checked(this->children.size(), false);
253   std::vector<bool> checkedFolders(this->folders.size(), false);
254
255   //check all folders
256   wxDir dir(crea::std2wx((this->path).c_str()));
257   if (dir.IsOpened())
258     {
259       wxString fileName;
260       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
261       while (cont)
262         {
263           std::string stdfileName = crea::wx2std(fileName);
264           //check if they already exist
265           bool found = false;
266           for (int i = 0; !found && i < this->folders.size(); i++)
267             {
268               if (this->folders[i]->GetName() == stdfileName)
269                 {
270                   found = true;
271                   int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
272                   checked[pos] = true;
273                   checkedFolders[i] = true;
274                   if(!this->folders[i]->Refresh(result))
275                     return false;
276                 }
277             }
278           if(!found)
279             {
280               modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
281               this->folders.push_back(folder);
282               this->children.push_back(folder);
283             }
284           cont = dir.GetNext(&fileName);
285         }
286
287       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
288       while (cont)
289         {
290           std::string stdfileName = crea::wx2std(fileName);
291
292           //if CMakeLists, create CMakeLists
293           if(stdfileName == "CMakeLists.txt")
294             {
295               if (this->CMakeLists == NULL)
296                 {
297                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
298                   this->children.push_back(this->CMakeLists);
299                 }
300               else
301                 {
302                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
303                   checked[pos] = true;
304                   if(!this->CMakeLists->Refresh(result))
305                     return false;
306                 }
307             }
308           //if is an unknown file, check if exist in children
309           else
310             {
311               bool found = false;
312               for (int i = 0; !found && i < this->children.size(); i++)
313                 {
314                   if (this->children[i]->GetName() == stdfileName)
315                     {
316                       found = true;
317                       checked[i] = true;
318                       if(!this->children[i]->Refresh(result))
319                         return false;
320                     }
321                 }
322
323               if(!found)
324                 {
325                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
326                   this->children.push_back(file);
327                 }
328             }
329
330           cont = dir.GetNext(&fileName);
331         }
332     }
333
334   for (int i = 0; i < checkedFolders.size(); i++)
335     {
336       if(!checkedFolders[i])
337         {
338           this->folders.erase(this->folders.begin()+i);
339           checkedFolders.erase(checkedFolders.begin()+i);
340           i--;
341         }
342     }
343   for (int i = 0; i < checked.size(); i++)
344     {
345       if(!checked[i])
346         {
347           delete this->children[i];
348           this->children.erase(this->children.begin()+i);
349           checked.erase(checked.begin()+i);
350           i--;
351         }
352     }
353   this->SortChildren();
354   std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
355   return true;
356 }
357
358 void modelCDMLibrary::CheckStructure(std::map<std::string, bool>& properties)
359 {
360   //check cmake exist
361   if(this->CMakeLists != NULL)
362     {
363       //set default values
364       properties["library " + this->name + " lib ${crea_LIBRARIES}"] = false;
365       properties["library " + this->name + " lib ${WXWIDGETS_LIBRARIES}"] = false;
366       properties["library " + this->name + " lib ${KWWidgets_LIBRARIES}"] = false;
367       properties["library " + this->name + " lib ${VTK_LIBRARIES}"] = false;
368       properties["library " + this->name + " lib ${ITK_LIBRARIES}"] = false;
369       properties["library " + this->name + " lib ${GDCM_LIBRARIES}"] = false;
370       properties["library " + this->name + " lib ${BOOST_LIBRARIES}"] = false;
371
372
373       //open cmakelists
374       std::ifstream confFile;
375       confFile.open((this->CMakeLists->GetPath()).c_str());
376
377       //take everything that is not commented
378       std::string fileContent;
379
380       std::string word;
381       std::vector<std::string> words;
382       while(confFile.is_open() && !confFile.eof())
383         {
384           std::getline(confFile,word, '\n');
385           if(word[0] != '#')
386             {
387               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
388               if (words.size() > 0)
389                 {
390                   word = words[0];
391                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
392                   for (int i = 0; i < words.size(); i++)
393                     {
394                       if(words[i].substr(0,2) == "//")
395                         break;
396                       fileContent += words[i] + " ";
397                     }
398                 }
399             }
400         }
401
402       //check every instruction
403       std::stringstream ss(fileContent);
404       while(!ss.eof())
405         {
406           std::getline(ss,word, '(');
407
408           //check instruction name
409           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
410
411           //set instructions
412           if (words.size() > 0 && words[words.size()-1] == "SET")
413             {
414               std::getline(ss,word, ')');
415
416               CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
417               if (words.size() > 1)
418                 {
419                   if (words[0] == "${LIBRARY_NAME}_LINK_LIBRARIES")
420                     {
421                       for (int i = 1; i < words.size(); i++)
422                         {
423                           properties["library " + this->name + " lib " + words[i]] = true;
424                         }
425                     }
426                 }
427             }
428         }
429
430     }
431 }