2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
31 * Created on: 13/11/2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "modelCDMProject.h"
43 #include <boost/regex.hpp>
45 #include "CDMUtilities.h"
49 modelCDMProject::modelCDMProject()
51 std::cout << "in constructor1" << std::endl;
54 this->CMakeLists = NULL;
57 modelCDMProject::modelCDMProject(
58 modelCDMIProjectTreeNode* parent,
59 const std::string& path,
60 const std::string& name,
61 const std::string& buildPath
64 std::cout << "creating project: " + name + " in " + path + "\n";
65 this->parent = parent;
66 this->path = CDMUtilities::fixPath(path);
68 std::string pathFixed(CDMUtilities::fixPath(path));
70 std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
72 std::ifstream confFile;
73 confFile.open((pathMakeLists).c_str());
76 while(confFile.is_open() && !confFile.eof())
78 //std::cout << "leyendo " << word << std::endl;
80 std::getline(confFile,word,'(');
81 std::vector<std::string> wordBits;
82 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
84 if(wordBits[wordBits.size()-1] == "PROJECT")
86 std::getline(confFile,word,')');
87 std::vector<std::string> nameBits;
88 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
90 this->name = this->nameProject = "";
91 for (int i = 0; i < (int)(nameBits.size()); i++)
95 this->name += nameBits[i];
97 this->nameProject = this->name;
102 if(wordBits[wordBits.size()-1] == "SET")
104 //get project version
105 std::getline(confFile,word,')');
106 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
107 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
109 version = wordBits[1];
111 if(wordBits[0] == "PROJECT_MINOR_VERSION")
113 version += "." + wordBits[1];
115 if(wordBits[0] == "PROJECT_BUILD_VERSION")
117 version += "." + wordBits[1];
120 //get project versionDate
121 if(wordBits[0] == "PROJECT_VERSION_DATE")
123 std::vector<std::string> versionBits;
124 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
125 versionDate = versionBits[0];
127 //get project buildPath
131 this->buildPath = buildPath;
135 this->buildPath = this->path + "Bin";
141 this->type = wxDIR_DIRS;
144 this->children.clear();
147 this->packages.clear();
151 wxDir dir(crea::std2wx((pathFixed).c_str()));
155 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
158 std::string stdfileName = crea::wx2std(fileName);
160 //if appli, create appli
161 if(stdfileName == "appli")
163 this->appli = new modelCDMAppli(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
164 this->children.push_back(this->appli);
167 else if(stdfileName == "lib")
169 this->lib = new modelCDMLib(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
170 this->children.push_back(this->lib);
172 //if package , create package
173 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
175 modelCDMPackage* package = new modelCDMPackage(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
176 this->packages.push_back(package);
177 this->children.push_back(package);
179 //if is an unknown folder, create folder
182 this->children.push_back(new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
185 cont = dir.GetNext(&fileName);
188 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
191 std::string stdfileName = crea::wx2std(fileName);
192 std::size_t fileTypePos = stdfileName.find_last_of(".");
193 std::string fileType = stdfileName.substr(fileTypePos);
195 //if CMakeLists, create CMakeLists
196 if(stdfileName == "CMakeLists.txt")
198 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
199 this->children.push_back(this->CMakeLists);
201 //if is a code file, create modelCDMCodeFile
204 fileType == ".cxx" ||
206 fileType == ".cpp" ||
207 fileType == ".txx" ||
208 fileType == ".cmake" )
210 this->children.push_back(new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
212 //if is an unknown file, create file
215 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
218 cont = dir.GetNext(&fileName);
222 this->SortChildren();
223 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
227 modelCDMProject::~modelCDMProject()
231 const std::string& modelCDMProject::GetNameProject() const
233 return this->nameProject;
236 const std::string& modelCDMProject::GetVersion() const
238 return this->version;
241 const std::string& modelCDMProject::GetVersionDate() const
243 return this->versionDate;
246 const std::string& modelCDMProject::GetBuildPath() const
248 return this->buildPath;
251 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
253 return this->packages;
256 modelCDMAppli* modelCDMProject::GetAppli() const
261 modelCDMLib* modelCDMProject::GetLib() const
266 std::string modelCDMProject::GetBuildInstruction() const
268 std::string makeComm = "make -C \"" + this->buildPath + "\""; /*> \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";*/
272 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
275 std::vector<std::string> vers;
276 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
278 time_t now = time(0);
282 localtime_s(<m, &now);
284 ltm = *(localtime(&now));
287 std::stringstream date;
288 date << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year;
290 //set name of library in CMakeLists inside copied folder
292 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
295 result = new std::string("CMakeLists.txt file failed to open.");
298 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
301 result = new std::string("CMakeLists.txt.tmp file failed to open.");
304 while (getline(in, line))
306 if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
307 line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
308 else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
309 line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
310 else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
311 line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
312 else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
313 line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
314 out << line << std::endl;
318 //delete old file and rename new file
320 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
322 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
324 if(system(renameCommand.c_str()))
326 result = new std::string("An error occurred while running '" + renameCommand + "'.");
330 this->version = vers[0] + "." + vers[1] + "." + vers[2];
331 this->versionDate = date.str();
335 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
339 result = new std::string("The path cannot be empty");
342 if(path == this->path)
344 result = new std::string("The path cannot be same as the project sources");
347 this->buildPath = path;
351 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
352 const std::string& name,
353 std::string*& result,
354 const std::string& authors,
355 const std::string& authorsEmail,
356 const std::string& description,
357 const std::string& version
360 //fixing input parameters
361 std::vector<std::string> words;
363 CDMUtilities::splitter::split(words,name," '/\"\\,.",CDMUtilities::splitter::no_empties);
364 std::string nameFixed = "";
365 for (int i = 0; i < (int)(words.size()); i++)
367 nameFixed += words[i];
371 CDMUtilities::splitter::split(words,authors," '/\"\\,.",CDMUtilities::splitter::no_empties);
372 std::string authorFixed;
373 for (int i = 0; i < (int)(words.size()); i++)
375 authorFixed += words[i];
379 std::string descriptionFixed;
380 CDMUtilities::splitter::split(words,authorsEmail," '/\"\\,",CDMUtilities::splitter::no_empties);
381 for (int i = 0; i < (int)(words.size()); i++)
383 descriptionFixed += words[i] + "/";
386 CDMUtilities::splitter::split(words,description," '\"",CDMUtilities::splitter::no_empties);
387 for (int i = 0; i < (int)(words.size()); i++)
389 descriptionFixed += "_" + words[i];
392 //call project to create package : use bbCreatePackage <path> <name> [author] [description]
393 std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
394 //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
395 bool resultCommand = 0 != system(creationCommand.c_str());
397 resultCommand = false;
401 result = new std::string("An error occurred while running '" + creationCommand + "'.");
405 //add library to project CMakeLists
406 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
409 out1 << "ADD_SUBDIRECTORY(bbtk_" << nameFixed << "_PKG)" << std::endl;
413 //add library to model
414 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
415 this->packages.push_back(package);
416 this->children.push_back(package);
418 //TODO: set package version
420 this->SortChildren();
422 result = new std::string(this->path + CDMUtilities::SLASH + name);
426 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
427 const std::string& name,
428 std::string*& result,
429 const std::string& path
432 if(this->lib != NULL)
434 return this->lib->CreateLibrary(name, result);
436 result = new std::string("there is no lib folder in this project.");
440 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
441 const std::string& name,
443 std::string*& result,
444 const std::string& path
447 if(this->appli != NULL)
449 return this->appli->CreateApplication(name, type, result);
451 result = new std::string("there is no appli folder in this project.");
455 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
456 const std::string& name,
457 const std::string& package,
458 const std::string& authors,
459 const std::string& authorsEmail,
460 const std::string& categories,
461 const std::string& description
464 //TODO: implement method
468 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
470 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
474 result = new std::string("Couldn't open CMakeLists file.");
479 const bool modelCDMProject::Refresh(std::string*& result)
481 std::cout << "refreshing project" << std::endl;
482 //open makelists file
483 std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
485 std::ifstream confFile;
486 confFile.open((pathMakeLists).c_str());
489 while(confFile.is_open() && !confFile.eof())
491 //std::cout << "leyendo " << word << std::endl;
493 std::getline(confFile,word,'(');
494 std::vector<std::string> wordBits;
495 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
497 if(wordBits[wordBits.size()-1] == "PROJECT")
499 std::getline(confFile,word,')');
500 std::vector<std::string> nameBits;
501 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
503 this->name = this->nameProject = "";
504 for (int i = 0; i < (int)(nameBits.size()); i++)
508 this->name += nameBits[i];
510 this->nameProject = this->name;
515 if(wordBits[wordBits.size()-1] == "SET")
517 //get project version
518 std::getline(confFile,word,')');
519 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
520 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
522 version = wordBits[1];
524 if(wordBits[0] == "PROJECT_MINOR_VERSION")
526 version += "." + wordBits[1];
528 if(wordBits[0] == "PROJECT_BUILD_VERSION")
530 version += "." + wordBits[1];
533 //get project versionDate
534 if(wordBits[0] == "PROJECT_VERSION_DATE")
536 std::vector<std::string> versionBits;
537 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
538 versionDate = versionBits[0];
544 this->type = wxDIR_DIRS;
547 std::vector<bool> checked(this->children.size(), false);
548 std::vector<bool> checkedPackages(this->packages.size(), false);
551 wxDir dir(crea::std2wx((this->path).c_str()));
555 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
558 std::string stdfileName = crea::wx2std(fileName);
560 //if appli, create appli
561 if(stdfileName == "appli")
563 if (this->appli == NULL)
565 this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
566 this->children.push_back(this->appli);
570 int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
572 if(!this->appli->Refresh(result))
577 else if(stdfileName == "lib")
579 if (this->lib == NULL)
581 this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
582 this->children.push_back(this->lib);
586 int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
588 if(!this->lib->Refresh(result))
593 //if package , create package
594 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
597 for (int i = 0; !found && i < (int)(this->packages.size()); i++)
599 if (this->packages[i]->GetName() == stdfileName)
602 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
604 checkedPackages[i] = true;
605 if(!this->packages[i]->Refresh(result))
611 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
612 this->packages.push_back(package);
613 this->children.push_back(package);
617 //if is an unknown folder, create folder
621 for (int i = 0; !found && i < (int)(this->children.size()); i++)
623 if (this->children[i]->GetName() == stdfileName)
627 if(!this->children[i]->Refresh(result))
634 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
635 this->children.push_back(folder);
639 cont = dir.GetNext(&fileName);
642 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
645 std::string stdfileName = crea::wx2std(fileName);
646 std::size_t fileTypePos = stdfileName.find_last_of(".");
647 std::string fileType = stdfileName.substr(fileTypePos);
649 //if CMakeLists, create CMakeLists
650 if(stdfileName == "CMakeLists.txt")
652 if (this->CMakeLists == NULL)
654 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
655 this->children.push_back(this->CMakeLists);
659 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
661 if(!this->CMakeLists->Refresh(result))
665 //if is a code file, create modelCDMCodeFile
666 //if is an unknown file, create file
670 for (int i = 0; !found && i < (int)(this->children.size()); i++)
672 if (this->children[i]->GetName() == stdfileName)
676 if(!this->children[i]->Refresh(result))
685 fileType == ".cxx" ||
687 fileType == ".cpp" ||
688 fileType == ".txx" ||
689 fileType == ".cmake" )
691 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
695 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
696 this->children.push_back(file);
701 cont = dir.GetNext(&fileName);
705 for (int i = 0; i < (int)(checkedPackages.size()); i++)
707 if(!checkedPackages[i])
709 this->packages.erase(this->packages.begin()+i);
710 checkedPackages.erase(checkedPackages.begin()+i);
714 for (int i = 0; i < (int)(checked.size()); i++)
718 delete this->children[i];
719 this->children.erase(this->children.begin()+i);
720 checked.erase(checked.begin()+i);
725 this->SortChildren();
726 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
730 bool modelCDMProject::ConfigureBuild(std::string*& result)
732 //TODO: adjust for windows and mac
735 if(0 == system("cmake-gui"))
739 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.");
747 wxDir dir(crea::std2wx((this->buildPath).c_str()));
749 //if folder doesn't exist then create it
752 //create command line to create folder
753 std::string createComm = "mkdir \"" + this->buildPath + "\"";
754 //execute creation command
755 if (system(createComm.c_str()))
757 //if there was an error then report it
758 result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
762 //create command line to execute ccmake
763 //TODO:: adjust for different Linux distributions
764 std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
766 if(CDMUtilities::openTerminal(confComm))
768 //if there was an error then report it
769 result = new std::string("There was an error opening the configuration tool in the desired place.");
776 bool modelCDMProject::Build(std::string*& result, const std::string& line)
778 //TODO: adjust for windows and mac
781 std::string command = "start \"\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"";
782 //wxMessageBox(crea::std2wx(command), wxT("Project Compilation - Check!"));
783 if(0 == system(command.c_str()))
787 result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ installed.");
795 wxDir dir(crea::std2wx((this->buildPath).c_str()));
797 //if binary folder can't be opened then return false
800 result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
803 //create make command
804 std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
805 for (int i = 0; i < line.size(); i++)
809 makeComm+="\\\\\\\"";
811 else if(line[i] == '\\')
813 makeComm+="\\\\\\\\";
817 makeComm.push_back(line[i]);
820 makeComm += "; echo -e '\\a'; bash";
821 makeComm += "\\\"\"";
823 std::cout << "executing '" << makeComm << "'" << std::endl;
824 //execute make command
825 if(system(makeComm.c_str()))
827 //if there was an error then report it
828 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.");
835 bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
837 //TODO: adjust for mac
841 wxDir dir(crea::std2wx(folder));
843 //if binary folder can't be opened then return false
846 result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
849 //create plug command
850 std::string plugComm = "bbPlugPackage \"" + folder + "\"";
851 std::cout << "executing '" << plugComm << "'" << std::endl;
852 //execute plug command
853 if(system(std::string("start cmd.exe /k \"" + plugComm + "\"").c_str()))
855 //if there was an error then report it
856 result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
864 wxDir dir(crea::std2wx(folder));
866 //if binary folder can't be opened then return false
869 result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
872 //create plug command
873 std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\"";
875 std::string Comm = "gnome-terminal -e \"bash -c \\\"";
876 for (int i = 0; i < plugComm.size(); i++)
878 if(plugComm[i] == '"')
882 else if(plugComm[i] == '\\')
888 Comm.push_back(plugComm[i]);
891 Comm += "; echo -e '\\a'; bash";
895 std::cout << "executing '" << Comm << "'" << std::endl;
896 //execute plug command
897 if(system(Comm.c_str()))
899 //if there was an error then report it
900 result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
907 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
910 if(this->CMakeLists != NULL)
912 //set properties parameters based on model
913 properties["project add appli"] = this->appli != NULL;
914 properties["project add lib"] = this->lib != NULL;
915 for (int i = 0; i < (int)(this->packages.size()); i++)
916 properties["project add " + packages[i]->GetName()] = false;
919 std::ifstream confFile;
920 confFile.open((this->CMakeLists->GetPath()).c_str());
922 //take everything that is not commented
923 std::string fileContent;
926 std::vector<std::string> words;
927 while(confFile.is_open() && !confFile.eof())
929 std::getline(confFile,word, '\n');
932 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
933 if (words.size() > 0)
936 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
937 for (int i = 0; i < (int)(words.size()); i++)
939 if(words[i].substr(0,2) == "//")
941 fileContent += words[i] + " ";
948 //check every instruction
949 std::stringstream ss(fileContent);
952 std::getline(ss,word, '(');
954 //check instruction name
955 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
958 if (words.size() > 0 && words[words.size()-1] == "SET")
960 std::getline(ss,word, ')');
962 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
963 if (words.size() > 1)
965 if (words[0] == "USE_CREA" && words[1] == "ON")
967 properties["project set USE_CREA"] = true;
969 else if (words[0] == "USE_GDCM" && words[1] == "ON")
971 properties["project set USE_GDCM"] = true;
973 else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
975 properties["project set USE_GDCM_VTK"] = true;
977 else if (words[0] == "USE_GDCM2" && words[1] == "ON")
979 properties["project set USE_GDCM2"] = true;
981 else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
983 properties["project set USE_WXWIDGETS"] = true;
985 else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
987 properties["project set USE_KWWIDGETS"] = true;
989 else if (words[0] == "USE_VTK" && words[1] == "ON")
991 properties["project set USE_VTK"] = true;
993 else if (words[0] == "USE_ITK" && words[1] == "ON")
995 properties["project set USE_ITK"] = true;
997 else if (words[0] == "USE_BOOST" && words[1] == "ON")
999 properties["project set USE_BOOST"] = true;
1004 else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
1006 std::getline(ss,word, ')');
1007 //std::cout << word << std::endl;
1008 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
1010 if (words.size() > 0)
1012 if(words[0] == "appli")
1014 properties["project add "+words[0]] = this->appli != NULL;
1016 else if(words[0] == "lib")
1018 properties["project add "+words[0]] = this->lib != NULL;
1022 properties["project add "+words[0]] = true;
1030 //check appli's structure
1031 this->appli->CheckStructure(properties);
1032 //check lib's structure
1033 this->lib->CheckStructure(properties);
1034 //check packages' structure
1035 for (int i = 0; i < (int)(this->packages.size()); i++)
1037 properties["package " + this->packages[i]->GetName()] = true;
1038 this->packages[i]->CheckStructure(properties);
1042 bool modelCDMProject::IsPackageIncluded(const std::string& package_name)
1044 if (this->HasCMakeLists())
1046 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1047 for (int i = 0; i < cmlFile.size(); ++i)
1049 if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1052 while (pos < cmlFile[i].second.size())
1054 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1056 if (package_name == cmlFile[i].second[pos])
1069 bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude)
1071 if (this->HasCMakeLists())
1073 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1077 for (int i = 0; i < cmlFile.size(); ++i)
1079 if(toInclude && cmlFile[i].first == "comment")
1081 std::vector<std::string> segments;
1082 std::string line = cmlFile[i].second[0];
1083 while(line[0] == '#')
1086 CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
1087 if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name)
1090 while(cmlFile[i].second[0][0] == '#')
1091 cmlFile[i].second[0].erase(0,1);
1094 else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1097 while (pos < cmlFile[i].second.size())
1099 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1101 if (package_name == cmlFile[i].second[pos])
1106 cmlFile[i].first = "comment";
1107 cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
1108 while (cmlFile[i].second.size() > 1)
1110 cmlFile[i].second[0] += cmlFile[i].second[1];
1111 cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
1122 if (!found && toInclude)
1124 CDMUtilities::syntaxElement element;
1125 element.first = "command";
1126 element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")");
1127 cmlFile.push_back(element);
1130 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
1135 std::map<std::string, bool> modelCDMProject::Get3rdPartyLibraries()
1137 std::map<std::string, std::string> correspondence;
1138 correspondence["USE_CREA"] = "Crea";
1139 correspondence["USE_GDCM"] = "GDCM";
1140 correspondence["USE_GDCM_VTK"] = "GDCM_VTK";
1141 correspondence["USE_GDCM2"] = "GDCM2";
1142 correspondence["USE_WXWIDGETS"] = "WxWidgets";
1143 correspondence["USE_KWWIDGETS"] = "KWWidgets";
1144 correspondence["USE_VTK"] = "VTK";
1145 correspondence["USE_ITK"] = "ITK";
1146 correspondence["USE_BOOST"] = "Boost";
1148 std::map<std::string, bool> res;
1149 res["Crea"] = false;
1150 res["GDCM"] = false;
1151 res["GDCM_VTK"] = false;
1152 res["GDCM2"] = false;
1153 res["WxWidgets"] = false;
1154 res["KWWidgets"] = false;
1157 res["Boost"] = false;
1159 if (this->HasCMakeLists())
1161 std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1163 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*USE_\\w+\\s+ON");
1164 std::string::const_iterator start, end;
1165 start = CMfile.begin();
1167 boost::match_results<std::string::const_iterator> what;
1168 boost::match_flag_type flags = boost::match_default;
1169 while(boost::regex_search(start, end, what, expression, flags))
1171 //std::cout << what[0].str() << std::endl;
1172 boost::regex expression1 = boost::regex("USE_\\w+");
1173 std::string::const_iterator start1, end1;
1174 start1 = what[0].first;
1175 end1 = what[0].second;
1176 boost::match_results<std::string::const_iterator> what1;
1177 if(boost::regex_search(start1, end1, what1, expression1, flags))
1179 std::string dete = what1.str();
1180 CDMUtilities::normalizeStr(dete);
1181 //std::cout << dete << std::endl;
1182 if(correspondence.find(dete) != correspondence.end())
1183 res[correspondence[dete]] = true;
1185 start = what[0].second;
1192 bool modelCDMProject::Set3rdPartyLibrary(const std::string& library_name,
1193 const bool& toInclude)
1195 std::map<std::string, std::string> correspondence;
1196 correspondence["Crea"] = "USE_CREA";
1197 correspondence["GDCM"] = "USE_GDCM";
1198 correspondence["GDCM_VTK"] = "USE_GDCM_VTK";
1199 correspondence["GDCM2"] = "USE_GDCM2";
1200 correspondence["WxWidgets"] = "USE_WXWIDGETS";
1201 correspondence["KWWidgets"] = "USE_KWWIDGETS";
1202 correspondence["VTK"] = "USE_VTK";
1203 correspondence["ITK"] = "USE_ITK";
1204 correspondence["Boost"] = "USE_BOOST";
1206 if (correspondence.find(library_name) != correspondence.end())
1208 std::string library_command = correspondence[library_name];
1210 if (this->HasCMakeLists())
1212 std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1213 std::string resCMfile = "";
1215 std::string::const_iterator start, end;
1216 boost::match_results<std::string::const_iterator> what, tmp;
1217 boost::match_flag_type flags = boost::match_default;
1220 start = CMfile.begin();
1223 //search for existing inclusions
1224 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
1225 while(boost::regex_search(start, end, what, expression, flags))
1228 resCMfile += what.prefix().str();
1231 std::string dete = what.str();
1232 int pos = dete.find("ON",0);
1233 dete.replace(pos, 2, "OFF");
1237 resCMfile += what.str();
1239 start = what[0].second;
1243 resCMfile += tmp.suffix().str();
1246 start = CMfile.begin();
1249 //search for existing exclusions
1250 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+OFF\\s*\\)");
1251 while(boost::regex_search(start, end, what, expression, flags))
1254 resCMfile += what.prefix().str();
1257 std::string dete = what.str();
1258 int pos = dete.find("OFF",0);
1259 dete.replace(pos, 3, "ON");
1263 resCMfile += what.str();
1265 start = what[0].second;
1269 resCMfile += tmp.suffix().str();
1272 start = CMfile.begin();
1275 //search for existing commented inclusions
1276 expression = boost::regex("^\\h*#+\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
1277 while(boost::regex_search(start, end, what, expression, flags))
1280 resCMfile += what.prefix().str();
1283 std::string dete = what.str();
1284 for (int i = 0; i < dete.size(); ++i) {
1294 resCMfile += what.str();
1297 start = what[0].second;
1301 resCMfile += tmp.suffix().str();
1306 start = CMfile.begin();
1309 //search for position to insert
1310 expression = boost::regex("^\\h*#\\h*Libraries\\/tools used\\h*$");
1311 if(boost::regex_search(start, end, what, expression, flags))
1314 resCMfile += what.prefix().str();
1315 resCMfile += what.str();
1316 resCMfile += "\nSET(" + library_command + " ON)";
1317 resCMfile += what.suffix().str();
1331 return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);