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