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