]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMPackage.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMPackage.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  * modelCDMPackage.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMPackage.h"
36
37 #include "modelCDMProject.h"
38 #include "modelCDMLib.h"
39 #include "modelCDMLibrary.h"
40
41
42 #include <fstream>
43 #include <sstream>
44 #include <algorithm>
45 #include <boost/regex.hpp>
46
47 #include "creaWx.h"
48 #include "wx/dir.h"
49 #include "CDMUtilities.h"
50
51 modelCDMPackage::modelCDMPackage()
52 {
53   this->src = NULL;
54 }
55
56 modelCDMPackage::modelCDMPackage(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
57 {
58   std::cout << "creating package: " + path + "\n";
59   this->parent = parent;
60   this->type = wxDIR_DIRS;
61   this->name = name;
62   //Get Package Name
63
64   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
65
66   std::ifstream confFile;
67   confFile.open((pathMakeLists).c_str());
68
69   std::string word;
70   while(confFile.is_open() && !confFile.eof())
71     {
72       //get sets
73       std::getline(confFile,word,'(');
74       std::vector<std::string> wordBits;
75       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
76
77       if(wordBits[wordBits.size()-1] == "SET")
78         {
79           //get package name
80           std::getline(confFile,word,')');
81           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
82           if(wordBits[0] == "BBTK_PACKAGE_NAME")
83             {
84               word = wordBits[1];
85               for (int i = 2; i < (int)(wordBits.size()); i++)
86                 {
87                   word += " " + wordBits[i];
88                 }
89               wordBits.clear();
90               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
91
92               this->namePackage = wordBits[0];
93             }
94           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
95             {
96               word = wordBits[1];
97               for (int i = 2; i < (int)(wordBits.size()); i++)
98                 {
99                   word += " " + wordBits[i];
100                 }
101               wordBits.clear();
102               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
103
104               this->authors = wordBits[0];
105             }
106           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
107             {
108               word = wordBits[1];
109               for (int i = 2; i < (int)(wordBits.size()); i++)
110                 {
111                   word += " " + wordBits[i];
112                 }
113               wordBits.clear();
114               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
115
116               this->description = wordBits[0];
117             }
118           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
119             {
120               this->version = wordBits[1];
121             }
122           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
123             {
124               this->version += "." + wordBits[1];
125             }
126           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
127             {
128               this->version += "." + wordBits[1];
129             }
130         }
131     }
132
133   this->level = level;
134   this->path = path;
135
136   //check all folders and files
137   wxDir dir(crea::std2wx((path).c_str()));
138   if (dir.IsOpened())
139     {
140       wxString fileName;
141
142       //folders
143       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
144       while (cont)
145         {
146           std::string stdfileName = crea::wx2std(fileName);
147           //if src, check for black boxes
148           if(stdfileName == "src")
149             {
150               this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
151               this->children.push_back(this->src);
152             }
153           else
154             {
155               this->children.push_back(new modelCDMFolder(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
156             }
157
158           cont = dir.GetNext(&fileName);
159         }
160
161       //files
162       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
163       while (cont)
164         {
165           std::string stdfileName = crea::wx2std(fileName);
166           std::size_t fileTypePos = stdfileName.find_last_of(".");
167           std::string fileType;
168           if(fileTypePos != std::string::npos)
169             fileType = stdfileName.substr(fileTypePos);
170           else
171             fileType = "";
172
173           //if CMakeLists, create CMakeLists
174           if(stdfileName == "CMakeLists.txt")
175             {
176               this->CMakeLists = new modelCDMCMakeListsFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
177               this->children.push_back(this->CMakeLists);
178             }
179           //if is a code file, create code file
180           else if(fileType == ".c" ||
181               fileType == ".cxx" ||
182               fileType == ".h" ||
183               fileType == ".cpp" ||
184               fileType == ".txx" ||
185               fileType == ".cmake" )
186             {
187               modelCDMCodeFile* file = new modelCDMCodeFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
188               this->children.push_back(file);
189             }
190           //if is an unknown file, create file
191           else
192             {
193               this->children.push_back(new modelCDMFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
194             }
195
196           cont = dir.GetNext(&fileName);
197         }
198     }
199   this->SortChildren();
200 }
201
202 modelCDMPackage::~modelCDMPackage()
203 {
204 }
205
206 const std::string& modelCDMPackage::GetNamePackage() const
207 {
208   return this->namePackage;
209 }
210
211 const std::string& modelCDMPackage::GetAuthors() const
212 {
213   return this->authors;
214 }
215
216 const std::string& modelCDMPackage::GetAuthorsEmail() const
217 {
218   return this->authorsEmail;
219 }
220
221 const std::string& modelCDMPackage::GetVersion() const
222 {
223   return this->version;
224 }
225
226 const std::string& modelCDMPackage::GetDescription() const
227 {
228   return this->description;
229 }
230
231 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
232 {
233   return this->src;
234 }
235
236 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
237 {
238   std::vector<std::string> words;
239   CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
240   std::string authorsReal = words[0];
241   for (int i = 1; i < (int)(words.size()); i++)
242     {
243       authorsReal += "/" + words[i];
244     }
245
246   std::string line;
247   //opening original cmakelists
248   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
249   if( !in.is_open())
250     {
251       result = new std::string("CMakeLists.txt file failed to open.");
252       return false;
253     }
254   //opening temporal cmakelists
255   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
256   if( !out.is_open())
257     {
258       result = new std::string("CMakeLists.txt.tmp file failed to open.");
259       return false;
260     }
261   //copying contents from original to temporal and making changes
262   while (getline(in, line))
263     {
264       if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
265         line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
266       out << line << std::endl;
267     }
268   in.close();
269   out.close();
270   //delete old file and rename new file
271 #ifdef _WIN32
272   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
273 #else
274   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
275 #endif
276   
277   if(system(renameCommand.c_str()))
278     {
279       result = new std::string("An error occurred while running '" + renameCommand + "'.");
280       return false;
281     }
282
283   this->authors = authorsReal;
284   return true;
285 }
286
287 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
288 {
289   //TODO: implement method
290   return true;
291 }
292
293 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
294 {
295   std::vector<std::string> vers;
296   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
297
298
299   std::string line;
300   //opening original cmakelists
301   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
302   if( !in.is_open())
303     {
304       result = new std::string("CMakeLists.txt file failed to open.");
305       return false;
306     }
307   //opening temporal cmakelists
308   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
309   if( !out.is_open())
310     {
311       result = new std::string("CMakeLists.txt.tmp file failed to open.");
312       return false;
313     }
314   //copying contents from original to temporal and making changes
315   while (getline(in, line))
316     {
317       if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
318         line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
319       else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
320         line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
321       else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
322         line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
323       out << line << std::endl;
324     }
325   in.close();
326   out.close();
327   //delete old file and rename new file
328 #ifdef _WIN32
329   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
330 #else
331   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
332 #endif
333   
334   if(system(renameCommand.c_str()))
335     {
336       result = new std::string("An error occurred while running '" + renameCommand + "'.");
337       return false;
338     }
339
340   this->version = vers[0] + "." + vers[1] + "." + vers[2];
341   return true;
342 }
343
344 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
345 {
346   std::vector<std::string> words;
347   CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
348   std::string descriptionReal = words[0];
349   for (int i = 1; i < (int)(words.size()); i++)
350     {
351       descriptionReal += " " + words[i];
352     }
353
354   std::string line;
355   //opening original cmakelists
356   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
357   if( !in.is_open())
358     {
359       result = new std::string("CMakeLists.txt file failed to open.");
360       return false;
361     }
362   //opening temporal cmakelists
363   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
364   if( !out.is_open())
365     {
366       result = new std::string("CMakeLists.txt.tmp file failed to open.");
367       return false;
368     }
369   //copying contents from original to temporal and making changes
370   while (getline(in, line))
371     {
372       if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
373         line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
374       out << line << std::endl;
375     }
376   in.close();
377   out.close();
378   //delete old file and rename new file
379 #ifdef _WIN32
380   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
381 #else
382   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
383 #endif
384   
385   if(system(renameCommand.c_str()))
386     {
387       result = new std::string("An error occurred while running '" + renameCommand + "'.");
388       return false;
389     }
390
391   this->description = descriptionReal;
392   return true;
393 }
394
395 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
396     std::string*& result,
397     const std::string& name,
398     const std::string& type,
399     const std::string& format,
400     const std::string& categories,
401     const std::string& authors,
402     const std::string& authorsEmail,
403     const std::string& description
404 )
405 {
406   return this->src->CreateBlackBox(result,name, this->namePackage, type,format,categories,authors,authorsEmail,description);
407 }
408
409 const bool modelCDMPackage::Refresh(std::string*& result)
410 {
411   std::cout << "refreshing package " << this->namePackage << std::endl;
412   this->type = wxDIR_DIRS;
413
414   //Get Package Name
415
416   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
417
418   std::ifstream confFile;
419   confFile.open((pathMakeLists).c_str());
420
421   std::string word;
422   while(confFile.is_open() && !confFile.eof())
423     {
424       //get sets
425       std::getline(confFile,word,'(');
426       std::vector<std::string> wordBits;
427       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
428
429       if(wordBits[wordBits.size()-1] == "SET")
430         {
431           //get package name
432           std::getline(confFile,word,')');
433           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
434           if(wordBits[0] == "BBTK_PACKAGE_NAME")
435             {
436               word = wordBits[1];
437               for (int i = 2; i < (int)(wordBits.size()); i++)
438                 {
439                   word += " " + wordBits[i];
440                 }
441               wordBits.clear();
442               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
443
444               this->namePackage = wordBits[0];
445             }
446           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
447             {
448               word = wordBits[1];
449               for (int i = 2; i < (int)(wordBits.size()); i++)
450                 {
451                   word += " " + wordBits[i];
452                 }
453               wordBits.clear();
454               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
455
456               this->authors = wordBits[0];
457             }
458           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
459             {
460               word = wordBits[1];
461               for (int i = 2; i < (int)(wordBits.size()); i++)
462                 {
463                   word += " " + wordBits[i];
464                 }
465               wordBits.clear();
466               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
467
468               this->description = wordBits[0];
469             }
470           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
471             {
472               this->version = wordBits[1];
473             }
474           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
475             {
476               this->version += "." + wordBits[1];
477             }
478           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
479             {
480               this->version += "." + wordBits[1];
481             }
482         }
483     }
484
485
486
487   std::vector<bool> checked(this->children.size(), false);
488   bool checkedSrc = false;
489
490   //check all folders
491   wxDir dir(crea::std2wx((this->path).c_str()));
492   if (dir.IsOpened())
493     {
494       wxString fileName;
495       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
496       while (cont)
497         {
498
499           std::string stdfileName = crea::wx2std(fileName);
500
501           //detect black boxes in src
502           if(stdfileName == "src")
503             {
504               //check if box already exist
505               bool found = false;
506               if (this->src != NULL)
507                 {
508                   found = true;
509                   int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
510                   checked[pos] = true;
511                   checkedSrc = true;
512                   if(!this->src->Refresh(result))
513                     return false;
514                 }
515               else
516                 {
517                   this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
518                   this->children.push_back(this->src);
519                 }
520             }
521           else
522             {
523
524               //check if folder already exist
525               bool found = false;
526               for (int i = 0; !found && i < (int)(this->children.size()); i++)
527                 {
528                   if (this->children[i]->GetName() == stdfileName)
529                     {
530                       found = true;
531                       checked[i] = true;
532                       if(!this->children[i]->Refresh(result))
533                         return false;
534                     }
535                 }
536               if(!found)
537                 {
538                   modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
539                   this->children.push_back(folder);
540                 }
541             }
542           cont = dir.GetNext(&fileName);
543
544         }
545
546       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
547       while (cont)
548         {
549           std::string stdfileName = crea::wx2std(fileName);
550           std::size_t fileTypePos = stdfileName.find_last_of(".");
551           std::string fileType;
552           if(fileTypePos != std::string::npos)
553             fileType = stdfileName.substr(fileTypePos);
554           else
555             fileType = "";
556
557           //if CMakeLists, create CMakeLists
558           if(stdfileName == "CMakeLists.txt")
559             {
560               if (this->CMakeLists == NULL)
561                 {
562                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
563                   this->children.push_back(this->CMakeLists);
564                 }
565               else
566                 {
567                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
568                   checked[pos] = true;
569                   if(!this->CMakeLists->Refresh(result))
570                     return false;
571                 }
572             }
573           //if is an unknown file, create file
574           else
575             {
576               bool found = false;
577               for (int i = 0; !found && i < (int)(this->children.size()); i++)
578                 {
579                   if (this->children[i]->GetName() == stdfileName)
580                     {
581                       found = true;
582                       checked[i] = true;
583                       if(!this->children[i]->Refresh(result))
584                         return false;
585                     }
586                 }
587
588               if(!found)
589                 {
590                   //if is a code file, create modelCDMCodeFile
591                   if(
592                       fileType == ".c" ||
593                       fileType == ".cxx" ||
594                       fileType == ".h" ||
595                       fileType == ".cpp" ||
596                       fileType == ".txx" ||
597                       fileType == ".cmake" )
598                     {
599                       this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
600                     }
601                   else
602                     {
603                       modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
604                       this->children.push_back(file);
605                     }
606                 }
607             }
608
609           cont = dir.GetNext(&fileName);
610         }
611     }
612
613   if(!checkedSrc)
614     {
615       this->src = NULL;
616     }
617
618   for (int i = 0; i < (int)(checked.size()); i++)
619     {
620       if(!checked[i])
621         {
622           delete this->children[i];
623           this->children.erase(this->children.begin()+i);
624           checked.erase(checked.begin()+i);
625           i--;
626         }
627     }
628   this->SortChildren();
629   return true;
630 }
631
632 void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
633 {
634   //check cmake exist
635   if(this->CMakeLists != NULL)
636     {
637       //open cmakelists
638       std::ifstream confFile;
639       confFile.open((this->CMakeLists->GetPath()).c_str());
640
641       //take everything that is not commented
642       std::string fileContent;
643
644       std::string word;
645       std::vector<std::string> words;
646       while(confFile.is_open() && !confFile.eof())
647         {
648           std::getline(confFile,word, '\n');
649           if(word[0] != '#')
650             {
651               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
652               if (words.size() > 0)
653                 {
654                   word = words[0];
655                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
656                   for (int i = 0; i < (int)(words.size()); i++)
657                     {
658                       if(words[i].substr(0,2) == "//")
659                         break;
660                       fileContent += words[i] + " ";
661                     }
662                 }
663             }
664         }
665
666       //check every instruction
667       std::stringstream ss(fileContent);
668       while(!ss.eof())
669         {
670           std::getline(ss,word, '(');
671
672           //check instruction name
673           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
674
675           //set instructions
676           if (words.size() > 0 && words[words.size()-1] == "SET")
677             {
678               std::getline(ss,word, ')');
679
680               CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
681               if (words.size() > 1)
682                 {
683                   if (words[0] == "${BBTK_PACKAGE_NAME}_USE_VTK" && words[1] == "ON")
684                     {
685                       properties["package " + this->name + " set USE_VTK"] = true;
686                     }
687                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_ITK" && words[1] == "ON")
688                     {
689                       properties["package " + this->name + " set USE_ITK"] = true;
690                     }
691                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM" && words[1] == "ON")
692                     {
693                       properties["package " + this->name + " set USE_GDCM"] = true;
694                     }
695                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM_VTK" && words[1] == "ON")
696                     {
697                       properties["package " + this->name + " set USE_GDCM_VTK"] = true;
698                     }
699                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GSMIS" && words[1] == "ON")
700                     {
701                       properties["package " + this->name + " set USE_GSMIS"] = true;
702                     }
703                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_WXWIDGETS" && words[1] == "ON")
704                     {
705                       properties["package " + this->name + " set USE_WXWIDGETS"] = true;
706                     }
707                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_KWWIDGETS" && words[1] == "ON")
708                     {
709                       properties["package " + this->name + " set USE_KWWIDGETS"] = true;
710                     }
711                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
712                     {
713                       properties["package " + this->name + " set USE_BOOST"] = true;
714                     }
715                   else if (words[0] == "${BBTK_PACKAGE_NAME}_INCLUDE_DIRS")
716                     {
717                       for (int i = 1; i < (int)(words.size()); i++)
718                         {
719                           if(words[i].substr(0,2) == "${" || words[i].substr(0,2) == "..")
720                           properties["package " + this->name + " dir " + words[i]] = true;
721                         }
722                     }
723                   else if (words[0] == "${BBTK_PACKAGE_NAME}_LIBS")
724                     {
725                       for (int i = 1; i < (int)(words.size()); i++)
726                         {
727                           properties["package " + this->name + " lib " + words[i]] = true;
728                         }
729                     }
730                 }
731             }
732         }
733
734     }
735 }
736
737 std::map<std::string, bool> modelCDMPackage::Get3rdPartyLibraries()
738 {
739   std::map<std::string, std::string> correspondence;
740   correspondence["${BBTK_PACKAGE_NAME}_USE_VTK"] = "VTK";
741   correspondence["${BBTK_PACKAGE_NAME}_USE_ITK"] = "ITK";
742   correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM"] = "GDCM";
743   correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM_VTK"] = "GDCM_VTK";
744   correspondence["${BBTK_PACKAGE_NAME}_USE_GSMIS"] = "GSMIS";
745   correspondence["${BBTK_PACKAGE_NAME}_USE_WXWIDGETS"] = "WxWidgets";
746   correspondence["${BBTK_PACKAGE_NAME}_USE_KWWIDGETS"] = "KWWidgets";
747   std::map<std::string, bool> res;
748   res["VTK"] = false;
749   res["ITK"] = false;
750   res["GDCM"] = false;
751   res["GDCM_VTK"] = false;
752   res["GSMIS"] = false;
753   res["WxWidgets"] = false;
754   res["KWWidgets"] = false;
755
756   if (this->HasCMakeLists())
757     {
758       std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
759
760       boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+\\s+ON");
761       std::string::const_iterator start, end;
762       start = CMfile.begin();
763       end = CMfile.end();
764       boost::match_results<std::string::const_iterator> what;
765       boost::match_flag_type flags = boost::match_default;
766       while(boost::regex_search(start, end, what, expression, flags))
767         {
768           //std::cout << what[0].str() << std::endl;
769           boost::regex expression1 = boost::regex("\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+");
770           std::string::const_iterator start1, end1;
771           start1 = what[0].first;
772           end1 = what[0].second;
773           boost::match_results<std::string::const_iterator> what1;
774           if(boost::regex_search(start1, end1, what1, expression1, flags))
775             {
776               std::string dete = what1.str();
777               CDMUtilities::normalizeStr(dete);
778               //std::cout << dete << std::endl;
779               if(correspondence.find(dete) != correspondence.end())
780                 res[correspondence[dete]] = true;
781             }
782           start = what[0].second;
783         }
784     }
785   return res;
786 }
787
788 bool modelCDMPackage::Set3rdPartyLibrary(const std::string& library_name,
789     const bool& toInclude)
790 {
791   std::map<std::string, std::string> correspondence;
792
793   correspondence["VTK"] = "_USE_VTK";
794   correspondence["ITK"] = "_USE_ITK";
795   correspondence["GDCM"] = "_USE_GDCM";
796   correspondence["GDCM_VTK"] = "_USE_GDCM_VTK";
797   correspondence["GSMIS"] = "_USE_GSMIS";
798   correspondence["WxWidgets"] = "_USE_WXWIDGETS";
799   correspondence["KWWidgets"] = "_USE_KWWIDGETS";
800
801   if (correspondence.find(library_name) != correspondence.end())
802     {
803       std::string library_command = correspondence[library_name];
804       if (this->HasCMakeLists())
805         {
806           std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
807           std::string resCMfile = "";
808           bool found = false;
809
810           try {
811             boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
812
813             std::string::const_iterator start, end;
814             start = CMfile.begin();
815             end = CMfile.end();
816             boost::match_results<std::string::const_iterator> what;
817             boost::match_flag_type flags = boost::match_default;
818             if(boost::regex_search(start, end, what, expression, flags))
819               {
820                 found = true;
821                 resCMfile += what.prefix().str();
822                 if (toInclude)
823                   resCMfile += what.str();
824                 else
825                   resCMfile += "#" + what.str();
826                 resCMfile += what.suffix().str();
827
828                 return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
829               }
830             else
831               {
832                 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+OFF([\\s]|#[^\\n]*\\n)*\\)");
833
834                 start = CMfile.begin();
835                 end = CMfile.end();
836                 if(boost::regex_search(start, end, what, expression, flags))
837                   {
838                     found = true;
839                     resCMfile += what.prefix().str();
840                     if (toInclude)
841                       {
842                         std::string dete = what.str();
843                         int pos = dete.rfind("OFF");
844                         dete.replace(pos, 3, "ON");
845                         resCMfile += dete;
846                       }
847                     else
848                       resCMfile += what.str();
849                     resCMfile += what.suffix().str();
850
851                     return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
852                   }
853                 else
854                   {
855                     boost::regex expression("^\\h*#\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
856                     if(boost::regex_search(start, end, what, expression, flags))
857                       {
858                         found = true;
859                         resCMfile += what.prefix().str();
860                         if(toInclude)
861                           {
862                             std::string dete = what[0].str();
863                             for (int i = 0; i < dete.size(); ++i) {
864                               if (dete[i] != '#')
865                                 resCMfile.push_back(dete[i]);
866                               if (dete[i] == 'S')
867                                 {
868                                   resCMfile += dete.substr(i+1);
869                                   break;
870                                 }
871                             }
872                           }
873                         else
874                           resCMfile += what.str();
875
876                         resCMfile += what.suffix().str();
877                         return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
878                       }
879                     else
880                       {
881                         boost::regex expression("^\\h*#\\h*UNCOMMENT EACH LIBRARY NEEDED \\(WILL BE FOUND AND USED AUTOMATICALLY\\)[^\\n]*\\n");
882                         if(boost::regex_search(start, end, what, expression, flags))
883                           {
884                             found = true;
885                             resCMfile += what.prefix().str();
886                             resCMfile += what.str();
887                             if(toInclude)
888                               {
889                                 resCMfile += "SET(${BBTK_PACKAGE_NAME}"+ library_command +"  ON)\n";
890                               }
891                             resCMfile += what.suffix().str();
892                             return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
893                           }
894                       }
895                   }
896               }
897           } catch (boost::bad_expression& e) {
898             std::cout << "bad regex: " << e.what() << std::endl;
899             std::cout.flush();
900           }
901         }
902     }
903   return false;
904 }
905
906 std::map<std::string, bool> modelCDMPackage::GetCustomLibraries()
907 {
908   std::map<std::string, bool> res;
909   std::map<std::string, bool> res1;
910
911   std::map<std::string, std::string> correspondence;
912   std::vector<modelCDMLibrary*> libraries;
913   modelCDMIProjectTreeNode* p = this;
914   while(p != NULL && dynamic_cast<modelCDMProject*>(p) == NULL)
915     p = p->GetParent();
916
917   if(p != NULL && dynamic_cast<modelCDMProject*>(p)->GetLib() != NULL)
918     libraries = dynamic_cast<modelCDMProject*>(p)->GetLib()->GetLibraries();
919
920   for (int i = 0; i < libraries.size(); ++i)
921     {
922       correspondence[libraries[i]->GetName()] = libraries[i]->GetNameLibrary();
923       res[libraries[i]->GetNameLibrary()] = false;
924       res1[libraries[i]->GetNameLibrary()] = false;
925     }
926
927   if (this->HasCMakeLists())
928     {
929       std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
930
931       //find included libraries
932       boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS(([\\s]|#[^\\n]*\\n)+([\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
933       std::string::const_iterator start, end;
934       start = CMfile.begin();
935       end = CMfile.end();
936       boost::match_results<std::string::const_iterator> what;
937       boost::match_flag_type flags = boost::match_default;
938       if(boost::regex_search(start, end, what, expression, flags))
939         {
940
941           expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS");
942           std::string::const_iterator start1, end1;
943           start1 = what[0].first;
944           end1 = what[0].second;
945           boost::match_results<std::string::const_iterator> what1;
946           if(boost::regex_search(start1, end1, what1, expression, flags))
947             {
948               expression = boost::regex("^\\h*[\\w\\d]+");
949               std::string::const_iterator start2, end2;
950               start2 = what1[0].second;
951               end2 = what[0].second;
952               boost::match_results<std::string::const_iterator> what2;
953               while(boost::regex_search(start2, end2, what2, expression, flags))
954                 {
955                   std::string dete = what2.str();
956                   CDMUtilities::normalizeStr(dete);
957                   //std::cout << "detectado lib: " << dete << std::endl;
958                   if(res1.find(dete) != res1.end())
959                     res1[dete] = true;
960
961                   start2 = what2[0].second;
962                 }
963             }
964         }
965
966       //find included folders
967       //std::cout << "searching..." << std::endl;
968       expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_INCLUDE_DIRS(([\\s]|#[^\\n]*\\n|////[^\\n]*\\n)+([\\.\\/\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
969       start = CMfile.begin();
970       end = CMfile.end();
971       if(boost::regex_search(start, end, what, expression, flags))
972         {
973           //std::cout << what.str() << std::endl;
974           expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_INCLUDE_DIRS");
975           std::string::const_iterator start1, end1;
976           start1 = what[0].first;
977           end1 = what[0].second;
978           boost::match_results<std::string::const_iterator> what1;
979           if(boost::regex_search(start1, end1, what1, expression, flags))
980             {
981               //std::cout << what1.str() << std::endl;
982               expression = boost::regex("^\\h*\\.\\.\\/lib\\/([\\w\\d])+");
983               std::string::const_iterator start2, end2;
984               start2 = what1[0].second;
985               end2 = what[0].second;
986               boost::match_results<std::string::const_iterator> what2;
987               while(boost::regex_search(start2, end2, what2, expression, flags))
988                 {
989                   std::string dete = what2.str();
990                   CDMUtilities::normalizeStr(dete);
991                   //std::cout << "detectado dir: " << dete.substr(7) << std::endl;
992                   if(correspondence.find(dete.substr(7)) != correspondence.end())
993                     res[correspondence[dete.substr(7)]] = res1[correspondence[dete.substr(7)]];
994
995                   start2 = what2[0].second;
996                 }
997             }
998         }
999     }
1000
1001   return res;
1002 }
1003
1004 bool modelCDMPackage::SetCustomLibrary(const std::string& library_name,
1005     const bool& toInclude)
1006 {
1007   std::map<std::string, std::string> correspondence;
1008
1009     std::vector<modelCDMLibrary*> libraries;
1010     modelCDMIProjectTreeNode* p = this;
1011     while(p != NULL && dynamic_cast<modelCDMProject*>(p) == NULL)
1012       p = p->GetParent();
1013
1014     if(p != NULL && dynamic_cast<modelCDMProject*>(p)->GetLib() != NULL)
1015       libraries = dynamic_cast<modelCDMProject*>(p)->GetLib()->GetLibraries();
1016
1017     for (int i = 0; i < libraries.size(); ++i)
1018       {
1019         correspondence[libraries[i]->GetNameLibrary()] = libraries[i]->GetName();
1020       }
1021
1022     if (correspondence.find(library_name) != correspondence.end())
1023       {
1024         if (this->HasCMakeLists())
1025           {
1026             std::string resCMfile = "";
1027             std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1028             bool found = false;
1029
1030             //find included libraries
1031             //std::cout << "searching..." << CMfile << std::endl;
1032             boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS(([\\s]|#[^\\n]*\\n)+([\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
1033             std::string::const_iterator start, end;
1034             start = CMfile.begin();
1035             end = CMfile.end();
1036             boost::match_results<std::string::const_iterator> what;
1037             boost::match_flag_type flags = boost::match_default;
1038             if(boost::regex_search(start, end, what, expression, flags))
1039               {
1040                 //std::cout << what.str() << std::endl;
1041                 resCMfile += what.prefix().str();
1042                 expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS");
1043                 std::string::const_iterator start1, end1;
1044                 start1 = what[0].first;
1045                 end1 = what[0].second;
1046                 boost::match_results<std::string::const_iterator> what1;
1047                 if(boost::regex_search(start1, end1, what1, expression, flags))
1048                   {
1049                     resCMfile += what1.prefix().str() + what1.str();
1050                     //check if already exists
1051                     expression = boost::regex("^\\h*"+library_name);
1052                     std::string::const_iterator start2, end2;
1053                     start2 = what1[0].second;
1054                     end2 = what[0].second;
1055                     boost::match_results<std::string::const_iterator> what2, temp2;
1056                     while(boost::regex_search(start2, end2, what2, expression, flags))
1057                       {
1058                         resCMfile += what2.prefix().str();
1059                         found = true;
1060                         if (!toInclude)
1061                           {
1062                             resCMfile += "#";
1063                           }
1064                         resCMfile += what2.str();
1065                         temp2 = what2;
1066                         start2 = what2[0].second;
1067                       }
1068                     if(found)
1069                       resCMfile += temp2.suffix().str();
1070                     //check if is commented
1071                     else
1072                       {
1073                         expression = boost::regex("^\\h*#+\\h*"+library_name);
1074                         start2 = what1[0].second;
1075                         end2 = what[0].second;
1076                         while(boost::regex_search(start2, end2, what2, expression, flags))
1077                           {
1078                             found = true;
1079                             resCMfile += what2.prefix().str();
1080                             if(toInclude)
1081                               {
1082                                 std::string dete = what2[0].str();
1083                                 for (int i = 0; i < dete.size(); ++i) {
1084                                   if (dete[i] != '#')
1085                                     resCMfile.push_back(dete[i]);
1086                                 }
1087                               }
1088                             temp2 = what2;
1089                             start2 = what2[0].second;
1090                           }
1091                         if(found)
1092                           resCMfile += temp2.suffix().str();
1093                         //add at the beggining of instruction
1094                         else
1095                           {
1096                             if(toInclude)
1097                               resCMfile += "\n" + library_name;
1098                             resCMfile += what1.suffix().str();
1099                           }
1100                       }
1101                   }
1102                 resCMfile += what.suffix().str();
1103               }
1104             else
1105               return false;
1106
1107             //find included folders
1108             CMfile = resCMfile;
1109             resCMfile = "";
1110
1111
1112             found = false;
1113             //std::cout << "searching..." << CMfile << std::endl;
1114             expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_INCLUDE_DIRS(([\\s]|#[^\\n]*\\n)+([\\.\\/\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
1115             start = CMfile.begin();
1116             end = CMfile.end();
1117             if(boost::regex_search(start, end, what, expression, flags))
1118               {
1119                 resCMfile += what.prefix().str();
1120                 //std::cout << what.str() << std::endl;
1121                 expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_INCLUDE_DIRS");
1122                 std::string::const_iterator start1, end1;
1123                 start1 = what[0].first;
1124                 end1 = what[0].second;
1125                 boost::match_results<std::string::const_iterator> what1;
1126                 if(boost::regex_search(start1, end1, what1, expression, flags))
1127                   {
1128                     resCMfile += what1.prefix().str() + what1.str();
1129                     //std::cout << what1.str() << std::endl;
1130                     //search if dir is already included
1131                     expression = boost::regex("^\\h*\\.\\.\\/lib\\/"+correspondence[library_name]);
1132                     std::string::const_iterator start2, end2;
1133                     start2 = what1[0].second;
1134                     end2 = what[0].second;
1135                     boost::match_results<std::string::const_iterator> what2, temp2;
1136                     while(boost::regex_search(start2, end2, what2, expression, flags))
1137                       {
1138                         found = true;
1139                         resCMfile += what2.prefix().str();
1140                         if(!toInclude)
1141                           resCMfile += "#";
1142                         resCMfile += what2.str();
1143                         temp2 = what2;
1144                         start2 = what2[0].second;
1145                       }
1146                     if(found)
1147                       resCMfile += temp2.suffix().str();
1148                     //search if dir is commented
1149                     else
1150                       {
1151                         expression = boost::regex("^\\h*#+\\h*\\.\\.\\/lib\\/"+correspondence[library_name]);
1152                         start2 = what1[0].second;
1153                         end2 = what[0].second;
1154                         while(boost::regex_search(start2, end2, what2, expression, flags))
1155                           {
1156                             found = true;
1157                             resCMfile += what2.prefix().str();
1158                             if(toInclude)
1159                               {
1160                                 std::string dete = what2[0].str();
1161                                 for (int i = 0; i < dete.size(); ++i) {
1162                                   if (dete[i] != '#')
1163                                     resCMfile.push_back(dete[i]);
1164                                 }
1165                               }
1166                             temp2 = what2;
1167                             start2 = what2[0].second;
1168                           }
1169                         if(found)
1170                           resCMfile += temp2.suffix().str();
1171                         //add at the beggining of instruction
1172                         else
1173                           {
1174                             if(toInclude)
1175                               resCMfile += "\n../lib/" + correspondence[library_name];
1176                             resCMfile += what1.suffix().str();
1177                           }
1178                       }
1179                   }
1180                 resCMfile += what.suffix().str();
1181               }
1182             else
1183               return false;
1184
1185             return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
1186           }
1187       }
1188
1189     return false;
1190 }