]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMProject.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMProject.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  * modelCDMProject.cpp
30  *
31  *  Created on: 13/11/2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMProject.h"
36
37 #include <iostream>
38 #include <sstream>
39 #include <vector>
40 #include <algorithm>
41 #include <fstream>
42 #include <ctime>
43 #include <boost/regex.hpp>
44
45 #include "CDMUtilities.h"
46 #include "creaWx.h"
47 #include "wx/dir.h"
48 #include <wx/config.h>
49
50 modelCDMProject::modelCDMProject()
51 {
52   std::cout << "in constructor1" << std::endl;
53   this->appli = NULL;
54   this->lib = NULL;
55   this->CMakeLists = NULL;
56 }
57
58 modelCDMProject::modelCDMProject(
59     modelCDMIProjectTreeNode* parent,
60     const std::string& path,
61     const std::string& name,
62     const std::string& buildPath
63 )
64 {
65   std::cout << "creating project: " + name + " in " + path + "\n";
66   this->parent = parent;
67   this->path = CDMUtilities::fixPath(path);
68   //open makelists file
69   std::string pathFixed(CDMUtilities::fixPath(path));
70
71   std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
72
73   std::ifstream confFile;
74   confFile.open((pathMakeLists).c_str());
75
76   std::string word;
77   while(confFile.is_open() && !confFile.eof())
78     {
79       //std::cout << "leyendo " << word << std::endl;
80       //get project name
81       std::getline(confFile,word,'(');
82       std::vector<std::string> wordBits;
83       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
84
85       if(wordBits[wordBits.size()-1] == "PROJECT")
86         {
87           std::getline(confFile,word,')');
88           std::vector<std::string> nameBits;
89           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
90
91           this->name = this->nameProject = "";
92           for (int i = 0; i < (int)(nameBits.size()); i++)
93             {
94               if(i != 0)
95                 this->name += " ";
96               this->name += nameBits[i];
97             }
98           this->nameProject = this->name;
99
100         }
101
102
103       if(wordBits[wordBits.size()-1] == "SET")
104         {
105           //get project version
106           std::getline(confFile,word,')');
107           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
108           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
109             {
110               version = wordBits[1];
111             }
112           if(wordBits[0] == "PROJECT_MINOR_VERSION")
113             {
114               version += "." + wordBits[1];
115             }
116           if(wordBits[0] == "PROJECT_BUILD_VERSION")
117             {
118               version += "." + wordBits[1];
119             }
120
121           //get project versionDate
122           if(wordBits[0] == "PROJECT_VERSION_DATE")
123             {
124               std::vector<std::string> versionBits;
125               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
126               versionDate = versionBits[0];
127             }
128           //get project buildPath
129
130           if (buildPath != "")
131             {
132               this->buildPath = buildPath;
133             }
134           else
135             {
136               this->buildPath = this->path + "Bin";
137             }
138         }
139     }
140   confFile.close();
141
142   this->type = wxDIR_DIRS;
143   this->level = 0;
144
145   this->children.clear();
146   this->appli = NULL;
147   this->lib = NULL;
148   this->packages.clear();
149
150
151   //check all folders
152   wxDir dir(crea::std2wx((pathFixed).c_str()));
153   if (dir.IsOpened())
154     {
155       wxString fileName;
156       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
157       while (cont)
158         {
159           std::string stdfileName = crea::wx2std(fileName);
160
161           //if appli, create appli
162           if(stdfileName == "appli")
163             {
164               this->appli = new modelCDMAppli(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
165               this->children.push_back(this->appli);
166             }
167           //if lib, create lib
168           else if(stdfileName == "lib")
169             {
170               this->lib = new modelCDMLib(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
171               this->children.push_back(this->lib);
172             }
173           else
174             {
175               bool isPackage = false;
176
177               // check if cmakelists file exist in folder.
178               std::string tmppath = this->GetPath() + CDMUtilities::SLASH + stdfileName + CDMUtilities::SLASH + "CMakeLists.txt";
179               CDMUtilities::CMLFile CMfile = CDMUtilities::readCMLFile(tmppath.c_str());
180               if (CMfile.size() > 0)
181                 {
182                   std::cout << "-->" << stdfileName << " ::has cmakelists" << std::endl;
183                   // check if cmakelists file has the SET(BBTK_PACKAGE_NAME [pkgname]) instruction.
184                   for (int i = 0; !isPackage && i < CMfile.size(); ++i) {
185                     if (CMfile[i].first == "command" && CMfile[i].second.size() > 1)
186                       {
187                         std::string cm = CMfile[i].second[0];
188
189                         int pos = 1;
190                         while(pos < CMfile[i].second.size()-1 && !isalpha(CMfile[i].second[pos][0]))
191                           pos++;
192
193                         std::string pm1 = CMfile[i].second[pos];
194
195                         std::cout << cm << " " << pm1 << std::endl;
196                         std::transform(cm.begin(), cm.end(), cm.begin(), ::toupper);
197                         std::transform(pm1.begin(), pm1.end(), pm1.begin(), ::toupper);
198                         if(cm == "SET" && pm1 == "BBTK_PACKAGE_NAME")
199                           {
200                             isPackage = true;
201                           }
202                       }
203                   }
204                 }
205
206               // if package, create package
207               if(isPackage)
208                 {
209                   std::cout << "is Package\n";
210                   modelCDMPackage* package = new modelCDMPackage(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
211                   std::cout << "Package created\n";
212                   this->packages.push_back(package);
213                   this->children.push_back(package);
214                 }
215               // if is an unknown folder, create folder
216               else
217                 {
218                   this->children.push_back(new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
219                 }
220
221             }
222
223           cont = dir.GetNext(&fileName);
224         }
225
226       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
227       while (cont)
228         {
229           std::string stdfileName = crea::wx2std(fileName);
230           std::size_t fileTypePos = stdfileName.find_last_of(".");
231           std::string fileType;
232           if(fileTypePos != std::string::npos)
233             fileType = stdfileName.substr(fileTypePos);
234           else
235             fileType = "";
236
237           //if CMakeLists, create CMakeLists
238           if(stdfileName == "CMakeLists.txt")
239             {
240               this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
241               this->children.push_back(this->CMakeLists);
242             }
243           //if is a code file, create modelCDMCodeFile
244           else if(
245               fileType == ".c" ||
246               fileType == ".cxx" ||
247               fileType == ".h" ||
248               fileType == ".cpp" ||
249               fileType == ".txx" ||
250               fileType == ".cmake" )
251             {
252               this->children.push_back(new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
253             }
254           //if is an unknown file, create file
255           else
256             {
257               this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
258             }
259
260           cont = dir.GetNext(&fileName);
261         }
262     }
263
264   this->SortChildren();
265   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
266
267 }
268
269 modelCDMProject::~modelCDMProject()
270 {
271 }
272
273 const std::string& modelCDMProject::GetNameProject() const
274 {
275   return this->nameProject;
276 }
277
278 const std::string& modelCDMProject::GetVersion() const
279 {
280   return this->version;
281 }
282
283 const std::string& modelCDMProject::GetVersionDate() const
284 {
285   return this->versionDate;
286 }
287
288 const std::string& modelCDMProject::GetBuildPath() const
289 {
290   return this->buildPath;
291 }
292
293 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
294 {
295   return this->packages;
296 }
297
298 modelCDMAppli* modelCDMProject::GetAppli() const
299 {
300   return this->appli;
301 }
302
303 modelCDMLib* modelCDMProject::GetLib() const
304 {
305   return this->lib;
306 }
307
308 std::string modelCDMProject::GetBuildInstruction() const
309 {
310   wxConfigBase* pConfig =  wxConfigBase::Get();
311   std::string commandline = crea::wx2std(pConfig->Read(wxT("BUILD_COMMAND"), crea::std2wx(CDMUtilities::BUILD_COMMAND)));
312
313
314   std::string makeComm = "cd \"" + this->buildPath + "\";" + commandline;// -C \"" + this->buildPath + "\""; /*> \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";*/
315   return makeComm;
316 }
317
318 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
319 {
320
321   std::vector<std::string> vers;
322   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
323
324   time_t now = time(0);
325
326   tm ltm;
327 #ifdef _WIN32
328   localtime_s(&ltm, &now);
329 #else
330   ltm = *(localtime(&now));
331 #endif
332
333   std::stringstream date;
334   date << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year;
335
336   //set name of library in CMakeLists inside copied folder
337   std::string line;
338   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
339   if( !in.is_open())
340     {
341       result = new std::string("CMakeLists.txt file failed to open.");
342       return false;
343     }
344   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
345   if( !out.is_open())
346     {
347       result = new std::string("CMakeLists.txt.tmp file failed to open.");
348       return false;
349     }
350   while (getline(in, line))
351     {
352       if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
353         line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
354       else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
355         line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
356       else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
357         line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
358       else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
359         line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
360       out << line << std::endl;
361     }
362   in.close();
363   out.close();
364   //delete old file and rename new file
365 #ifdef _WIN32
366   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
367 #else
368   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
369 #endif
370   if(system(renameCommand.c_str()))
371     {
372       result = new std::string("An error occurred while running '" + renameCommand + "'.");
373       return false;
374     }
375
376   this->version = vers[0] + "." + vers[1] + "." + vers[2];
377   this->versionDate = date.str();
378   return true;
379 }
380
381 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
382 {
383   if(path == "")
384     {
385       result = new std::string("The path cannot be empty");
386       return false;
387     }
388   if(path == this->path)
389     {
390       result = new std::string("The path cannot be same as the project sources");
391       return false;
392     }
393   this->buildPath = path;
394   return true;
395 }
396
397 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
398     const std::string& name,
399     std::string*& result,
400     const std::string& authors,
401     const std::string& authorsEmail,
402     const std::string& description,
403     const std::string& version
404 )
405 {
406   //fixing input parameters
407   std::vector<std::string> words;
408
409   CDMUtilities::splitter::split(words,name," '/\"\\,.",CDMUtilities::splitter::no_empties);
410   std::string nameFixed = "";
411   for (int i = 0; i < (int)(words.size()); i++)
412     {
413       nameFixed += words[i];
414     }
415
416   words.clear();
417   CDMUtilities::splitter::split(words,authors," '/\"\\,.",CDMUtilities::splitter::no_empties);
418   std::string authorFixed;
419   for (int i = 0; i < (int)(words.size()); i++)
420     {
421       authorFixed += words[i];
422     }
423
424   words.clear();
425   std::string descriptionFixed;
426   CDMUtilities::splitter::split(words,authorsEmail," '/\"\\,",CDMUtilities::splitter::no_empties);
427   for (int i = 0; i < (int)(words.size()); i++)
428     {
429       descriptionFixed += words[i] + "/";
430     }
431   words.clear();
432   CDMUtilities::splitter::split(words,description," '\"",CDMUtilities::splitter::no_empties);
433   for (int i = 0; i < (int)(words.size()); i++)
434     {
435       descriptionFixed += "_" + words[i];
436     }
437
438   //call project to create package : use bbCreatePackage <path> <name> [author] [description]
439   std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
440   //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
441   bool resultCommand = 0 != system(creationCommand.c_str());
442 #ifdef _WIN32
443   resultCommand = false;
444 #endif
445   if(resultCommand)
446     {
447       result = new std::string("An error occurred while running '" + creationCommand + "'.");
448       return NULL;
449     }
450
451   //add library to project CMakeLists
452   std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
453   if (out1.is_open())
454     {
455       out1 << "ADD_SUBDIRECTORY(bbtk_" << nameFixed << "_PKG)" << std::endl;
456       out1.close();
457     }
458
459   //add library to model
460   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
461   this->packages.push_back(package);
462   this->children.push_back(package);
463
464   //TODO: set package version
465
466   this->SortChildren();
467
468   result = new std::string(this->path + CDMUtilities::SLASH + name);
469   return package;
470 }
471
472 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
473     const std::string& name,
474     std::string*& result,
475     const std::string& path
476 )
477 {
478   if(this->lib != NULL)
479     {
480       return this->lib->CreateLibrary(name, result);
481     }
482   result = new std::string("there is no lib folder in this project.");
483   return NULL;
484 }
485
486 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
487     const std::string& name,
488     const int& type,
489     std::string*& result,
490     const std::string& path
491 )
492 {
493   if(this->appli != NULL)
494     {
495       return this->appli->CreateApplication(name, type, result);
496     }
497   result = new std::string("there is no appli folder in this project.");
498   return NULL;
499 }
500
501 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
502     const std::string& name,
503     const std::string& package,
504     const std::string& authors,
505     const std::string& authorsEmail,
506     const std::string& categories,
507     const std::string& description
508 )
509 {
510   //TODO: implement method
511   return NULL;
512 }
513
514 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
515 {
516   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
517     return true;
518   else
519     {
520       result = new std::string("Couldn't open CMakeLists file.");
521       return false;
522     }
523 }
524
525 const bool modelCDMProject::Refresh(std::string*& result)
526 {
527   std::cout << "refreshing project" << std::endl;
528   //open makelists file
529   std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
530
531   std::ifstream confFile;
532   confFile.open((pathMakeLists).c_str());
533
534   std::string word;
535   while(confFile.is_open() && !confFile.eof())
536     {
537       //std::cout << "leyendo " << word << std::endl;
538       //get project name
539       std::getline(confFile,word,'(');
540       std::vector<std::string> wordBits;
541       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
542
543       if(wordBits[wordBits.size()-1] == "PROJECT")
544         {
545           std::getline(confFile,word,')');
546           std::vector<std::string> nameBits;
547           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
548
549           this->name = this->nameProject = "";
550           for (int i = 0; i < (int)(nameBits.size()); i++)
551             {
552               if(i != 0)
553                 this->name += " ";
554               this->name += nameBits[i];
555             }
556           this->nameProject = this->name;
557
558         }
559
560
561       if(wordBits[wordBits.size()-1] == "SET")
562         {
563           //get project version
564           std::getline(confFile,word,')');
565           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
566           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
567             {
568               version = wordBits[1];
569             }
570           if(wordBits[0] == "PROJECT_MINOR_VERSION")
571             {
572               version += "." + wordBits[1];
573             }
574           if(wordBits[0] == "PROJECT_BUILD_VERSION")
575             {
576               version += "." + wordBits[1];
577             }
578
579           //get project versionDate
580           if(wordBits[0] == "PROJECT_VERSION_DATE")
581             {
582               std::vector<std::string> versionBits;
583               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
584               versionDate = versionBits[0];
585             }
586         }
587     }
588   confFile.close();
589
590   this->type = wxDIR_DIRS;
591   this->level = 0;
592
593   std::vector<bool> checked(this->children.size(), false);
594   std::vector<bool> checkedPackages(this->packages.size(), false);
595
596   //check all folders
597   wxDir dir(crea::std2wx((this->path).c_str()));
598   if (dir.IsOpened())
599     {
600       wxString fileName;
601       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
602       while (cont)
603         {
604           std::string stdfileName = crea::wx2std(fileName);
605
606           //if appli, create appli
607           if(stdfileName == "appli")
608             {
609               if (this->appli == NULL)
610                 {
611                   this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
612                   this->children.push_back(this->appli);
613                 }
614               else
615                 {
616                   int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
617                   checked[pos] = true;
618                   if(!this->appli->Refresh(result))
619                     return false;
620                 }
621             }
622           //if lib, create lib
623           else if(stdfileName == "lib")
624             {
625               if (this->lib == NULL)
626                 {
627                   this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
628                   this->children.push_back(this->lib);
629                 }
630               else
631                 {
632                   int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
633                   checked[pos] = true;
634                   if(!this->lib->Refresh(result))
635                     return false;
636                 }
637
638             }
639           //if package , create package
640           /*else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
641             {
642               bool found = false;
643               for (int i = 0; !found && i < (int)(this->packages.size()); i++)
644                 {
645                   if (this->packages[i]->GetName() == stdfileName)
646                     {
647                       found = true;
648                       int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
649                       checked[pos] = true;
650                       checkedPackages[i] = true;
651                       if(!this->packages[i]->Refresh(result))
652                         return false;
653                     }
654                 }
655               if(!found)
656                 {
657                   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
658                   this->packages.push_back(package);
659                   this->children.push_back(package);
660                 }
661
662             }*/
663           // package or folder
664           else
665             {
666               bool isPackage = false;
667
668               // check if cmakelists file exist in folder.
669               std::string tmppath = this->GetPath() + CDMUtilities::SLASH + stdfileName + CDMUtilities::SLASH + "CMakeLists.txt";
670               CDMUtilities::CMLFile CMfile = CDMUtilities::readCMLFile(tmppath.c_str());
671               if (CMfile.size() > 0)
672
673                 {
674                   // check if cmakelists file has the SET(BBTK_PACKAGE_NAME [pkgname]) instruction.
675                   for (int i = 0; !isPackage && i < CMfile.size(); ++i) {
676                     if (CMfile[i].first == "command" && CMfile[i].second.size() > 1)
677                       {
678                         std::string cm = CMfile[i].second[0];
679
680                         int pos = 1;
681                         while(pos < CMfile[i].second.size()-1 && !isalpha(CMfile[i].second[pos][0]))
682                           pos++;
683
684                         std::string pm1 = CMfile[i].second[pos];
685
686                         std::cout << cm << " " << pm1 << std::endl;
687                         std::transform(cm.begin(), cm.end(), cm.begin(), ::toupper);
688                         std::transform(pm1.begin(), pm1.end(), pm1.begin(), ::toupper);
689                         if(cm == "SET" && pm1 == "BBTK_PACKAGE_NAME")
690                           {
691                             isPackage = true;
692                           }
693                       }
694                   }
695                 }
696
697               // if package, create package
698               if (isPackage)
699                 {
700                   bool found = false;
701                   for (int i = 0; !found && i < (int)(this->packages.size()); i++)
702                     {
703                       if (this->packages[i]->GetName() == stdfileName)
704                         {
705                           found = true;
706                           int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
707                           checked[pos] = true;
708                           checkedPackages[i] = true;
709                           if(!this->packages[i]->Refresh(result))
710                             return false;
711                         }
712                     }
713
714                   if (!found)
715                     {
716                       modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
717                       this->packages.push_back(package);
718                       this->children.push_back(package);
719                     }
720                 }
721               //if is an unknown folder, create folder
722               else
723                 {
724                   bool found = false;
725                   for (int i = 0; !found && i < (int)(this->children.size()); i++)
726                     {
727                       if (this->children[i]->GetName() == stdfileName)
728                         {
729                           found = true;
730                           checked[i] = true;
731                           if(!this->children[i]->Refresh(result))
732                             return false;
733                         }
734                     }
735
736                   if(!found)
737                     {
738                       modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
739                       this->children.push_back(folder);
740                     }
741                 }
742             }
743
744           cont = dir.GetNext(&fileName);
745         }
746
747       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
748       while (cont)
749         {
750           std::string stdfileName = crea::wx2std(fileName);
751           std::size_t fileTypePos = stdfileName.find_last_of(".");
752           std::string fileType;
753           if(fileTypePos != std::string::npos)
754             fileType = stdfileName.substr(fileTypePos);
755           else
756             fileType = "";
757
758           //if CMakeLists, create CMakeLists
759           if(stdfileName == "CMakeLists.txt")
760             {
761               if (this->CMakeLists == NULL)
762                 {
763                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
764                   this->children.push_back(this->CMakeLists);
765                 }
766               else
767                 {
768                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
769                   checked[pos] = true;
770                   if(!this->CMakeLists->Refresh(result))
771                     return false;
772                 }
773             }
774           //if is a code file, create modelCDMCodeFile
775           //if is an unknown file, create file
776           else
777             {
778               bool found = false;
779               for (int i = 0; !found && i < (int)(this->children.size()); i++)
780                 {
781                   if (this->children[i]->GetName() == stdfileName)
782                     {
783                       found = true;
784                       checked[i] = true;
785                       if(!this->children[i]->Refresh(result))
786                         return false;
787                     }
788                 }
789
790               if(!found)
791                 {
792                   if(
793                     fileType == ".c" ||
794                     fileType == ".cxx" ||
795                     fileType == ".h" ||
796                     fileType == ".cpp" ||
797                     fileType == ".txx" ||
798                     fileType == ".cmake" )
799                     {
800                       this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
801                     }
802                   else
803                     {
804                       modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
805                       this->children.push_back(file);
806                     }
807                 }
808             }
809
810           cont = dir.GetNext(&fileName);
811         }
812     }
813
814   for (int i = 0; i < (int)(checkedPackages.size()); i++)
815     {
816       if(!checkedPackages[i])
817         {
818           this->packages.erase(this->packages.begin()+i);
819           checkedPackages.erase(checkedPackages.begin()+i);
820           i--;
821         }
822     }
823   for (int i = 0; i < (int)(checked.size()); i++)
824     {
825       if(!checked[i])
826         {
827           delete this->children[i];
828           this->children.erase(this->children.begin()+i);
829           checked.erase(checked.begin()+i);
830           i--;
831         }
832     }
833
834   this->SortChildren();
835   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
836   return true;
837 }
838
839 bool modelCDMProject::ConfigureBuild(std::string*& result)
840 {
841   //TODO: adjust for windows and mac
842 #ifdef _WIN32
843   // ------ Windows
844   if(0 == system("cmake-gui"))
845     return true;
846   else
847     {
848           result = new std::string("There was an error opening cmake-gui. Please make sure it's installed and that cmake's bin folder is in the system path.");
849       return false;
850     }
851 #elif __APPLE__
852   // ------ Apple
853 #else
854   // ------ Linux
855   //open binary folder
856   wxDir dir(crea::std2wx((this->buildPath).c_str()));
857
858   //if folder doesn't exist then create it
859   if (!dir.IsOpened())
860     {
861       //create command line to create folder
862       std::string createComm = "mkdir \"" + this->buildPath + "\"";
863       //execute creation command
864       if (system(createComm.c_str()))
865         {
866           //if there was an error then report it
867           result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
868           return false;
869         }
870     }
871   //create command line to execute ccmake
872   //TODO:: adjust for different Linux distributions
873   std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
874   //execute command
875   if(CDMUtilities::openTerminal(confComm))
876     {
877       //if there was an error then report it
878       result = new std::string("There was an error opening the configuration tool in the desired place.");
879       return false;
880     }
881 #endif
882   return true;
883 }
884
885 bool modelCDMProject::Build(std::string*& result, const std::string& line)
886 {
887   //TODO: adjust for windows and mac
888 #ifdef _WIN32
889   // ------ Windows
890         std::string command = "start \"\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"";
891   //wxMessageBox(crea::std2wx(command), wxT("Project Compilation - Check!"));
892         if(0 == system(command.c_str()))
893     return true;
894   else
895     {
896       result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ installed.");
897           return false;
898     }
899 #elif __APPLE__
900   // ------ Apple
901 #else
902   // ------ Linux
903   //open binary folder
904   wxDir dir(crea::std2wx((this->buildPath).c_str()));
905
906   //if binary folder can't be opened then return false
907   if (!dir.IsOpened())
908     {
909       result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
910       return false;
911     }
912   //create make command
913   std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
914   for (int i = 0; i < line.size(); i++)
915     {
916       if(line[i] == '"')
917         {
918           makeComm+="\\\\\\\"";
919         }
920       else if(line[i] == '\\')
921         {
922           makeComm+="\\\\\\\\";
923         }
924       else
925         {
926           makeComm.push_back(line[i]);
927         }
928     }
929   makeComm += "; echo -e '\\a'; bash";
930   makeComm += "\\\"\"";
931
932   std::cout << "executing '" << makeComm << "'" << std::endl;
933   //execute make command
934   if(system(makeComm.c_str()))
935     {
936       //if there was an error then report it
937       result = new std::string("There was an error compiling the project, please check the \"building.log\" file located in the build folder to read more about the problem.");
938       return false;
939     }
940 #endif
941   return true;
942 }
943
944 bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
945 {
946   //TODO: adjust for mac
947 #ifdef _WIN32
948   // ------ Windows
949   //open binary folder
950   wxDir dir(crea::std2wx(folder));
951
952   //if binary folder can't be opened then return false
953   if (!dir.IsOpened())
954     {
955       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
956       return false;
957     }
958   //create plug command
959   std::string plugComm = "bbPlugPackage \"" + folder + "\"";
960   std::cout << "executing '" << plugComm << "'" << std::endl;
961   //execute plug command
962   if(system(std::string("start cmd.exe /k \"" + plugComm + "\"").c_str()))
963     {
964       //if there was an error then report it
965       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
966       return false;
967     }
968 #elif __APPLE__
969   // ------ Apple
970 #else
971   // ------ Linux
972   //open binary folder
973   wxDir dir(crea::std2wx(folder));
974
975   //if binary folder can't be opened then return false
976   if (!dir.IsOpened())
977     {
978       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
979       return false;
980     }
981   //create plug command
982   std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\"";
983
984   std::string Comm = "gnome-terminal -e \"bash -c \\\"";
985     for (int i = 0; i < plugComm.size(); i++)
986       {
987         if(plugComm[i] == '"')
988           {
989             Comm+="\\\\\\\"";
990           }
991         else if(plugComm[i] == '\\')
992           {
993             Comm+="\\\\\\\\";
994           }
995         else
996           {
997             Comm.push_back(plugComm[i]);
998           }
999       }
1000     Comm += "; echo -e '\\a'; bash";
1001     Comm += "\\\"\"";
1002
1003
1004   std::cout << "executing '" << Comm << "'" << std::endl;
1005   //execute plug command
1006   if(system(Comm.c_str()))
1007     {
1008       //if there was an error then report it
1009       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
1010       return false;
1011     }
1012 #endif
1013   return true;
1014 }
1015
1016 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
1017 {
1018   //check cmake exist
1019   if(this->CMakeLists != NULL)
1020     {
1021       //set properties parameters based on model
1022       properties["project add appli"] = this->appli != NULL;
1023       properties["project add lib"] = this->lib != NULL;
1024       for (int i = 0; i < (int)(this->packages.size()); i++)
1025         properties["project add " + packages[i]->GetName()] = false;
1026
1027       //open cmakelists
1028       std::ifstream confFile;
1029       confFile.open((this->CMakeLists->GetPath()).c_str());
1030
1031       //take everything that is not commented
1032       std::string fileContent;
1033
1034       std::string word;
1035       std::vector<std::string> words;
1036       while(confFile.is_open() && !confFile.eof())
1037         {
1038           std::getline(confFile,word, '\n');
1039           if(word[0] != '#')
1040             {
1041               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
1042               if (words.size() > 0)
1043                 {
1044                   word = words[0];
1045                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
1046                   for (int i = 0; i < (int)(words.size()); i++)
1047                     {
1048                       if(words[i].substr(0,2) == "//")
1049                         break;
1050                       fileContent += words[i] + " ";
1051                     }
1052                 }
1053
1054             }
1055         }
1056
1057       //check every instruction
1058       std::stringstream ss(fileContent);
1059       while(!ss.eof())
1060         {
1061           std::getline(ss,word, '(');
1062
1063           //check instruction name
1064           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
1065
1066           //set instructions
1067           if (words.size() > 0 && words[words.size()-1] == "SET")
1068             {
1069               std::getline(ss,word, ')');
1070
1071               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
1072               if (words.size() > 1)
1073                 {
1074                   if (words[0] == "USE_CREA" && words[1] == "ON")
1075                     {
1076                       properties["project set USE_CREA"] = true;
1077                     }
1078                   else if (words[0] == "USE_GDCM" && words[1] == "ON")
1079                     {
1080                       properties["project set USE_GDCM"] = true;
1081                     }
1082                   else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
1083                     {
1084                       properties["project set USE_GDCM_VTK"] = true;
1085                     }
1086                   else if (words[0] == "USE_GDCM2" && words[1] == "ON")
1087                     {
1088                       properties["project set USE_GDCM2"] = true;
1089                     }
1090                   else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
1091                     {
1092                       properties["project set USE_WXWIDGETS"] = true;
1093                     }
1094                   else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
1095                     {
1096                       properties["project set USE_KWWIDGETS"] = true;
1097                     }
1098                   else if (words[0] == "USE_VTK" && words[1] == "ON")
1099                     {
1100                       properties["project set USE_VTK"] = true;
1101                     }
1102                   else if (words[0] == "USE_ITK" && words[1] == "ON")
1103                     {
1104                       properties["project set USE_ITK"] = true;
1105                     }
1106                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
1107                     {
1108                       properties["project set USE_BOOST"] = true;
1109                     }
1110                 }
1111             }
1112           //add instructions
1113           else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
1114             {
1115               std::getline(ss,word, ')');
1116               //std::cout << word << std::endl;
1117               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
1118
1119               if (words.size() > 0)
1120                 {
1121                   if(words[0] == "appli")
1122                     {
1123                       properties["project add "+words[0]] = this->appli != NULL;
1124                     }
1125                   else if(words[0] == "lib")
1126                     {
1127                       properties["project add "+words[0]] = this->lib != NULL;
1128                     }
1129                   else
1130                     {
1131                       properties["project add "+words[0]] = true;
1132                     }
1133                 }
1134             }
1135         }
1136
1137     }
1138
1139   //check appli's structure
1140   this->appli->CheckStructure(properties);
1141   //check lib's structure
1142   this->lib->CheckStructure(properties);
1143   //check packages' structure
1144   for (int i = 0; i < (int)(this->packages.size()); i++)
1145     {
1146       properties["package " + this->packages[i]->GetName()] = true;
1147       this->packages[i]->CheckStructure(properties);
1148     }
1149 }
1150
1151 bool modelCDMProject::IsPackageIncluded(const std::string& package_name)
1152 {
1153   if (this->HasCMakeLists())
1154     {
1155       CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1156       for (int i = 0; i < cmlFile.size(); ++i)
1157         {
1158           if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1159             {
1160               int pos = 1;
1161               while (pos < cmlFile[i].second.size())
1162                 {
1163                   if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1164                     {
1165                       if (package_name == cmlFile[i].second[pos])
1166                         return true;
1167                       break;
1168                     }
1169                   pos++;
1170                 }
1171             }
1172         }
1173     }
1174   return false;
1175
1176 }
1177
1178 bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude)
1179 {
1180   if (this->HasCMakeLists())
1181     {
1182       CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1183
1184       bool found = false;
1185
1186       for (int i = 0; i < cmlFile.size(); ++i)
1187         {
1188           if(toInclude && cmlFile[i].first == "comment")
1189             {
1190               std::vector<std::string> segments;
1191               std::string line = cmlFile[i].second[0];
1192               while(line[0] == '#')
1193                 line.erase(0,1);
1194
1195               CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
1196               if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name)
1197                 {
1198                   found = true;
1199                   while(cmlFile[i].second[0][0] == '#')
1200                     cmlFile[i].second[0].erase(0,1);
1201                 }
1202             }
1203           else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1204             {
1205               int pos = 1;
1206               while (pos < cmlFile[i].second.size())
1207                 {
1208                   if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1209                     {
1210                       if (package_name == cmlFile[i].second[pos])
1211                         {
1212                           found = true;
1213                           if (!toInclude)
1214                             {
1215                               cmlFile[i].first = "comment";
1216                               cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
1217                               while (cmlFile[i].second.size() > 1)
1218                                 {
1219                                   cmlFile[i].second[0] += cmlFile[i].second[1];
1220                                   cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
1221                                 }
1222
1223                             }
1224                         }
1225                       break;
1226                     }
1227                   pos++;
1228                 }
1229             }
1230         }
1231       if (!found && toInclude)
1232         {
1233           CDMUtilities::syntaxElement element;
1234           element.first = "command";
1235           element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")");
1236           cmlFile.push_back(element);
1237         }
1238
1239       return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
1240     }
1241   return false;
1242 }
1243
1244 std::map<std::string, bool> modelCDMProject::Get3rdPartyLibraries()
1245 {
1246   std::map<std::string, std::string> correspondence;
1247   correspondence["USE_CREA"] = "Crea";
1248   correspondence["USE_GDCM"] = "GDCM";
1249   correspondence["USE_GDCM_VTK"] = "GDCM_VTK";
1250   correspondence["USE_GDCM2"] = "GDCM2";
1251   correspondence["USE_WXWIDGETS"] = "WxWidgets";
1252   correspondence["USE_KWWIDGETS"] = "KWWidgets";
1253   correspondence["USE_VTK"] = "VTK";
1254   correspondence["USE_ITK"] = "ITK";
1255   correspondence["USE_BOOST"] = "Boost";
1256
1257   std::map<std::string, bool> res;
1258   res["Crea"] = false;
1259   res["GDCM"] = false;
1260   res["GDCM_VTK"] = false;
1261   res["GDCM2"] = false;
1262   res["WxWidgets"] = false;
1263   res["KWWidgets"] = false;
1264   res["VTK"] = false;
1265   res["ITK"] = false;
1266   res["Boost"] = false;
1267
1268   if (this->HasCMakeLists())
1269     {
1270       std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1271
1272       boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*USE_\\w+\\s+ON");
1273       std::string::const_iterator start, end;
1274       start = CMfile.begin();
1275       end = CMfile.end();
1276       boost::match_results<std::string::const_iterator> what;
1277       boost::match_flag_type flags = boost::match_default;
1278       while(boost::regex_search(start, end, what, expression, flags))
1279         {
1280           //std::cout << what[0].str() << std::endl;
1281           boost::regex expression1 = boost::regex("USE_\\w+");
1282           std::string::const_iterator start1, end1;
1283           start1 = what[0].first;
1284           end1 = what[0].second;
1285           boost::match_results<std::string::const_iterator> what1;
1286           if(boost::regex_search(start1, end1, what1, expression1, flags))
1287             {
1288               std::string dete = what1.str();
1289               CDMUtilities::normalizeStr(dete);
1290               //std::cout << dete << std::endl;
1291               if(correspondence.find(dete) != correspondence.end())
1292                 res[correspondence[dete]] = true;
1293             }
1294           start = what[0].second;
1295         }
1296     }
1297
1298   return res;
1299 }
1300
1301 bool modelCDMProject::Set3rdPartyLibrary(const std::string& library_name,
1302     const bool& toInclude)
1303 {
1304   std::map<std::string, std::string> correspondence;
1305   correspondence["Crea"] = "USE_CREA";
1306   correspondence["GDCM"] = "USE_GDCM";
1307   correspondence["GDCM_VTK"] = "USE_GDCM_VTK";
1308   correspondence["GDCM2"] = "USE_GDCM2";
1309   correspondence["WxWidgets"] = "USE_WXWIDGETS";
1310   correspondence["KWWidgets"] = "USE_KWWIDGETS";
1311   correspondence["VTK"] = "USE_VTK";
1312   correspondence["ITK"] = "USE_ITK";
1313   correspondence["Boost"] = "USE_BOOST";
1314
1315   if (correspondence.find(library_name) != correspondence.end())
1316     {
1317       std::string library_command = correspondence[library_name];
1318
1319       if (this->HasCMakeLists())
1320         {
1321           std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1322           std::string resCMfile = "";
1323
1324           std::string::const_iterator start, end;
1325           boost::match_results<std::string::const_iterator> what, tmp;
1326           boost::match_flag_type flags = boost::match_default;
1327           bool found = false;
1328
1329           start = CMfile.begin();
1330           end = CMfile.end();
1331
1332           //search for existing inclusions
1333           boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
1334           while(boost::regex_search(start, end, what, expression, flags))
1335             {
1336               found = true;
1337               resCMfile += what.prefix().str();
1338               if(!toInclude)
1339                 {
1340                   std::string dete = what.str();
1341                   int pos = dete.find("ON",0);
1342                   dete.replace(pos, 2, "OFF");
1343                   resCMfile += dete;
1344                 }
1345               else
1346                 resCMfile += what.str();
1347               tmp = what;
1348               start = what[0].second;
1349             }
1350
1351           if (found)
1352             resCMfile += tmp.suffix().str();
1353           else
1354             {
1355               start = CMfile.begin();
1356               end = CMfile.end();
1357
1358               //search for existing exclusions
1359               boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+OFF\\s*\\)");
1360               while(boost::regex_search(start, end, what, expression, flags))
1361                 {
1362                   found = true;
1363                   resCMfile += what.prefix().str();
1364                   if(toInclude)
1365                     {
1366                       std::string dete = what.str();
1367                       int pos = dete.find("OFF",0);
1368                       dete.replace(pos, 3, "ON");
1369                       resCMfile += dete;
1370                     }
1371                   else
1372                     resCMfile += what.str();
1373                   tmp = what;
1374                   start = what[0].second;
1375                 }
1376
1377               if (found)
1378                 resCMfile += tmp.suffix().str();
1379               else
1380                 {
1381                   start = CMfile.begin();
1382                   end = CMfile.end();
1383
1384                   //search for existing commented inclusions
1385                   expression = boost::regex("^\\h*#+\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
1386                   while(boost::regex_search(start, end, what, expression, flags))
1387                     {
1388                       found = true;
1389                       resCMfile += what.prefix().str();
1390                       if(toInclude)
1391                         {
1392                           std::string dete = what.str();
1393                           for (int i = 0; i < dete.size(); ++i) {
1394                             if(dete[i] == '#')
1395                               {
1396                                 dete.erase(i,1);
1397                                 i--;
1398                               }
1399                           }
1400                           resCMfile += dete;
1401                         }
1402                       else
1403                         resCMfile += what.str();
1404
1405                       tmp = what;
1406                       start = what[0].second;
1407                     }
1408
1409                   if (found)
1410                     resCMfile += tmp.suffix().str();
1411                   else
1412                     {
1413                       if(toInclude)
1414                         {
1415                           start = CMfile.begin();
1416                           end = CMfile.end();
1417
1418                           //search for position to insert
1419                           expression = boost::regex("^\\h*#\\h*Libraries\\/tools used\\h*$");
1420                           if(boost::regex_search(start, end, what, expression, flags))
1421                             {
1422                               found = true;
1423                               resCMfile += what.prefix().str();
1424                               resCMfile += what.str();
1425                               resCMfile += "\nSET(" + library_command + " ON)";
1426                               resCMfile += what.suffix().str();
1427                             }
1428                         }
1429                       else
1430                         {
1431                           found = true;
1432                         }
1433                     }
1434                 }
1435             }
1436           if (!found) {
1437             return false;
1438           }
1439           else
1440             return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);
1441         }
1442     }
1443
1444   return false;
1445 }