]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMPackage.cpp
5816dc43684d60f655c4705c6f2726f7b2b797c0
[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       if (this->HasCMakeLists())
767         {
768           std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
769           std::string resCMfile = "";
770           bool found = false;
771
772           try {
773             boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
774
775             std::string::const_iterator start, end;
776             start = CMfile.begin();
777             end = CMfile.end();
778             boost::match_results<std::string::const_iterator> what;
779             boost::match_flag_type flags = boost::match_default;
780             if(boost::regex_search(start, end, what, expression, flags))
781               {
782                 found = true;
783                 resCMfile += what.prefix().str();
784                 if (toInclude)
785                   resCMfile += what.str();
786                 else
787                   resCMfile += "#" + what.str();
788                 resCMfile += what.suffix().str();
789
790                 return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
791               }
792             else
793               {
794                 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+OFF([\\s]|#[^\\n]*\\n)*\\)");
795
796                 start = CMfile.begin();
797                 end = CMfile.end();
798                 if(boost::regex_search(start, end, what, expression, flags))
799                   {
800                     found = true;
801                     resCMfile += what.prefix().str();
802                     if (toInclude)
803                       {
804                         std::string dete = what.str();
805                         int pos = dete.rfind("OFF");
806                         dete.replace(pos, 3, "ON");
807                         resCMfile += dete;
808                       }
809                     else
810                       resCMfile += what.str();
811                     resCMfile += what.suffix().str();
812
813                     return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
814                   }
815                 else
816                   {
817                     boost::regex expression("^\\h*#\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
818                     if(boost::regex_search(start, end, what, expression, flags))
819                       {
820                         found = true;
821                         resCMfile += what.prefix().str();
822                         if(toInclude)
823                           {
824                             std::string dete = what[0].str();
825                             for (int i = 0; i < dete.size(); ++i) {
826                               if (dete[i] != '#')
827                                 resCMfile.push_back(dete[i]);
828                               if (dete[i] == 'S')
829                                 {
830                                   resCMfile += dete.substr(i+1);
831                                   break;
832                                 }
833                             }
834                           }
835                         else
836                           resCMfile += what.str();
837
838                         resCMfile += what.suffix().str();
839                         return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
840                       }
841                     else
842                       {
843                         boost::regex expression("^\\h*#\\h*UNCOMMENT EACH LIBRARY NEEDED \\(WILL BE FOUND AND USED AUTOMATICALLY\\)[^\\n]*\\n");
844                         if(boost::regex_search(start, end, what, expression, flags))
845                           {
846                             found = true;
847                             resCMfile += what.prefix().str();
848                             resCMfile += what.str();
849                             if(toInclude)
850                               {
851                                 resCMfile += "SET(${BBTK_PACKAGE_NAME}"+ library_command +"  ON)\n";
852                               }
853                             resCMfile += what.suffix().str();
854                             return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
855                           }
856                       }
857                   }
858               }
859           } catch (boost::bad_expression& e) {
860             std::cout << "bad regex: " << e.what() << std::endl;
861             std::cout.flush();
862           }
863         }
864     }
865   return false;
866 }
867
868 std::map<std::string, bool> modelCDMPackage::GetCustomLibraries()
869 {
870   std::map<std::string, bool> res;
871   std::map<std::string, bool> res1;
872
873   std::map<std::string, std::string> correspondence;
874   std::vector<modelCDMLibrary*> libraries;
875   modelCDMIProjectTreeNode* p = this;
876   while(p != NULL && dynamic_cast<modelCDMProject*>(p) == NULL)
877     p = p->GetParent();
878
879   if(p != NULL && dynamic_cast<modelCDMProject*>(p)->GetLib() != NULL)
880     libraries = dynamic_cast<modelCDMProject*>(p)->GetLib()->GetLibraries();
881
882   for (int i = 0; i < libraries.size(); ++i)
883     {
884       correspondence[libraries[i]->GetName()] = libraries[i]->GetNameLibrary();
885       res[libraries[i]->GetNameLibrary()] = false;
886       res1[libraries[i]->GetNameLibrary()] = false;
887     }
888
889   if (this->HasCMakeLists())
890     {
891       std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
892
893       //find included libraries
894       boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS(([\\s]|#[^\\n]*\\n)+([\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
895       std::string::const_iterator start, end;
896       start = CMfile.begin();
897       end = CMfile.end();
898       boost::match_results<std::string::const_iterator> what;
899       boost::match_flag_type flags = boost::match_default;
900       if(boost::regex_search(start, end, what, expression, flags))
901         {
902
903           expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS");
904           std::string::const_iterator start1, end1;
905           start1 = what[0].first;
906           end1 = what[0].second;
907           boost::match_results<std::string::const_iterator> what1;
908           if(boost::regex_search(start1, end1, what1, expression, flags))
909             {
910               expression = boost::regex("^\\h*[\\w\\d]+");
911               std::string::const_iterator start2, end2;
912               start2 = what1[0].second;
913               end2 = what[0].second;
914               boost::match_results<std::string::const_iterator> what2;
915               while(boost::regex_search(start2, end2, what2, expression, flags))
916                 {
917                   std::string dete = what2.str();
918                   CDMUtilities::normalizeStr(dete);
919                   //std::cout << "detectado lib: " << dete << std::endl;
920                   if(res1.find(dete) != res1.end())
921                     res1[dete] = true;
922
923                   start2 = what2[0].second;
924                 }
925             }
926         }
927
928       //find included folders
929       //std::cout << "searching..." << std::endl;
930       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)*\\)");
931       start = CMfile.begin();
932       end = CMfile.end();
933       if(boost::regex_search(start, end, what, expression, flags))
934         {
935           //std::cout << what.str() << std::endl;
936           expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_INCLUDE_DIRS");
937           std::string::const_iterator start1, end1;
938           start1 = what[0].first;
939           end1 = what[0].second;
940           boost::match_results<std::string::const_iterator> what1;
941           if(boost::regex_search(start1, end1, what1, expression, flags))
942             {
943               //std::cout << what1.str() << std::endl;
944               expression = boost::regex("^\\h*\\.\\.\\/lib\\/([\\w\\d])+");
945               std::string::const_iterator start2, end2;
946               start2 = what1[0].second;
947               end2 = what[0].second;
948               boost::match_results<std::string::const_iterator> what2;
949               while(boost::regex_search(start2, end2, what2, expression, flags))
950                 {
951                   std::string dete = what2.str();
952                   CDMUtilities::normalizeStr(dete);
953                   //std::cout << "detectado dir: " << dete.substr(7) << std::endl;
954                   if(correspondence.find(dete.substr(7)) != correspondence.end())
955                     res[correspondence[dete.substr(7)]] = res1[correspondence[dete.substr(7)]];
956
957                   start2 = what2[0].second;
958                 }
959             }
960         }
961     }
962
963   return res;
964 }
965
966 bool modelCDMPackage::SetCustomLibrary(const std::string& library_name,
967     const bool& toInclude)
968 {
969   std::map<std::string, std::string> correspondence;
970
971     std::vector<modelCDMLibrary*> libraries;
972     modelCDMIProjectTreeNode* p = this;
973     while(p != NULL && dynamic_cast<modelCDMProject*>(p) == NULL)
974       p = p->GetParent();
975
976     if(p != NULL && dynamic_cast<modelCDMProject*>(p)->GetLib() != NULL)
977       libraries = dynamic_cast<modelCDMProject*>(p)->GetLib()->GetLibraries();
978
979     for (int i = 0; i < libraries.size(); ++i)
980       {
981         correspondence[libraries[i]->GetNameLibrary()] = libraries[i]->GetName();
982       }
983
984     if (correspondence.find(library_name) != correspondence.end())
985       {
986         if (this->HasCMakeLists())
987           {
988             std::string resCMfile = "";
989             std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
990             bool found = false;
991
992             //find included libraries
993             //std::cout << "searching..." << CMfile << std::endl;
994             boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS(([\\s]|#[^\\n]*\\n)+([\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
995             std::string::const_iterator start, end;
996             start = CMfile.begin();
997             end = CMfile.end();
998             boost::match_results<std::string::const_iterator> what;
999             boost::match_flag_type flags = boost::match_default;
1000             if(boost::regex_search(start, end, what, expression, flags))
1001               {
1002                 //std::cout << what.str() << std::endl;
1003                 resCMfile += what.prefix().str();
1004                 expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_LIBS");
1005                 std::string::const_iterator start1, end1;
1006                 start1 = what[0].first;
1007                 end1 = what[0].second;
1008                 boost::match_results<std::string::const_iterator> what1;
1009                 if(boost::regex_search(start1, end1, what1, expression, flags))
1010                   {
1011                     resCMfile += what1.prefix().str() + what1.str();
1012                     //check if already exists
1013                     expression = boost::regex("^\\h*"+library_name);
1014                     std::string::const_iterator start2, end2;
1015                     start2 = what1[0].second;
1016                     end2 = what[0].second;
1017                     boost::match_results<std::string::const_iterator> what2, temp2;
1018                     while(boost::regex_search(start2, end2, what2, expression, flags))
1019                       {
1020                         resCMfile += what2.prefix().str();
1021                         found = true;
1022                         if (!toInclude)
1023                           {
1024                             resCMfile += "#";
1025                           }
1026                         resCMfile += what2.str();
1027                         temp2 = what2;
1028                         start2 = what2[0].second;
1029                       }
1030                     if(found)
1031                       resCMfile += temp2.suffix().str();
1032                     //check if is commented
1033                     else
1034                       {
1035                         expression = boost::regex("^\\h*#+\\h*"+library_name);
1036                         start2 = what1[0].second;
1037                         end2 = what[0].second;
1038                         while(boost::regex_search(start2, end2, what2, expression, flags))
1039                           {
1040                             found = true;
1041                             resCMfile += what2.prefix().str();
1042                             if(toInclude)
1043                               {
1044                                 std::string dete = what2[0].str();
1045                                 for (int i = 0; i < dete.size(); ++i) {
1046                                   if (dete[i] != '#')
1047                                     resCMfile.push_back(dete[i]);
1048                                 }
1049                               }
1050                             temp2 = what2;
1051                             start2 = what2[0].second;
1052                           }
1053                         if(found)
1054                           resCMfile += temp2.suffix().str();
1055                         //add at the beggining of instruction
1056                         else
1057                           {
1058                             if(toInclude)
1059                               resCMfile += "\n" + library_name;
1060                             resCMfile += what1.suffix().str();
1061                           }
1062                       }
1063                   }
1064                 resCMfile += what.suffix().str();
1065               }
1066             else
1067               return false;
1068
1069             //find included folders
1070             CMfile = resCMfile;
1071             resCMfile = "";
1072
1073
1074             found = false;
1075             //std::cout << "searching..." << CMfile << std::endl;
1076             expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_INCLUDE_DIRS(([\\s]|#[^\\n]*\\n)+([\\.\\/\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
1077             start = CMfile.begin();
1078             end = CMfile.end();
1079             if(boost::regex_search(start, end, what, expression, flags))
1080               {
1081                 resCMfile += what.prefix().str();
1082                 //std::cout << what.str() << std::endl;
1083                 expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_INCLUDE_DIRS");
1084                 std::string::const_iterator start1, end1;
1085                 start1 = what[0].first;
1086                 end1 = what[0].second;
1087                 boost::match_results<std::string::const_iterator> what1;
1088                 if(boost::regex_search(start1, end1, what1, expression, flags))
1089                   {
1090                     resCMfile += what1.prefix().str() + what1.str();
1091                     //std::cout << what1.str() << std::endl;
1092                     //search if dir is already included
1093                     expression = boost::regex("^\\h*\\.\\.\\/lib\\/"+correspondence[library_name]);
1094                     std::string::const_iterator start2, end2;
1095                     start2 = what1[0].second;
1096                     end2 = what[0].second;
1097                     boost::match_results<std::string::const_iterator> what2, temp2;
1098                     while(boost::regex_search(start2, end2, what2, expression, flags))
1099                       {
1100                         found = true;
1101                         resCMfile += what2.prefix().str();
1102                         if(!toInclude)
1103                           resCMfile += "#";
1104                         resCMfile += what2.str();
1105                         temp2 = what2;
1106                         start2 = what2[0].second;
1107                       }
1108                     if(found)
1109                       resCMfile += temp2.suffix().str();
1110                     //search if dir is commented
1111                     else
1112                       {
1113                         expression = boost::regex("^\\h*#+\\h*\\.\\.\\/lib\\/"+correspondence[library_name]);
1114                         start2 = what1[0].second;
1115                         end2 = what[0].second;
1116                         while(boost::regex_search(start2, end2, what2, expression, flags))
1117                           {
1118                             found = true;
1119                             resCMfile += what2.prefix().str();
1120                             if(toInclude)
1121                               {
1122                                 std::string dete = what2[0].str();
1123                                 for (int i = 0; i < dete.size(); ++i) {
1124                                   if (dete[i] != '#')
1125                                     resCMfile.push_back(dete[i]);
1126                                 }
1127                               }
1128                             temp2 = what2;
1129                             start2 = what2[0].second;
1130                           }
1131                         if(found)
1132                           resCMfile += temp2.suffix().str();
1133                         //add at the beggining of instruction
1134                         else
1135                           {
1136                             if(toInclude)
1137                               resCMfile += "\n../lib/" + correspondence[library_name];
1138                             resCMfile += what1.suffix().str();
1139                           }
1140                       }
1141                   }
1142                 resCMfile += what.suffix().str();
1143               }
1144             else
1145               return false;
1146
1147             return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
1148           }
1149       }
1150
1151     return false;
1152 }