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