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"
48 #include <wx/config.h>
50 modelCDMProject::modelCDMProject()
52 std::cout << "in constructor1" << std::endl;
55 this->CMakeLists = NULL;
58 modelCDMProject::modelCDMProject(
59 modelCDMIProjectTreeNode* parent,
60 const std::string& path,
61 const std::string& name,
62 const std::string& buildPath
65 std::cout << "creating project: " + name + " in " + path + "\n";
66 this->parent = parent;
67 this->path = CDMUtilities::fixPath(path);
69 std::string pathFixed(CDMUtilities::fixPath(path));
71 std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
73 std::ifstream confFile;
74 confFile.open((pathMakeLists).c_str());
77 while(confFile.is_open() && !confFile.eof())
79 //std::cout << "leyendo " << word << std::endl;
81 std::getline(confFile,word,'(');
82 std::vector<std::string> wordBits;
83 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
85 if(wordBits[wordBits.size()-1] == "PROJECT")
87 std::getline(confFile,word,')');
88 std::vector<std::string> nameBits;
89 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
91 this->name = this->nameProject = "";
92 for (int i = 0; i < (int)(nameBits.size()); i++)
96 this->name += nameBits[i];
98 this->nameProject = this->name;
103 if(wordBits[wordBits.size()-1] == "SET")
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")
110 version = wordBits[1];
112 if(wordBits[0] == "PROJECT_MINOR_VERSION")
114 version += "." + wordBits[1];
116 if(wordBits[0] == "PROJECT_BUILD_VERSION")
118 version += "." + wordBits[1];
121 //get project versionDate
122 if(wordBits[0] == "PROJECT_VERSION_DATE")
124 std::vector<std::string> versionBits;
125 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
126 versionDate = versionBits[0];
128 //get project buildPath
132 this->buildPath = buildPath;
136 this->buildPath = this->path + "Bin";
142 this->type = wxDIR_DIRS;
145 this->children.clear();
148 this->packages.clear();
152 wxDir dir(crea::std2wx((pathFixed).c_str()));
156 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
159 std::string stdfileName = crea::wx2std(fileName);
161 //if appli, create appli
162 if(stdfileName == "appli")
164 this->appli = new modelCDMAppli(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
165 this->children.push_back(this->appli);
168 else if(stdfileName == "lib")
170 this->lib = new modelCDMLib(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
171 this->children.push_back(this->lib);
173 //if package , create package
174 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
176 modelCDMPackage* package = new modelCDMPackage(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
177 this->packages.push_back(package);
178 this->children.push_back(package);
180 //if is an unknown folder, create folder
183 this->children.push_back(new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
186 cont = dir.GetNext(&fileName);
189 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
192 std::string stdfileName = crea::wx2std(fileName);
193 std::size_t fileTypePos = stdfileName.find_last_of(".");
194 std::string fileType = stdfileName.substr(fileTypePos);
196 //if CMakeLists, create CMakeLists
197 if(stdfileName == "CMakeLists.txt")
199 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
200 this->children.push_back(this->CMakeLists);
202 //if is a code file, create modelCDMCodeFile
205 fileType == ".cxx" ||
207 fileType == ".cpp" ||
208 fileType == ".txx" ||
209 fileType == ".cmake" )
211 this->children.push_back(new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
213 //if is an unknown file, create file
216 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
219 cont = dir.GetNext(&fileName);
223 this->SortChildren();
224 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
228 modelCDMProject::~modelCDMProject()
232 const std::string& modelCDMProject::GetNameProject() const
234 return this->nameProject;
237 const std::string& modelCDMProject::GetVersion() const
239 return this->version;
242 const std::string& modelCDMProject::GetVersionDate() const
244 return this->versionDate;
247 const std::string& modelCDMProject::GetBuildPath() const
249 return this->buildPath;
252 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
254 return this->packages;
257 modelCDMAppli* modelCDMProject::GetAppli() const
262 modelCDMLib* modelCDMProject::GetLib() const
267 std::string modelCDMProject::GetBuildInstruction() const
269 wxConfigBase* pConfig = wxConfigBase::Get();
270 std::string commandline = crea::wx2std(pConfig->Read(wxT("BUILD_COMMAND"), crea::std2wx(CDMUtilities::BUILD_COMMAND)));
273 std::string makeComm = "cd \"" + this->buildPath + "\";" + commandline;// -C \"" + this->buildPath + "\""; /*> \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";*/
277 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
280 std::vector<std::string> vers;
281 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
283 time_t now = time(0);
287 localtime_s(<m, &now);
289 ltm = *(localtime(&now));
292 std::stringstream date;
293 date << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year;
295 //set name of library in CMakeLists inside copied folder
297 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
300 result = new std::string("CMakeLists.txt file failed to open.");
303 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
306 result = new std::string("CMakeLists.txt.tmp file failed to open.");
309 while (getline(in, line))
311 if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
312 line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
313 else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
314 line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
315 else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
316 line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
317 else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
318 line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
319 out << line << std::endl;
323 //delete old file and rename new file
325 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
327 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
329 if(system(renameCommand.c_str()))
331 result = new std::string("An error occurred while running '" + renameCommand + "'.");
335 this->version = vers[0] + "." + vers[1] + "." + vers[2];
336 this->versionDate = date.str();
340 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
344 result = new std::string("The path cannot be empty");
347 if(path == this->path)
349 result = new std::string("The path cannot be same as the project sources");
352 this->buildPath = path;
356 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
357 const std::string& name,
358 std::string*& result,
359 const std::string& authors,
360 const std::string& authorsEmail,
361 const std::string& description,
362 const std::string& version
365 //fixing input parameters
366 std::vector<std::string> words;
368 CDMUtilities::splitter::split(words,name," '/\"\\,.",CDMUtilities::splitter::no_empties);
369 std::string nameFixed = "";
370 for (int i = 0; i < (int)(words.size()); i++)
372 nameFixed += words[i];
376 CDMUtilities::splitter::split(words,authors," '/\"\\,.",CDMUtilities::splitter::no_empties);
377 std::string authorFixed;
378 for (int i = 0; i < (int)(words.size()); i++)
380 authorFixed += words[i];
384 std::string descriptionFixed;
385 CDMUtilities::splitter::split(words,authorsEmail," '/\"\\,",CDMUtilities::splitter::no_empties);
386 for (int i = 0; i < (int)(words.size()); i++)
388 descriptionFixed += words[i] + "/";
391 CDMUtilities::splitter::split(words,description," '\"",CDMUtilities::splitter::no_empties);
392 for (int i = 0; i < (int)(words.size()); i++)
394 descriptionFixed += "_" + words[i];
397 //call project to create package : use bbCreatePackage <path> <name> [author] [description]
398 std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
399 //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
400 bool resultCommand = 0 != system(creationCommand.c_str());
402 resultCommand = false;
406 result = new std::string("An error occurred while running '" + creationCommand + "'.");
410 //add library to project CMakeLists
411 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
414 out1 << "ADD_SUBDIRECTORY(bbtk_" << nameFixed << "_PKG)" << std::endl;
418 //add library to model
419 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
420 this->packages.push_back(package);
421 this->children.push_back(package);
423 //TODO: set package version
425 this->SortChildren();
427 result = new std::string(this->path + CDMUtilities::SLASH + name);
431 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
432 const std::string& name,
433 std::string*& result,
434 const std::string& path
437 if(this->lib != NULL)
439 return this->lib->CreateLibrary(name, result);
441 result = new std::string("there is no lib folder in this project.");
445 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
446 const std::string& name,
448 std::string*& result,
449 const std::string& path
452 if(this->appli != NULL)
454 return this->appli->CreateApplication(name, type, result);
456 result = new std::string("there is no appli folder in this project.");
460 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
461 const std::string& name,
462 const std::string& package,
463 const std::string& authors,
464 const std::string& authorsEmail,
465 const std::string& categories,
466 const std::string& description
469 //TODO: implement method
473 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
475 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
479 result = new std::string("Couldn't open CMakeLists file.");
484 const bool modelCDMProject::Refresh(std::string*& result)
486 std::cout << "refreshing project" << std::endl;
487 //open makelists file
488 std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
490 std::ifstream confFile;
491 confFile.open((pathMakeLists).c_str());
494 while(confFile.is_open() && !confFile.eof())
496 //std::cout << "leyendo " << word << std::endl;
498 std::getline(confFile,word,'(');
499 std::vector<std::string> wordBits;
500 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
502 if(wordBits[wordBits.size()-1] == "PROJECT")
504 std::getline(confFile,word,')');
505 std::vector<std::string> nameBits;
506 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
508 this->name = this->nameProject = "";
509 for (int i = 0; i < (int)(nameBits.size()); i++)
513 this->name += nameBits[i];
515 this->nameProject = this->name;
520 if(wordBits[wordBits.size()-1] == "SET")
522 //get project version
523 std::getline(confFile,word,')');
524 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
525 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
527 version = wordBits[1];
529 if(wordBits[0] == "PROJECT_MINOR_VERSION")
531 version += "." + wordBits[1];
533 if(wordBits[0] == "PROJECT_BUILD_VERSION")
535 version += "." + wordBits[1];
538 //get project versionDate
539 if(wordBits[0] == "PROJECT_VERSION_DATE")
541 std::vector<std::string> versionBits;
542 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
543 versionDate = versionBits[0];
549 this->type = wxDIR_DIRS;
552 std::vector<bool> checked(this->children.size(), false);
553 std::vector<bool> checkedPackages(this->packages.size(), false);
556 wxDir dir(crea::std2wx((this->path).c_str()));
560 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
563 std::string stdfileName = crea::wx2std(fileName);
565 //if appli, create appli
566 if(stdfileName == "appli")
568 if (this->appli == NULL)
570 this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
571 this->children.push_back(this->appli);
575 int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
577 if(!this->appli->Refresh(result))
582 else if(stdfileName == "lib")
584 if (this->lib == NULL)
586 this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
587 this->children.push_back(this->lib);
591 int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
593 if(!this->lib->Refresh(result))
598 //if package , create package
599 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
602 for (int i = 0; !found && i < (int)(this->packages.size()); i++)
604 if (this->packages[i]->GetName() == stdfileName)
607 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
609 checkedPackages[i] = true;
610 if(!this->packages[i]->Refresh(result))
616 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
617 this->packages.push_back(package);
618 this->children.push_back(package);
622 //if is an unknown folder, create folder
626 for (int i = 0; !found && i < (int)(this->children.size()); i++)
628 if (this->children[i]->GetName() == stdfileName)
632 if(!this->children[i]->Refresh(result))
639 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
640 this->children.push_back(folder);
644 cont = dir.GetNext(&fileName);
647 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
650 std::string stdfileName = crea::wx2std(fileName);
651 std::size_t fileTypePos = stdfileName.find_last_of(".");
652 std::string fileType = stdfileName.substr(fileTypePos);
654 //if CMakeLists, create CMakeLists
655 if(stdfileName == "CMakeLists.txt")
657 if (this->CMakeLists == NULL)
659 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
660 this->children.push_back(this->CMakeLists);
664 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
666 if(!this->CMakeLists->Refresh(result))
670 //if is a code file, create modelCDMCodeFile
671 //if is an unknown file, create file
675 for (int i = 0; !found && i < (int)(this->children.size()); i++)
677 if (this->children[i]->GetName() == stdfileName)
681 if(!this->children[i]->Refresh(result))
690 fileType == ".cxx" ||
692 fileType == ".cpp" ||
693 fileType == ".txx" ||
694 fileType == ".cmake" )
696 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
700 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
701 this->children.push_back(file);
706 cont = dir.GetNext(&fileName);
710 for (int i = 0; i < (int)(checkedPackages.size()); i++)
712 if(!checkedPackages[i])
714 this->packages.erase(this->packages.begin()+i);
715 checkedPackages.erase(checkedPackages.begin()+i);
719 for (int i = 0; i < (int)(checked.size()); i++)
723 delete this->children[i];
724 this->children.erase(this->children.begin()+i);
725 checked.erase(checked.begin()+i);
730 this->SortChildren();
731 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
735 bool modelCDMProject::ConfigureBuild(std::string*& result)
737 //TODO: adjust for windows and mac
740 if(0 == system("cmake-gui"))
744 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.");
752 wxDir dir(crea::std2wx((this->buildPath).c_str()));
754 //if folder doesn't exist then create it
757 //create command line to create folder
758 std::string createComm = "mkdir \"" + this->buildPath + "\"";
759 //execute creation command
760 if (system(createComm.c_str()))
762 //if there was an error then report it
763 result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
767 //create command line to execute ccmake
768 //TODO:: adjust for different Linux distributions
769 std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
771 if(CDMUtilities::openTerminal(confComm))
773 //if there was an error then report it
774 result = new std::string("There was an error opening the configuration tool in the desired place.");
781 bool modelCDMProject::Build(std::string*& result, const std::string& line)
783 //TODO: adjust for windows and mac
786 std::string command = "start \"\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"";
787 //wxMessageBox(crea::std2wx(command), wxT("Project Compilation - Check!"));
788 if(0 == system(command.c_str()))
792 result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ installed.");
800 wxDir dir(crea::std2wx((this->buildPath).c_str()));
802 //if binary folder can't be opened then return false
805 result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
808 //create make command
809 std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
810 for (int i = 0; i < line.size(); i++)
814 makeComm+="\\\\\\\"";
816 else if(line[i] == '\\')
818 makeComm+="\\\\\\\\";
822 makeComm.push_back(line[i]);
825 makeComm += "; echo -e '\\a'; bash";
826 makeComm += "\\\"\"";
828 std::cout << "executing '" << makeComm << "'" << std::endl;
829 //execute make command
830 if(system(makeComm.c_str()))
832 //if there was an error then report it
833 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.");
840 bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
842 //TODO: adjust for mac
846 wxDir dir(crea::std2wx(folder));
848 //if binary folder can't be opened then return false
851 result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
854 //create plug command
855 std::string plugComm = "bbPlugPackage \"" + folder + "\"";
856 std::cout << "executing '" << plugComm << "'" << std::endl;
857 //execute plug command
858 if(system(std::string("start cmd.exe /k \"" + plugComm + "\"").c_str()))
860 //if there was an error then report it
861 result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
869 wxDir dir(crea::std2wx(folder));
871 //if binary folder can't be opened then return false
874 result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
877 //create plug command
878 std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\"";
880 std::string Comm = "gnome-terminal -e \"bash -c \\\"";
881 for (int i = 0; i < plugComm.size(); i++)
883 if(plugComm[i] == '"')
887 else if(plugComm[i] == '\\')
893 Comm.push_back(plugComm[i]);
896 Comm += "; echo -e '\\a'; bash";
900 std::cout << "executing '" << Comm << "'" << std::endl;
901 //execute plug command
902 if(system(Comm.c_str()))
904 //if there was an error then report it
905 result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
912 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
915 if(this->CMakeLists != NULL)
917 //set properties parameters based on model
918 properties["project add appli"] = this->appli != NULL;
919 properties["project add lib"] = this->lib != NULL;
920 for (int i = 0; i < (int)(this->packages.size()); i++)
921 properties["project add " + packages[i]->GetName()] = false;
924 std::ifstream confFile;
925 confFile.open((this->CMakeLists->GetPath()).c_str());
927 //take everything that is not commented
928 std::string fileContent;
931 std::vector<std::string> words;
932 while(confFile.is_open() && !confFile.eof())
934 std::getline(confFile,word, '\n');
937 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
938 if (words.size() > 0)
941 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
942 for (int i = 0; i < (int)(words.size()); i++)
944 if(words[i].substr(0,2) == "//")
946 fileContent += words[i] + " ";
953 //check every instruction
954 std::stringstream ss(fileContent);
957 std::getline(ss,word, '(');
959 //check instruction name
960 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
963 if (words.size() > 0 && words[words.size()-1] == "SET")
965 std::getline(ss,word, ')');
967 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
968 if (words.size() > 1)
970 if (words[0] == "USE_CREA" && words[1] == "ON")
972 properties["project set USE_CREA"] = true;
974 else if (words[0] == "USE_GDCM" && words[1] == "ON")
976 properties["project set USE_GDCM"] = true;
978 else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
980 properties["project set USE_GDCM_VTK"] = true;
982 else if (words[0] == "USE_GDCM2" && words[1] == "ON")
984 properties["project set USE_GDCM2"] = true;
986 else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
988 properties["project set USE_WXWIDGETS"] = true;
990 else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
992 properties["project set USE_KWWIDGETS"] = true;
994 else if (words[0] == "USE_VTK" && words[1] == "ON")
996 properties["project set USE_VTK"] = true;
998 else if (words[0] == "USE_ITK" && words[1] == "ON")
1000 properties["project set USE_ITK"] = true;
1002 else if (words[0] == "USE_BOOST" && words[1] == "ON")
1004 properties["project set USE_BOOST"] = true;
1009 else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
1011 std::getline(ss,word, ')');
1012 //std::cout << word << std::endl;
1013 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
1015 if (words.size() > 0)
1017 if(words[0] == "appli")
1019 properties["project add "+words[0]] = this->appli != NULL;
1021 else if(words[0] == "lib")
1023 properties["project add "+words[0]] = this->lib != NULL;
1027 properties["project add "+words[0]] = true;
1035 //check appli's structure
1036 this->appli->CheckStructure(properties);
1037 //check lib's structure
1038 this->lib->CheckStructure(properties);
1039 //check packages' structure
1040 for (int i = 0; i < (int)(this->packages.size()); i++)
1042 properties["package " + this->packages[i]->GetName()] = true;
1043 this->packages[i]->CheckStructure(properties);
1047 bool modelCDMProject::IsPackageIncluded(const std::string& package_name)
1049 if (this->HasCMakeLists())
1051 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1052 for (int i = 0; i < cmlFile.size(); ++i)
1054 if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1057 while (pos < cmlFile[i].second.size())
1059 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1061 if (package_name == cmlFile[i].second[pos])
1074 bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude)
1076 if (this->HasCMakeLists())
1078 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1082 for (int i = 0; i < cmlFile.size(); ++i)
1084 if(toInclude && cmlFile[i].first == "comment")
1086 std::vector<std::string> segments;
1087 std::string line = cmlFile[i].second[0];
1088 while(line[0] == '#')
1091 CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
1092 if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name)
1095 while(cmlFile[i].second[0][0] == '#')
1096 cmlFile[i].second[0].erase(0,1);
1099 else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1102 while (pos < cmlFile[i].second.size())
1104 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1106 if (package_name == cmlFile[i].second[pos])
1111 cmlFile[i].first = "comment";
1112 cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
1113 while (cmlFile[i].second.size() > 1)
1115 cmlFile[i].second[0] += cmlFile[i].second[1];
1116 cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
1127 if (!found && toInclude)
1129 CDMUtilities::syntaxElement element;
1130 element.first = "command";
1131 element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")");
1132 cmlFile.push_back(element);
1135 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
1140 std::map<std::string, bool> modelCDMProject::Get3rdPartyLibraries()
1142 std::map<std::string, std::string> correspondence;
1143 correspondence["USE_CREA"] = "Crea";
1144 correspondence["USE_GDCM"] = "GDCM";
1145 correspondence["USE_GDCM_VTK"] = "GDCM_VTK";
1146 correspondence["USE_GDCM2"] = "GDCM2";
1147 correspondence["USE_WXWIDGETS"] = "WxWidgets";
1148 correspondence["USE_KWWIDGETS"] = "KWWidgets";
1149 correspondence["USE_VTK"] = "VTK";
1150 correspondence["USE_ITK"] = "ITK";
1151 correspondence["USE_BOOST"] = "Boost";
1153 std::map<std::string, bool> res;
1154 res["Crea"] = false;
1155 res["GDCM"] = false;
1156 res["GDCM_VTK"] = false;
1157 res["GDCM2"] = false;
1158 res["WxWidgets"] = false;
1159 res["KWWidgets"] = false;
1162 res["Boost"] = false;
1164 if (this->HasCMakeLists())
1166 std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1168 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*USE_\\w+\\s+ON");
1169 std::string::const_iterator start, end;
1170 start = CMfile.begin();
1172 boost::match_results<std::string::const_iterator> what;
1173 boost::match_flag_type flags = boost::match_default;
1174 while(boost::regex_search(start, end, what, expression, flags))
1176 //std::cout << what[0].str() << std::endl;
1177 boost::regex expression1 = boost::regex("USE_\\w+");
1178 std::string::const_iterator start1, end1;
1179 start1 = what[0].first;
1180 end1 = what[0].second;
1181 boost::match_results<std::string::const_iterator> what1;
1182 if(boost::regex_search(start1, end1, what1, expression1, flags))
1184 std::string dete = what1.str();
1185 CDMUtilities::normalizeStr(dete);
1186 //std::cout << dete << std::endl;
1187 if(correspondence.find(dete) != correspondence.end())
1188 res[correspondence[dete]] = true;
1190 start = what[0].second;
1197 bool modelCDMProject::Set3rdPartyLibrary(const std::string& library_name,
1198 const bool& toInclude)
1200 std::map<std::string, std::string> correspondence;
1201 correspondence["Crea"] = "USE_CREA";
1202 correspondence["GDCM"] = "USE_GDCM";
1203 correspondence["GDCM_VTK"] = "USE_GDCM_VTK";
1204 correspondence["GDCM2"] = "USE_GDCM2";
1205 correspondence["WxWidgets"] = "USE_WXWIDGETS";
1206 correspondence["KWWidgets"] = "USE_KWWIDGETS";
1207 correspondence["VTK"] = "USE_VTK";
1208 correspondence["ITK"] = "USE_ITK";
1209 correspondence["Boost"] = "USE_BOOST";
1211 if (correspondence.find(library_name) != correspondence.end())
1213 std::string library_command = correspondence[library_name];
1215 if (this->HasCMakeLists())
1217 std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1218 std::string resCMfile = "";
1220 std::string::const_iterator start, end;
1221 boost::match_results<std::string::const_iterator> what, tmp;
1222 boost::match_flag_type flags = boost::match_default;
1225 start = CMfile.begin();
1228 //search for existing inclusions
1229 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
1230 while(boost::regex_search(start, end, what, expression, flags))
1233 resCMfile += what.prefix().str();
1236 std::string dete = what.str();
1237 int pos = dete.find("ON",0);
1238 dete.replace(pos, 2, "OFF");
1242 resCMfile += what.str();
1244 start = what[0].second;
1248 resCMfile += tmp.suffix().str();
1251 start = CMfile.begin();
1254 //search for existing exclusions
1255 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+OFF\\s*\\)");
1256 while(boost::regex_search(start, end, what, expression, flags))
1259 resCMfile += what.prefix().str();
1262 std::string dete = what.str();
1263 int pos = dete.find("OFF",0);
1264 dete.replace(pos, 3, "ON");
1268 resCMfile += what.str();
1270 start = what[0].second;
1274 resCMfile += tmp.suffix().str();
1277 start = CMfile.begin();
1280 //search for existing commented inclusions
1281 expression = boost::regex("^\\h*#+\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
1282 while(boost::regex_search(start, end, what, expression, flags))
1285 resCMfile += what.prefix().str();
1288 std::string dete = what.str();
1289 for (int i = 0; i < dete.size(); ++i) {
1299 resCMfile += what.str();
1302 start = what[0].second;
1306 resCMfile += tmp.suffix().str();
1311 start = CMfile.begin();
1314 //search for position to insert
1315 expression = boost::regex("^\\h*#\\h*Libraries\\/tools used\\h*$");
1316 if(boost::regex_search(start, end, what, expression, flags))
1319 resCMfile += what.prefix().str();
1320 resCMfile += what.str();
1321 resCMfile += "\nSET(" + library_command + " ON)";
1322 resCMfile += what.suffix().str();
1336 return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);