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