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