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: Nov 23, 2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "modelCDMLib.h"
41 #include "CDMUtilities.h"
45 modelCDMLib::modelCDMLib()
49 modelCDMLib::modelCDMLib(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
51 std::cout << "creating lib\n";
52 this->parent = parent;
53 this->type = wxDIR_DIRS;
60 this->path = CDMUtilities::fixPath(path);
62 std::string pathFixed(CDMUtilities::fixPath(path));
64 this->libraries.clear();
65 wxDir dir(crea::std2wx((pathFixed).c_str()));
70 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
73 std::string stdfileName = crea::wx2std(fileName);
75 if(stdfileName != "template_lib")
77 modelCDMLibrary* library = new modelCDMLibrary(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
78 this->libraries.push_back(library);
79 this->children.push_back(library);
83 modelCDMFolder* folder = new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
84 this->children.push_back(folder);
87 cont = dir.GetNext(&fileName);
90 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
93 std::string stdfileName = crea::wx2std(fileName);
94 std::size_t fileTypePos = stdfileName.find_last_of(".");
95 std::string fileType = stdfileName.substr(fileTypePos);
97 //if CMakeLists, create CMakeLists
98 if(stdfileName == "CMakeLists.txt")
100 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
101 this->children.push_back(this->CMakeLists);
103 //if is a code file, create modelCDMCodeFile
106 fileType == ".cxx" ||
108 fileType == ".cpp" ||
109 fileType == ".txx" ||
110 fileType == ".cmake" )
112 this->children.push_back(new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
114 //if is an unknown file, create file
117 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
120 cont = dir.GetNext(&fileName);
123 this->SortChildren();
124 std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
127 modelCDMLib::~modelCDMLib()
131 const std::vector<modelCDMLibrary*>& modelCDMLib::GetLibraries() const
133 return this->libraries;
136 modelCDMLibrary* modelCDMLib::CreateLibrary(
137 const std::string& namein,
141 std::vector<std::string> words;
142 CDMUtilities::splitter::split(words,namein," '/\\*\"%",CDMUtilities::splitter::no_empties);
144 for (int i = 0; i < (int)(words.size()); i++)
150 result = new std::string("The given name is not valid: '/\\*\"% are forbidden.");
153 //copy template library folder with new name
155 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_lib\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
157 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_lib\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
159 if(system(copyCommand.c_str()))
161 result = new std::string("An error occurred while running '" + copyCommand + "'.");
164 //set name of library in CMakeLists inside copied folder
166 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
169 result = new std::string("CMakeLists.txt file failed to open.");
172 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
173 while (getline(in, line))
175 if(line == "SET ( LIBRARY_NAME MyLib )")
176 line = "SET ( LIBRARY_NAME " + name + " )";
177 out << line << std::endl;
181 //delete old file and rename new file
183 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
185 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
187 if(system(renameCommand.c_str()))
189 result = new std::string("An error occurred while running '" + renameCommand + "'.");
193 //add library to lib CMakeLists
194 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
197 out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
202 //add library to model
203 modelCDMLibrary* library = new modelCDMLibrary(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
204 this->libraries.push_back(library);
205 this->children.push_back(library);
207 this->SortChildren();
209 result = new std::string(this->path + CDMUtilities::SLASH + name);
213 const bool modelCDMLib::Refresh(std::string*& result)
215 std::cout << "refreshing lib" << std::endl;
216 this->type = wxDIR_DIRS;
220 std::vector<bool> checked(this->children.size(), false);
221 std::vector<bool> checkedLibraries(this->libraries.size(), false);
224 wxDir dir(crea::std2wx((this->path).c_str()));
228 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
231 std::string stdfileName = crea::wx2std(fileName);
233 if(stdfileName != "template_lib")
235 std::string libraryName = stdfileName;
236 //check if library already exist
238 for (int i = 0; !found && i < (int)(this->libraries.size()); i++)
240 if (this->libraries[i]->GetName() == libraryName)
243 int pos = std::find(this->children.begin(), this->children.end(), this->libraries[i]) - this->children.begin();
245 checkedLibraries[i] = true;
246 if(!this->libraries[i]->Refresh(result))
252 modelCDMLibrary* library = new modelCDMLibrary(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
253 this->libraries.push_back(library);
254 this->children.push_back(library);
259 //check if folder already exist
261 for (int i = 0; !found && i < (int)(this->children.size()); i++)
263 if (this->children[i]->GetName() == stdfileName)
267 if(!this->children[i]->Refresh(result))
273 modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
274 this->children.push_back(folder);
277 cont = dir.GetNext(&fileName);
280 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
283 std::string stdfileName = crea::wx2std(fileName);
284 std::size_t fileTypePos = stdfileName.find_last_of(".");
285 std::string fileType = stdfileName.substr(fileTypePos);
288 //if CMakeLists, create CMakeLists
289 if(stdfileName == "CMakeLists.txt")
291 if (this->CMakeLists == NULL)
293 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
294 this->children.push_back(this->CMakeLists);
298 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
300 if(!this->CMakeLists->Refresh(result))
304 //if is an unknown file, create file
308 for (int i = 0; !found && i < (int)(this->children.size()); i++)
310 if (this->children[i]->GetName() == stdfileName)
314 if(!this->children[i]->Refresh(result))
321 //if is a code file, create modelCDMCodeFile
324 fileType == ".cxx" ||
326 fileType == ".cpp" ||
327 fileType == ".txx" ||
328 fileType == ".cmake" )
330 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
334 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
335 this->children.push_back(file);
340 cont = dir.GetNext(&fileName);
344 for (int i = 0; i < (int)(checkedLibraries.size()); i++)
346 if(!checkedLibraries[i])
348 this->libraries.erase(this->libraries.begin()+i);
349 checkedLibraries.erase(checkedLibraries.begin()+i);
353 for (int i = 0; i < (int)(checked.size()); i++)
357 delete this->children[i];
358 this->children.erase(this->children.begin()+i);
359 checked.erase(checked.begin()+i);
363 this->SortChildren();
364 std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
368 void modelCDMLib::CheckStructure(std::map<std::string, bool>& properties)
371 if(this->CMakeLists != NULL)
373 //set properties parameters based on model
374 for (int i = 0; i < (int)(this->libraries.size()); i++)
375 properties["lib add " + libraries[i]->GetName()] = false;
378 std::ifstream confFile;
379 confFile.open((this->CMakeLists->GetPath()).c_str());
381 //take everything that is not commented
382 std::string fileContent;
385 std::vector<std::string> words;
386 while(confFile.is_open() && !confFile.eof())
388 std::getline(confFile,word, '\n');
391 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
392 if (words.size() > 0)
395 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
396 for (int i = 0; i < (int)(words.size()); i++)
398 if(words[i].substr(0,2) == "//")
400 fileContent += words[i] + " ";
406 //check every instruction
407 std::stringstream ss(fileContent);
410 std::getline(ss,word, '(');
412 //check instruction name
413 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
416 if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
418 std::getline(ss,word, ')');
419 //std::cout << word << std::endl;
420 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
422 if (words.size() > 0)
425 properties["lib add " + words[0]] = true;
433 //check libraries' structure
434 for (int i = 0; i < (int)(this->libraries.size()); i++)
436 properties["library " + this->libraries[i]->GetName()] = true;
437 this->libraries[i]->CheckStructure(properties);
441 bool modelCDMLib::IsLibraryIncluded(const std::string& library_name)
443 if (this->HasCMakeLists())
445 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
446 for (int i = 0; i < cmlFile.size(); ++i)
448 if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
451 while (pos < cmlFile[i].second.size())
453 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
455 if (library_name == cmlFile[i].second[pos])
467 bool modelCDMLib::SetLibraryInclude(const std::string& library_name, const bool& toInclude)
469 if (this->HasCMakeLists())
471 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
475 for (int i = 0; i < cmlFile.size(); ++i)
477 if(toInclude && cmlFile[i].first == "comment")
479 std::vector<std::string> segments;
480 std::string line = cmlFile[i].second[0];
481 while(line[0] == '#')
484 CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
485 if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == library_name)
488 while(cmlFile[i].second[0][0] == '#')
489 cmlFile[i].second[0].erase(0,1);
492 else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
495 while (pos < cmlFile[i].second.size())
497 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
499 if (library_name == cmlFile[i].second[pos])
504 cmlFile[i].first = "comment";
505 cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
506 while (cmlFile[i].second.size() > 1)
508 cmlFile[i].second[0] += cmlFile[i].second[1];
509 cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
520 if (!found && toInclude)
522 CDMUtilities::syntaxElement element;
523 element.first = "command";
524 element.second.push_back("ADD_SUBDIRECTORY(" + library_name + ")");
525 cmlFile.push_back(element);
528 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);