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