]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMPackage.cpp
7e8b9ba6265533b5f88fcf5c5cc4420640e968b8
[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
167           //if CMakeLists, create CMakeLists
168           if(stdfileName == "CMakeLists.txt")
169             {
170               this->CMakeLists = new modelCDMCMakeListsFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
171               this->children.push_back(this->CMakeLists);
172             }
173           else
174             {
175               this->children.push_back(new modelCDMFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
176             }
177           //if is an unknown file, create file
178           cont = dir.GetNext(&fileName);
179         }
180     }
181   this->SortChildren();
182 }
183
184 modelCDMPackage::~modelCDMPackage()
185 {
186 }
187
188 const std::string& modelCDMPackage::GetNamePackage() const
189 {
190   return this->namePackage;
191 }
192
193 const std::string& modelCDMPackage::GetAuthors() const
194 {
195   return this->authors;
196 }
197
198 const std::string& modelCDMPackage::GetAuthorsEmail() const
199 {
200   return this->authorsEmail;
201 }
202
203 const std::string& modelCDMPackage::GetVersion() const
204 {
205   return this->version;
206 }
207
208 const std::string& modelCDMPackage::GetDescription() const
209 {
210   return this->description;
211 }
212
213 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
214 {
215   return this->src;
216 }
217
218 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
219 {
220   std::vector<std::string> words;
221   CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
222   std::string authorsReal = words[0];
223   for (int i = 1; i < (int)(words.size()); i++)
224     {
225       authorsReal += "/" + words[i];
226     }
227
228   std::string line;
229   //opening original cmakelists
230   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
231   if( !in.is_open())
232     {
233       result = new std::string("CMakeLists.txt file failed to open.");
234       return false;
235     }
236   //opening temporal cmakelists
237   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
238   if( !out.is_open())
239     {
240       result = new std::string("CMakeLists.txt.tmp file failed to open.");
241       return false;
242     }
243   //copying contents from original to temporal and making changes
244   while (getline(in, line))
245     {
246       if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
247         line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
248       out << line << std::endl;
249     }
250   in.close();
251   out.close();
252   //delete old file and rename new file
253 #ifdef _WIN32
254   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
255 #else
256   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
257 #endif
258   
259   if(system(renameCommand.c_str()))
260     {
261       result = new std::string("An error occurred while running '" + renameCommand + "'.");
262       return false;
263     }
264
265   this->authors = authorsReal;
266   return true;
267 }
268
269 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
270 {
271   //TODO: implement method
272   return true;
273 }
274
275 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
276 {
277   std::vector<std::string> vers;
278   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
279
280
281   std::string line;
282   //opening original cmakelists
283   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
284   if( !in.is_open())
285     {
286       result = new std::string("CMakeLists.txt file failed to open.");
287       return false;
288     }
289   //opening temporal cmakelists
290   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
291   if( !out.is_open())
292     {
293       result = new std::string("CMakeLists.txt.tmp file failed to open.");
294       return false;
295     }
296   //copying contents from original to temporal and making changes
297   while (getline(in, line))
298     {
299       if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
300         line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
301       else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
302         line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
303       else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
304         line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
305       out << line << std::endl;
306     }
307   in.close();
308   out.close();
309   //delete old file and rename new file
310 #ifdef _WIN32
311   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
312 #else
313   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
314 #endif
315   
316   if(system(renameCommand.c_str()))
317     {
318       result = new std::string("An error occurred while running '" + renameCommand + "'.");
319       return false;
320     }
321
322   this->version = vers[0] + "." + vers[1] + "." + vers[2];
323   return true;
324 }
325
326 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
327 {
328   std::vector<std::string> words;
329   CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
330   std::string descriptionReal = words[0];
331   for (int i = 1; i < (int)(words.size()); i++)
332     {
333       descriptionReal += " " + words[i];
334     }
335
336   std::string line;
337   //opening original cmakelists
338   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
339   if( !in.is_open())
340     {
341       result = new std::string("CMakeLists.txt file failed to open.");
342       return false;
343     }
344   //opening temporal cmakelists
345   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
346   if( !out.is_open())
347     {
348       result = new std::string("CMakeLists.txt.tmp file failed to open.");
349       return false;
350     }
351   //copying contents from original to temporal and making changes
352   while (getline(in, line))
353     {
354       if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
355         line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
356       out << line << std::endl;
357     }
358   in.close();
359   out.close();
360   //delete old file and rename new file
361 #ifdef _WIN32
362   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
363 #else
364   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
365 #endif
366   
367   if(system(renameCommand.c_str()))
368     {
369       result = new std::string("An error occurred while running '" + renameCommand + "'.");
370       return false;
371     }
372
373   this->description = descriptionReal;
374   return true;
375 }
376
377 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
378     std::string*& result,
379     const std::string& name,
380     const std::string& type,
381     const std::string& format,
382     const std::string& categories,
383     const std::string& authors,
384     const std::string& authorsEmail,
385     const std::string& description
386 )
387 {
388   return this->src->CreateBlackBox(result,name, this->namePackage, type,format,categories,authors,authorsEmail,description);
389 }
390
391 const bool modelCDMPackage::Refresh(std::string*& result)
392 {
393   std::cout << "refreshing package " << this->namePackage << std::endl;
394   this->type = wxDIR_DIRS;
395
396   //Get Package Name
397
398   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
399
400   std::ifstream confFile;
401   confFile.open((pathMakeLists).c_str());
402
403   std::string word;
404   while(confFile.is_open() && !confFile.eof())
405     {
406       //get sets
407       std::getline(confFile,word,'(');
408       std::vector<std::string> wordBits;
409       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
410
411       if(wordBits[wordBits.size()-1] == "SET")
412         {
413           //get package name
414           std::getline(confFile,word,')');
415           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
416           if(wordBits[0] == "BBTK_PACKAGE_NAME")
417             {
418               word = wordBits[1];
419               for (int i = 2; i < (int)(wordBits.size()); i++)
420                 {
421                   word += " " + wordBits[i];
422                 }
423               wordBits.clear();
424               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
425
426               this->namePackage = wordBits[0];
427             }
428           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
429             {
430               word = wordBits[1];
431               for (int i = 2; i < (int)(wordBits.size()); i++)
432                 {
433                   word += " " + wordBits[i];
434                 }
435               wordBits.clear();
436               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
437
438               this->authors = wordBits[0];
439             }
440           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
441             {
442               word = wordBits[1];
443               for (int i = 2; i < (int)(wordBits.size()); i++)
444                 {
445                   word += " " + wordBits[i];
446                 }
447               wordBits.clear();
448               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
449
450               this->description = wordBits[0];
451             }
452           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
453             {
454               this->version = wordBits[1];
455             }
456           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
457             {
458               this->version += "." + wordBits[1];
459             }
460           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
461             {
462               this->version += "." + wordBits[1];
463             }
464         }
465     }
466
467
468
469   std::vector<bool> checked(this->children.size(), false);
470   bool checkedSrc = false;
471
472   //check all folders
473   wxDir dir(crea::std2wx((this->path).c_str()));
474   if (dir.IsOpened())
475     {
476       wxString fileName;
477       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
478       while (cont)
479         {
480
481           std::string stdfileName = crea::wx2std(fileName);
482
483           //detect black boxes in src
484           if(stdfileName == "src")
485             {
486               //check if box already exist
487               bool found = false;
488               if (this->src != NULL)
489                 {
490                   found = true;
491                   int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
492                   checked[pos] = true;
493                   checkedSrc = true;
494                   if(!this->src->Refresh(result))
495                     return false;
496                 }
497               else
498                 {
499                   this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
500                   this->children.push_back(this->src);
501                 }
502             }
503           else
504             {
505
506               //check if folder already exist
507               bool found = false;
508               for (int i = 0; !found && i < (int)(this->children.size()); i++)
509                 {
510                   if (this->children[i]->GetName() == stdfileName)
511                     {
512                       found = true;
513                       checked[i] = true;
514                       if(!this->children[i]->Refresh(result))
515                         return false;
516                     }
517                 }
518               if(!found)
519                 {
520                   modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
521                   this->children.push_back(folder);
522                 }
523             }
524           cont = dir.GetNext(&fileName);
525
526         }
527
528       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
529       while (cont)
530         {
531           std::string stdfileName = crea::wx2std(fileName);
532
533           //if CMakeLists, create CMakeLists
534           if(stdfileName == "CMakeLists.txt")
535             {
536               if (this->CMakeLists == NULL)
537                 {
538                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
539                   this->children.push_back(this->CMakeLists);
540                 }
541               else
542                 {
543                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
544                   checked[pos] = true;
545                   if(!this->CMakeLists->Refresh(result))
546                     return false;
547                 }
548             }
549           //if is an unknown file, create file
550           else
551             {
552               bool found = false;
553               for (int i = 0; !found && i < (int)(this->children.size()); i++)
554                 {
555                   if (this->children[i]->GetName() == stdfileName)
556                     {
557                       found = true;
558                       checked[i] = true;
559                       if(!this->children[i]->Refresh(result))
560                         return false;
561                     }
562                 }
563
564               if(!found)
565                 {
566                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
567                   this->children.push_back(file);
568                 }
569             }
570
571           cont = dir.GetNext(&fileName);
572         }
573     }
574
575   if(!checkedSrc)
576     {
577       this->src = NULL;
578     }
579
580   for (int i = 0; i < (int)(checked.size()); i++)
581     {
582       if(!checked[i])
583         {
584           delete this->children[i];
585           this->children.erase(this->children.begin()+i);
586           checked.erase(checked.begin()+i);
587           i--;
588         }
589     }
590   this->SortChildren();
591   return true;
592 }
593
594 void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
595 {
596   //check cmake exist
597   if(this->CMakeLists != NULL)
598     {
599       //open cmakelists
600       std::ifstream confFile;
601       confFile.open((this->CMakeLists->GetPath()).c_str());
602
603       //take everything that is not commented
604       std::string fileContent;
605
606       std::string word;
607       std::vector<std::string> words;
608       while(confFile.is_open() && !confFile.eof())
609         {
610           std::getline(confFile,word, '\n');
611           if(word[0] != '#')
612             {
613               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
614               if (words.size() > 0)
615                 {
616                   word = words[0];
617                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
618                   for (int i = 0; i < (int)(words.size()); i++)
619                     {
620                       if(words[i].substr(0,2) == "//")
621                         break;
622                       fileContent += words[i] + " ";
623                     }
624                 }
625             }
626         }
627
628       //check every instruction
629       std::stringstream ss(fileContent);
630       while(!ss.eof())
631         {
632           std::getline(ss,word, '(');
633
634           //check instruction name
635           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
636
637           //set instructions
638           if (words.size() > 0 && words[words.size()-1] == "SET")
639             {
640               std::getline(ss,word, ')');
641
642               CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
643               if (words.size() > 1)
644                 {
645                   if (words[0] == "${BBTK_PACKAGE_NAME}_USE_VTK" && words[1] == "ON")
646                     {
647                       properties["package " + this->name + " set USE_VTK"] = true;
648                     }
649                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_ITK" && words[1] == "ON")
650                     {
651                       properties["package " + this->name + " set USE_ITK"] = true;
652                     }
653                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM" && words[1] == "ON")
654                     {
655                       properties["package " + this->name + " set USE_GDCM"] = true;
656                     }
657                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM_VTK" && words[1] == "ON")
658                     {
659                       properties["package " + this->name + " set USE_GDCM_VTK"] = true;
660                     }
661                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GSMIS" && words[1] == "ON")
662                     {
663                       properties["package " + this->name + " set USE_GSMIS"] = true;
664                     }
665                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_WXWIDGETS" && words[1] == "ON")
666                     {
667                       properties["package " + this->name + " set USE_WXWIDGETS"] = true;
668                     }
669                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_KWWIDGETS" && words[1] == "ON")
670                     {
671                       properties["package " + this->name + " set USE_KWWIDGETS"] = true;
672                     }
673                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
674                     {
675                       properties["package " + this->name + " set USE_BOOST"] = true;
676                     }
677                   else if (words[0] == "${BBTK_PACKAGE_NAME}_INCLUDE_DIRS")
678                     {
679                       for (int i = 1; i < (int)(words.size()); i++)
680                         {
681                           if(words[i].substr(0,2) == "${" || words[i].substr(0,2) == "..")
682                           properties["package " + this->name + " dir " + words[i]] = true;
683                         }
684                     }
685                   else if (words[0] == "${BBTK_PACKAGE_NAME}_LIBS")
686                     {
687                       for (int i = 1; i < (int)(words.size()); i++)
688                         {
689                           properties["package " + this->name + " lib " + words[i]] = true;
690                         }
691                     }
692                 }
693             }
694         }
695
696     }
697 }
698
699 std::map<std::string, bool> modelCDMPackage::Get3rdPartyLibraries()
700 {
701   std::map<std::string, std::string> correspondence;
702   correspondence["${BBTK_PACKAGE_NAME}_USE_VTK"] = "VTK";
703   correspondence["${BBTK_PACKAGE_NAME}_USE_ITK"] = "ITK";
704   correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM"] = "GDCM";
705   correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM_VTK"] = "GDCM_VTK";
706   correspondence["${BBTK_PACKAGE_NAME}_USE_GSMIS"] = "GSMIS";
707   correspondence["${BBTK_PACKAGE_NAME}_USE_WXWIDGETS"] = "WxWidgets";
708   correspondence["${BBTK_PACKAGE_NAME}_USE_KWWIDGETS"] = "KWWidgets";
709   std::map<std::string, bool> res;
710   res["VTK"] = false;
711   res["ITK"] = false;
712   res["GDCM"] = false;
713   res["GDCM_VTK"] = false;
714   res["GSMIS"] = false;
715   res["WxWidgets"] = false;
716   res["KWWidgets"] = false;
717
718   if (this->HasCMakeLists())
719     {
720       std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
721
722       boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+\\s+ON");
723       std::string::const_iterator start, end;
724       start = CMfile.begin();
725       end = CMfile.end();
726       boost::match_results<std::string::const_iterator> what;
727       boost::match_flag_type flags = boost::match_default;
728       while(boost::regex_search(start, end, what, expression, flags))
729         {
730           //std::cout << what[0].str() << std::endl;
731           boost::regex expression1 = boost::regex("\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+");
732           std::string::const_iterator start1, end1;
733           start1 = what[0].first;
734           end1 = what[0].second;
735           boost::match_results<std::string::const_iterator> what1;
736           if(boost::regex_search(start1, end1, what1, expression1, flags))
737             {
738               std::string dete = what1.str();
739               CDMUtilities::normalizeStr(dete);
740               //std::cout << dete << std::endl;
741               if(correspondence.find(dete) != correspondence.end())
742                 res[correspondence[dete]] = true;
743             }
744           start = what[0].second;
745         }
746     }
747   return res;
748 }
749
750 bool modelCDMPackage::Set3rdPartyLibrary(const std::string& library_name,
751     const bool& toInclude)
752 {
753   std::map<std::string, std::string> correspondence;
754
755   correspondence["VTK"] = "_USE_VTK";
756   correspondence["ITK"] = "_USE_ITK";
757   correspondence["GDCM"] = "_USE_GDCM";
758   correspondence["GDCM_VTK"] = "_USE_GDCM_VTK";
759   correspondence["GSMIS"] = "_USE_GSMIS";
760   correspondence["WxWidgets"] = "_USE_WXWIDGETS";
761   correspondence["KWWidgets"] = "_USE_KWWIDGETS";
762
763   if (correspondence.find(library_name) != correspondence.end())
764     {
765       std::string library_command = correspondence[library_name];
766 //      std::cout << "found correspondence " << library_command << std::endl;
767 //      std::cout.flush();
768       if (this->HasCMakeLists())
769         {
770           std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
771           std::string resCMfile = "";
772           bool found = false;
773
774 //          std::cout << "found cmakefile: " << CMfile << std::endl;
775 //          std::cout.flush();
776
777           try {
778 //            std::cout << "first regex" << std::endl;
779 //            std::cout.flush();
780             boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
781
782             std::string::const_iterator start, end;
783             start = CMfile.begin();
784             end = CMfile.end();
785             boost::match_results<std::string::const_iterator> what;
786             boost::match_flag_type flags = boost::match_default;
787             if(boost::regex_search(start, end, what, expression, flags))
788               {
789 //                std::cout << "found " << what.str() << std::endl;
790 //                std::cout.flush();
791                 found = true;
792                 resCMfile += what.prefix().str();
793                 if (toInclude)
794                   resCMfile += what.str();
795                 else
796                   resCMfile += "#" + what.str();
797                 resCMfile += what.suffix().str();
798
799                 return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
800               }
801             else
802               {
803 //                std::cout << "second regex" << std::endl;
804 //                std::cout.flush();
805                 boost::regex expression("^\\h*#\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
806                 if(boost::regex_search(start, end, what, expression, flags))
807                   {
808                     found = true;
809                     resCMfile += what.prefix().str();
810                     if(toInclude)
811                       {
812                         std::string dete = what[0].str();
813                         for (int i = 0; i < dete.size(); ++i) {
814                           if (dete[i] != '#')
815                             resCMfile.push_back(dete[i]);
816                           if (dete[i] == 'S')
817                             {
818                               resCMfile += dete.substr(i+1);
819                               break;
820                             }
821                         }
822                       }
823                     else
824                       resCMfile += what.str();
825
826                     resCMfile += what.suffix().str();
827                     return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
828                   }
829                 else
830                   {
831 //                    std::cout << "third regex" << std::endl;
832 //                    std::cout.flush();
833                     boost::regex expression("^\\h*#\\h*UNCOMMENT EACH LIBRARY NEEDED \\(WILL BE FOUND AND USED AUTOMATICALLY\\)[^\\n]*\\n");
834                     if(boost::regex_search(start, end, what, expression, flags))
835                       {
836                         found = true;
837                         resCMfile += what.prefix().str();
838                         resCMfile += what.str();
839                         if(toInclude)
840                           {
841                             resCMfile += "SET(${BBTK_PACKAGE_NAME}"+ library_command +"  ON)\n";
842                           }
843                         resCMfile += what.suffix().str();
844                         return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
845                       }
846                   }
847               }
848           } catch (boost::bad_expression& e) {
849             std::cout << "bad regex: " << e.what() << std::endl;
850             std::cout.flush();
851           }
852         }
853     }
854   return false;
855 }
856
857 std::map<std::string, bool> modelCDMPackage::GetCustomLibraries()
858 {
859   std::map<std::string, bool> res;
860   std::map<std::string, bool> res1;
861
862   std::map<std::string, std::string> correspondence;
863   std::vector<modelCDMLibrary*> libraries;
864   modelCDMIProjectTreeNode* p = this;
865   while(p != NULL && dynamic_cast<modelCDMProject*>(p) == NULL)
866     p = p->GetParent();
867
868   if(p != NULL && dynamic_cast<modelCDMProject*>(p)->GetLib() != NULL)
869     libraries = dynamic_cast<modelCDMProject*>(p)->GetLib()->GetLibraries();
870
871   for (int i = 0; i < libraries.size(); ++i)
872     {
873       correspondence[libraries[i]->GetName()] = libraries[i]->GetNameLibrary();
874       res[libraries[i]->GetNameLibrary()] = false;
875       res1[libraries[i]->GetNameLibrary()] = false;
876     }
877
878   if (this->HasCMakeLists())
879     {
880       std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
881
882       //find included libraries
883       boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS(([\\s]|#[^\\n]*\\n)+([\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
884       std::string::const_iterator start, end;
885       start = CMfile.begin();
886       end = CMfile.end();
887       boost::match_results<std::string::const_iterator> what;
888       boost::match_flag_type flags = boost::match_default;
889       if(boost::regex_search(start, end, what, expression, flags))
890         {
891
892           expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS");
893           std::string::const_iterator start1, end1;
894           start1 = what[0].first;
895           end1 = what[0].second;
896           boost::match_results<std::string::const_iterator> what1;
897           if(boost::regex_search(start1, end1, what1, expression, flags))
898             {
899               expression = boost::regex("^\\h*[\\w\\d]+");
900               std::string::const_iterator start2, end2;
901               start2 = what1[0].second;
902               end2 = what[0].second;
903               boost::match_results<std::string::const_iterator> what2;
904               while(boost::regex_search(start2, end2, what2, expression, flags))
905                 {
906                   std::string dete = what2.str();
907                   CDMUtilities::normalizeStr(dete);
908                   //std::cout << "detectado lib: " << dete << std::endl;
909                   if(res1.find(dete) != res1.end())
910                     res1[dete] = true;
911
912                   start2 = what2[0].second;
913                 }
914             }
915         }
916
917       //find included folders
918       //std::cout << "searching..." << std::endl;
919       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)*\\)");
920       start = CMfile.begin();
921       end = CMfile.end();
922       if(boost::regex_search(start, end, what, expression, flags))
923         {
924           //std::cout << what.str() << std::endl;
925           expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_INCLUDE_DIRS");
926           std::string::const_iterator start1, end1;
927           start1 = what[0].first;
928           end1 = what[0].second;
929           boost::match_results<std::string::const_iterator> what1;
930           if(boost::regex_search(start1, end1, what1, expression, flags))
931             {
932               //std::cout << what1.str() << std::endl;
933               expression = boost::regex("^\\h*\\.\\.\\/lib\\/([\\w\\d])+");
934               std::string::const_iterator start2, end2;
935               start2 = what1[0].second;
936               end2 = what[0].second;
937               boost::match_results<std::string::const_iterator> what2;
938               while(boost::regex_search(start2, end2, what2, expression, flags))
939                 {
940                   std::string dete = what2.str();
941                   CDMUtilities::normalizeStr(dete);
942                   //std::cout << "detectado dir: " << dete.substr(7) << std::endl;
943                   if(correspondence.find(dete.substr(7)) != correspondence.end())
944                     res[correspondence[dete.substr(7)]] = res1[correspondence[dete.substr(7)]];
945
946                   start2 = what2[0].second;
947                 }
948             }
949         }
950     }
951
952   return res;
953 }
954
955 bool modelCDMPackage::SetCustomLibrary(const std::string& library_name,
956     const bool& toInclude)
957 {
958   return false;
959 }