]> 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 <fstream>
38 #include <sstream>
39 #include <algorithm>
40 #include <boost/regex.hpp>
41
42 #include "creaWx.h"
43 #include "wx/dir.h"
44 #include "CDMUtilities.h"
45
46 modelCDMPackage::modelCDMPackage()
47 {
48   this->src = NULL;
49 }
50
51 modelCDMPackage::modelCDMPackage(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
52 {
53   std::cout << "creating package: " + path + "\n";
54   this->parent = parent;
55   this->type = wxDIR_DIRS;
56   this->name = name;
57   //Get Package Name
58
59   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
60
61   std::ifstream confFile;
62   confFile.open((pathMakeLists).c_str());
63
64   std::string word;
65   while(confFile.is_open() && !confFile.eof())
66     {
67       //get sets
68       std::getline(confFile,word,'(');
69       std::vector<std::string> wordBits;
70       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
71
72       if(wordBits[wordBits.size()-1] == "SET")
73         {
74           //get package name
75           std::getline(confFile,word,')');
76           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
77           if(wordBits[0] == "BBTK_PACKAGE_NAME")
78             {
79               word = wordBits[1];
80               for (int i = 2; i < (int)(wordBits.size()); i++)
81                 {
82                   word += " " + wordBits[i];
83                 }
84               wordBits.clear();
85               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
86
87               this->namePackage = wordBits[0];
88             }
89           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
90             {
91               word = wordBits[1];
92               for (int i = 2; i < (int)(wordBits.size()); i++)
93                 {
94                   word += " " + wordBits[i];
95                 }
96               wordBits.clear();
97               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
98
99               this->authors = wordBits[0];
100             }
101           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
102             {
103               word = wordBits[1];
104               for (int i = 2; i < (int)(wordBits.size()); i++)
105                 {
106                   word += " " + wordBits[i];
107                 }
108               wordBits.clear();
109               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
110
111               this->description = wordBits[0];
112             }
113           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
114             {
115               this->version = wordBits[1];
116             }
117           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
118             {
119               this->version += "." + wordBits[1];
120             }
121           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
122             {
123               this->version += "." + wordBits[1];
124             }
125         }
126     }
127
128   this->level = level;
129   this->path = path;
130
131   //check all folders and files
132   wxDir dir(crea::std2wx((path).c_str()));
133   if (dir.IsOpened())
134     {
135       wxString fileName;
136
137       //folders
138       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
139       while (cont)
140         {
141           std::string stdfileName = crea::wx2std(fileName);
142           //if src, check for black boxes
143           if(stdfileName == "src")
144             {
145               this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
146               this->children.push_back(this->src);
147             }
148           else
149             {
150               this->children.push_back(new modelCDMFolder(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
151             }
152
153           cont = dir.GetNext(&fileName);
154         }
155
156       //files
157       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
158       while (cont)
159         {
160           std::string stdfileName = crea::wx2std(fileName);
161
162           //if CMakeLists, create CMakeLists
163           if(stdfileName == "CMakeLists.txt")
164             {
165               this->CMakeLists = new modelCDMCMakeListsFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
166               this->children.push_back(this->CMakeLists);
167             }
168           else
169             {
170               this->children.push_back(new modelCDMFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
171             }
172           //if is an unknown file, create file
173           cont = dir.GetNext(&fileName);
174         }
175     }
176   this->SortChildren();
177 }
178
179 modelCDMPackage::~modelCDMPackage()
180 {
181 }
182
183 const std::string& modelCDMPackage::GetNamePackage() const
184 {
185   return this->namePackage;
186 }
187
188 const std::string& modelCDMPackage::GetAuthors() const
189 {
190   return this->authors;
191 }
192
193 const std::string& modelCDMPackage::GetAuthorsEmail() const
194 {
195   return this->authorsEmail;
196 }
197
198 const std::string& modelCDMPackage::GetVersion() const
199 {
200   return this->version;
201 }
202
203 const std::string& modelCDMPackage::GetDescription() const
204 {
205   return this->description;
206 }
207
208 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
209 {
210   return this->src;
211 }
212
213 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
214 {
215   std::vector<std::string> words;
216   CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
217   std::string authorsReal = words[0];
218   for (int i = 1; i < (int)(words.size()); i++)
219     {
220       authorsReal += "/" + words[i];
221     }
222
223   std::string line;
224   //opening original cmakelists
225   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
226   if( !in.is_open())
227     {
228       result = new std::string("CMakeLists.txt file failed to open.");
229       return false;
230     }
231   //opening temporal cmakelists
232   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
233   if( !out.is_open())
234     {
235       result = new std::string("CMakeLists.txt.tmp file failed to open.");
236       return false;
237     }
238   //copying contents from original to temporal and making changes
239   while (getline(in, line))
240     {
241       if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
242         line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
243       out << line << std::endl;
244     }
245   in.close();
246   out.close();
247   //delete old file and rename new file
248 #ifdef _WIN32
249   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
250 #else
251   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
252 #endif
253   
254   if(system(renameCommand.c_str()))
255     {
256       result = new std::string("An error occurred while running '" + renameCommand + "'.");
257       return false;
258     }
259
260   this->authors = authorsReal;
261   return true;
262 }
263
264 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
265 {
266   //TODO: implement method
267   return true;
268 }
269
270 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
271 {
272   std::vector<std::string> vers;
273   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
274
275
276   std::string line;
277   //opening original cmakelists
278   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
279   if( !in.is_open())
280     {
281       result = new std::string("CMakeLists.txt file failed to open.");
282       return false;
283     }
284   //opening temporal cmakelists
285   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
286   if( !out.is_open())
287     {
288       result = new std::string("CMakeLists.txt.tmp file failed to open.");
289       return false;
290     }
291   //copying contents from original to temporal and making changes
292   while (getline(in, line))
293     {
294       if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
295         line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
296       else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
297         line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
298       else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
299         line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
300       out << line << std::endl;
301     }
302   in.close();
303   out.close();
304   //delete old file and rename new file
305 #ifdef _WIN32
306   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
307 #else
308   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
309 #endif
310   
311   if(system(renameCommand.c_str()))
312     {
313       result = new std::string("An error occurred while running '" + renameCommand + "'.");
314       return false;
315     }
316
317   this->version = vers[0] + "." + vers[1] + "." + vers[2];
318   return true;
319 }
320
321 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
322 {
323   std::vector<std::string> words;
324   CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
325   std::string descriptionReal = words[0];
326   for (int i = 1; i < (int)(words.size()); i++)
327     {
328       descriptionReal += " " + words[i];
329     }
330
331   std::string line;
332   //opening original cmakelists
333   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
334   if( !in.is_open())
335     {
336       result = new std::string("CMakeLists.txt file failed to open.");
337       return false;
338     }
339   //opening temporal cmakelists
340   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
341   if( !out.is_open())
342     {
343       result = new std::string("CMakeLists.txt.tmp file failed to open.");
344       return false;
345     }
346   //copying contents from original to temporal and making changes
347   while (getline(in, line))
348     {
349       if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
350         line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
351       out << line << std::endl;
352     }
353   in.close();
354   out.close();
355   //delete old file and rename new file
356 #ifdef _WIN32
357   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
358 #else
359   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
360 #endif
361   
362   if(system(renameCommand.c_str()))
363     {
364       result = new std::string("An error occurred while running '" + renameCommand + "'.");
365       return false;
366     }
367
368   this->description = descriptionReal;
369   return true;
370 }
371
372 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
373     std::string*& result,
374     const std::string& name,
375     const std::string& type,
376     const std::string& format,
377     const std::string& categories,
378     const std::string& authors,
379     const std::string& authorsEmail,
380     const std::string& description
381 )
382 {
383   return this->src->CreateBlackBox(result,name, this->namePackage, type,format,categories,authors,authorsEmail,description);
384 }
385
386 const bool modelCDMPackage::Refresh(std::string*& result)
387 {
388   std::cout << "refreshing package " << this->namePackage << std::endl;
389   this->type = wxDIR_DIRS;
390
391   //Get Package Name
392
393   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
394
395   std::ifstream confFile;
396   confFile.open((pathMakeLists).c_str());
397
398   std::string word;
399   while(confFile.is_open() && !confFile.eof())
400     {
401       //get sets
402       std::getline(confFile,word,'(');
403       std::vector<std::string> wordBits;
404       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
405
406       if(wordBits[wordBits.size()-1] == "SET")
407         {
408           //get package name
409           std::getline(confFile,word,')');
410           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
411           if(wordBits[0] == "BBTK_PACKAGE_NAME")
412             {
413               word = wordBits[1];
414               for (int i = 2; i < (int)(wordBits.size()); i++)
415                 {
416                   word += " " + wordBits[i];
417                 }
418               wordBits.clear();
419               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
420
421               this->namePackage = wordBits[0];
422             }
423           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
424             {
425               word = wordBits[1];
426               for (int i = 2; i < (int)(wordBits.size()); i++)
427                 {
428                   word += " " + wordBits[i];
429                 }
430               wordBits.clear();
431               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
432
433               this->authors = wordBits[0];
434             }
435           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
436             {
437               word = wordBits[1];
438               for (int i = 2; i < (int)(wordBits.size()); i++)
439                 {
440                   word += " " + wordBits[i];
441                 }
442               wordBits.clear();
443               CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
444
445               this->description = wordBits[0];
446             }
447           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
448             {
449               this->version = wordBits[1];
450             }
451           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
452             {
453               this->version += "." + wordBits[1];
454             }
455           else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
456             {
457               this->version += "." + wordBits[1];
458             }
459         }
460     }
461
462
463
464   std::vector<bool> checked(this->children.size(), false);
465   bool checkedSrc = false;
466
467   //check all folders
468   wxDir dir(crea::std2wx((this->path).c_str()));
469   if (dir.IsOpened())
470     {
471       wxString fileName;
472       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
473       while (cont)
474         {
475
476           std::string stdfileName = crea::wx2std(fileName);
477
478           //detect black boxes in src
479           if(stdfileName == "src")
480             {
481               //check if box already exist
482               bool found = false;
483               if (this->src != NULL)
484                 {
485                   found = true;
486                   int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
487                   checked[pos] = true;
488                   checkedSrc = true;
489                   if(!this->src->Refresh(result))
490                     return false;
491                 }
492               else
493                 {
494                   this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
495                   this->children.push_back(this->src);
496                 }
497             }
498           else
499             {
500
501               //check if folder already exist
502               bool found = false;
503               for (int i = 0; !found && i < (int)(this->children.size()); i++)
504                 {
505                   if (this->children[i]->GetName() == stdfileName)
506                     {
507                       found = true;
508                       checked[i] = true;
509                       if(!this->children[i]->Refresh(result))
510                         return false;
511                     }
512                 }
513               if(!found)
514                 {
515                   modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
516                   this->children.push_back(folder);
517                 }
518             }
519           cont = dir.GetNext(&fileName);
520
521         }
522
523       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
524       while (cont)
525         {
526           std::string stdfileName = crea::wx2std(fileName);
527
528           //if CMakeLists, create CMakeLists
529           if(stdfileName == "CMakeLists.txt")
530             {
531               if (this->CMakeLists == NULL)
532                 {
533                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
534                   this->children.push_back(this->CMakeLists);
535                 }
536               else
537                 {
538                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
539                   checked[pos] = true;
540                   if(!this->CMakeLists->Refresh(result))
541                     return false;
542                 }
543             }
544           //if is an unknown file, create file
545           else
546             {
547               bool found = false;
548               for (int i = 0; !found && i < (int)(this->children.size()); i++)
549                 {
550                   if (this->children[i]->GetName() == stdfileName)
551                     {
552                       found = true;
553                       checked[i] = true;
554                       if(!this->children[i]->Refresh(result))
555                         return false;
556                     }
557                 }
558
559               if(!found)
560                 {
561                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
562                   this->children.push_back(file);
563                 }
564             }
565
566           cont = dir.GetNext(&fileName);
567         }
568     }
569
570   if(!checkedSrc)
571     {
572       this->src = NULL;
573     }
574
575   for (int i = 0; i < (int)(checked.size()); i++)
576     {
577       if(!checked[i])
578         {
579           delete this->children[i];
580           this->children.erase(this->children.begin()+i);
581           checked.erase(checked.begin()+i);
582           i--;
583         }
584     }
585   this->SortChildren();
586   return true;
587 }
588
589 void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
590 {
591   //check cmake exist
592   if(this->CMakeLists != NULL)
593     {
594       //open cmakelists
595       std::ifstream confFile;
596       confFile.open((this->CMakeLists->GetPath()).c_str());
597
598       //take everything that is not commented
599       std::string fileContent;
600
601       std::string word;
602       std::vector<std::string> words;
603       while(confFile.is_open() && !confFile.eof())
604         {
605           std::getline(confFile,word, '\n');
606           if(word[0] != '#')
607             {
608               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
609               if (words.size() > 0)
610                 {
611                   word = words[0];
612                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
613                   for (int i = 0; i < (int)(words.size()); i++)
614                     {
615                       if(words[i].substr(0,2) == "//")
616                         break;
617                       fileContent += words[i] + " ";
618                     }
619                 }
620             }
621         }
622
623       //check every instruction
624       std::stringstream ss(fileContent);
625       while(!ss.eof())
626         {
627           std::getline(ss,word, '(');
628
629           //check instruction name
630           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
631
632           //set instructions
633           if (words.size() > 0 && words[words.size()-1] == "SET")
634             {
635               std::getline(ss,word, ')');
636
637               CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
638               if (words.size() > 1)
639                 {
640                   if (words[0] == "${BBTK_PACKAGE_NAME}_USE_VTK" && words[1] == "ON")
641                     {
642                       properties["package " + this->name + " set USE_VTK"] = true;
643                     }
644                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_ITK" && words[1] == "ON")
645                     {
646                       properties["package " + this->name + " set USE_ITK"] = true;
647                     }
648                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM" && words[1] == "ON")
649                     {
650                       properties["package " + this->name + " set USE_GDCM"] = true;
651                     }
652                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM_VTK" && words[1] == "ON")
653                     {
654                       properties["package " + this->name + " set USE_GDCM_VTK"] = true;
655                     }
656                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GSMIS" && words[1] == "ON")
657                     {
658                       properties["package " + this->name + " set USE_GSMIS"] = true;
659                     }
660                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_WXWIDGETS" && words[1] == "ON")
661                     {
662                       properties["package " + this->name + " set USE_WXWIDGETS"] = true;
663                     }
664                   else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_KWWIDGETS" && words[1] == "ON")
665                     {
666                       properties["package " + this->name + " set USE_KWWIDGETS"] = true;
667                     }
668                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
669                     {
670                       properties["package " + this->name + " set USE_BOOST"] = true;
671                     }
672                   else if (words[0] == "${BBTK_PACKAGE_NAME}_INCLUDE_DIRS")
673                     {
674                       for (int i = 1; i < (int)(words.size()); i++)
675                         {
676                           if(words[i].substr(0,2) == "${" || words[i].substr(0,2) == "..")
677                           properties["package " + this->name + " dir " + words[i]] = true;
678                         }
679                     }
680                   else if (words[0] == "${BBTK_PACKAGE_NAME}_LIBS")
681                     {
682                       for (int i = 1; i < (int)(words.size()); i++)
683                         {
684                           properties["package " + this->name + " lib " + words[i]] = true;
685                         }
686                     }
687                 }
688             }
689         }
690
691     }
692 }
693
694 std::map<std::string, bool> modelCDMPackage::Get3rdPartyLibraries()
695 {
696   std::map<std::string, std::string> correspondence;
697   correspondence["${BBTK_PACKAGE_NAME}_USE_VTK"] = "VTK";
698   correspondence["${BBTK_PACKAGE_NAME}_USE_ITK"] = "ITK";
699   correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM"] = "GDCM";
700   correspondence["${BBTK_PACKAGE_NAME}_USE_GDCM_VTK"] = "GDCM_VTK";
701   correspondence["${BBTK_PACKAGE_NAME}_USE_GSMIS"] = "GSMIS";
702   correspondence["${BBTK_PACKAGE_NAME}_USE_WXWIDGETS"] = "WxWidgets";
703   correspondence["${BBTK_PACKAGE_NAME}_USE_KWWIDGETS"] = "KWWidgets";
704   std::map<std::string, bool> res;
705   res["VTK"] = false;
706   res["ITK"] = false;
707   res["GDCM"] = false;
708   res["GDCM_VTK"] = false;
709   res["GSMIS"] = false;
710   res["WxWidgets"] = false;
711   res["KWWidgets"] = false;
712
713   if (this->HasCMakeLists())
714     {
715       std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
716
717       boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+\\s+ON");
718       std::string::const_iterator start, end;
719       start = CMfile.begin();
720       end = CMfile.end();
721       boost::match_results<std::string::const_iterator> what;
722       boost::match_flag_type flags = boost::match_default;
723       while(boost::regex_search(start, end, what, expression, flags))
724         {
725           //std::cout << what[0].str() << std::endl;
726           boost::regex expression1 = boost::regex("\\$\\{BBTK_PACKAGE_NAME\\}_USE_\\w+");
727           std::string::const_iterator start1, end1;
728           start1 = what[0].first;
729           end1 = what[0].second;
730           boost::match_results<std::string::const_iterator> what1;
731           if(boost::regex_search(start1, end1, what1, expression1, flags))
732             {
733               std::string dete = what1.str();
734               CDMUtilities::normalizeStr(dete);
735               //std::cout << dete << std::endl;
736               if(correspondence.find(dete) != correspondence.end())
737                 res[correspondence[dete]] = true;
738             }
739           start = what[0].second;
740         }
741     }
742   return res;
743 }
744
745 bool modelCDMPackage::Set3rdPartyLibrary(const std::string& library_name,
746     const bool& toInclude)
747 {
748   std::map<std::string, std::string> correspondence;
749
750   correspondence["VTK"] = "_USE_VTK";
751   correspondence["ITK"] = "_USE_ITK";
752   correspondence["GDCM"] = "_USE_GDCM";
753   correspondence["GDCM_VTK"] = "_USE_GDCM_VTK";
754   correspondence["GSMIS"] = "_USE_GSMIS";
755   correspondence["WxWidgets"] = "_USE_WXWIDGETS";
756   correspondence["KWWidgets"] = "_USE_KWWIDGETS";
757
758   if (correspondence.find(library_name) != correspondence.end())
759     {
760       std::string library_command = correspondence[library_name];
761 //      std::cout << "found correspondence " << library_command << std::endl;
762 //      std::cout.flush();
763       if (this->HasCMakeLists())
764         {
765           std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
766           std::string resCMfile = "";
767           bool found = false;
768
769 //          std::cout << "found cmakefile: " << CMfile << std::endl;
770 //          std::cout.flush();
771
772           try {
773 //            std::cout << "first regex" << std::endl;
774 //            std::cout.flush();
775             boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
776
777             std::string::const_iterator start, end;
778             start = CMfile.begin();
779             end = CMfile.end();
780             boost::match_results<std::string::const_iterator> what;
781             boost::match_flag_type flags = boost::match_default;
782             if(boost::regex_search(start, end, what, expression, flags))
783               {
784 //                std::cout << "found " << what.str() << std::endl;
785 //                std::cout.flush();
786                 found = true;
787                 resCMfile += what.prefix().str();
788                 if (toInclude)
789                   resCMfile += what.str();
790                 else
791                   resCMfile += "#" + what.str();
792                 resCMfile += what.suffix().str();
793
794                 return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
795               }
796             else
797               {
798 //                std::cout << "second regex" << std::endl;
799 //                std::cout.flush();
800                 boost::regex expression("^\\h*#\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{BBTK_PACKAGE_NAME\\}"+library_command+"([\\s]|#[^\\n]*\\n)+ON([\\s]|#[^\\n]*\\n)*\\)");
801                 if(boost::regex_search(start, end, what, expression, flags))
802                   {
803                     found = true;
804                     resCMfile += what.prefix().str();
805                     if(toInclude)
806                       {
807                         std::string dete = what[0].str();
808                         for (int i = 0; i < dete.size(); ++i) {
809                           if (dete[i] != '#')
810                             resCMfile.push_back(dete[i]);
811                           if (dete[i] == 'S')
812                             {
813                               resCMfile += dete.substr(i+1);
814                               break;
815                             }
816                         }
817                       }
818                     else
819                       resCMfile += what.str();
820
821                     resCMfile += what.suffix().str();
822                     return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
823                   }
824                 else
825                   {
826 //                    std::cout << "third regex" << std::endl;
827 //                    std::cout.flush();
828                     boost::regex expression("^\\h*#\\h*UNCOMMENT EACH LIBRARY NEEDED \\(WILL BE FOUND AND USED AUTOMATICALLY\\)[^\\n]*\\n");
829                     if(boost::regex_search(start, end, what, expression, flags))
830                       {
831                         found = true;
832                         resCMfile += what.prefix().str();
833                         resCMfile += what.str();
834                         if(toInclude)
835                           {
836                             resCMfile += "SET(${BBTK_PACKAGE_NAME}"+ library_command +"  ON)\n";
837                           }
838                         resCMfile += what.suffix().str();
839                         return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
840                       }
841                   }
842               }
843           } catch (boost::bad_expression& e) {
844             std::cout << "bad regex: " << e.what() << std::endl;
845             std::cout.flush();
846           }
847         }
848     }
849   return false;
850 }
851
852 std::map<std::string, bool> modelCDMPackage::GetCustomLibraries()
853 {
854   std::map<std::string, bool> res;
855   res["Test"] = false;
856   return res;
857 }
858
859 bool modelCDMPackage::SetCustomLibrary(const std::string& library_name,
860     const bool& toInclude)
861 {
862   return false;
863 }