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);
95 //if CMakeLists, create CMakeLists
96 if(stdfileName == "CMakeLists.txt")
98 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
99 this->children.push_back(this->CMakeLists);
101 //if is an unknown file, create file
104 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
107 cont = dir.GetNext(&fileName);
110 this->SortChildren();
111 std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
114 modelCDMLib::~modelCDMLib()
118 const std::vector<modelCDMLibrary*>& modelCDMLib::GetLibraries() const
120 return this->libraries;
123 modelCDMLibrary* modelCDMLib::CreateLibrary(
124 const std::string& namein,
128 std::vector<std::string> words;
129 CDMUtilities::splitter::split(words,namein," '/\\*\"%",CDMUtilities::splitter::no_empties);
131 for (int i = 0; i < (int)(words.size()); i++)
137 result = new std::string("The given name is not valid: '/\\*\"% are forbidden.");
140 //copy template library folder with new name
142 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_lib\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
144 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_lib\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
146 if(system(copyCommand.c_str()))
148 result = new std::string("An error occurred while running '" + copyCommand + "'.");
151 //set name of library in CMakeLists inside copied folder
153 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
156 result = new std::string("CMakeLists.txt file failed to open.");
159 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
160 while (getline(in, line))
162 if(line == "SET ( LIBRARY_NAME MyLib )")
163 line = "SET ( LIBRARY_NAME " + name + " )";
164 out << line << std::endl;
168 //delete old file and rename new file
170 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
172 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
174 if(system(renameCommand.c_str()))
176 result = new std::string("An error occurred while running '" + renameCommand + "'.");
180 //add library to lib CMakeLists
181 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
184 out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
189 //add library to model
190 modelCDMLibrary* library = new modelCDMLibrary(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
191 this->libraries.push_back(library);
192 this->children.push_back(library);
194 this->SortChildren();
196 result = new std::string(this->path + CDMUtilities::SLASH + name);
200 const bool modelCDMLib::Refresh(std::string*& result)
202 std::cout << "refreshing lib" << std::endl;
203 this->type = wxDIR_DIRS;
207 std::vector<bool> checked(this->children.size(), false);
208 std::vector<bool> checkedLibraries(this->libraries.size(), false);
211 wxDir dir(crea::std2wx((this->path).c_str()));
215 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
218 std::string stdfileName = crea::wx2std(fileName);
220 if(stdfileName != "template_lib")
222 std::string libraryName = stdfileName;
223 //check if library already exist
225 for (int i = 0; !found && i < (int)(this->libraries.size()); i++)
227 if (this->libraries[i]->GetName() == libraryName)
230 int pos = std::find(this->children.begin(), this->children.end(), this->libraries[i]) - this->children.begin();
232 checkedLibraries[i] = true;
233 if(!this->libraries[i]->Refresh(result))
239 modelCDMLibrary* library = new modelCDMLibrary(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
240 this->libraries.push_back(library);
241 this->children.push_back(library);
246 //check if folder already exist
248 for (int i = 0; !found && i < (int)(this->children.size()); i++)
250 if (this->children[i]->GetName() == stdfileName)
254 if(!this->children[i]->Refresh(result))
260 modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
261 this->children.push_back(folder);
264 cont = dir.GetNext(&fileName);
267 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
270 std::string stdfileName = crea::wx2std(fileName);
272 //if CMakeLists, create CMakeLists
273 if(stdfileName == "CMakeLists.txt")
275 if (this->CMakeLists == NULL)
277 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
278 this->children.push_back(this->CMakeLists);
282 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
284 if(!this->CMakeLists->Refresh(result))
288 //if is an unknown file, create file
292 for (int i = 0; !found && i < (int)(this->children.size()); i++)
294 if (this->children[i]->GetName() == stdfileName)
298 if(!this->children[i]->Refresh(result))
305 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
306 this->children.push_back(file);
310 cont = dir.GetNext(&fileName);
314 for (int i = 0; i < (int)(checkedLibraries.size()); i++)
316 if(!checkedLibraries[i])
318 this->libraries.erase(this->libraries.begin()+i);
319 checkedLibraries.erase(checkedLibraries.begin()+i);
323 for (int i = 0; i < (int)(checked.size()); i++)
327 delete this->children[i];
328 this->children.erase(this->children.begin()+i);
329 checked.erase(checked.begin()+i);
333 this->SortChildren();
334 std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
338 void modelCDMLib::CheckStructure(std::map<std::string, bool>& properties)
341 if(this->CMakeLists != NULL)
343 //set properties parameters based on model
344 for (int i = 0; i < (int)(this->libraries.size()); i++)
345 properties["lib add " + libraries[i]->GetName()] = false;
348 std::ifstream confFile;
349 confFile.open((this->CMakeLists->GetPath()).c_str());
351 //take everything that is not commented
352 std::string fileContent;
355 std::vector<std::string> words;
356 while(confFile.is_open() && !confFile.eof())
358 std::getline(confFile,word, '\n');
361 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
362 if (words.size() > 0)
365 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
366 for (int i = 0; i < (int)(words.size()); i++)
368 if(words[i].substr(0,2) == "//")
370 fileContent += words[i] + " ";
376 //check every instruction
377 std::stringstream ss(fileContent);
380 std::getline(ss,word, '(');
382 //check instruction name
383 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
386 if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
388 std::getline(ss,word, ')');
389 //std::cout << word << std::endl;
390 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
392 if (words.size() > 0)
395 properties["lib add " + words[0]] = true;
403 //check libraries' structure
404 for (int i = 0; i < (int)(this->libraries.size()); i++)
406 properties["library " + this->libraries[i]->GetName()] = true;
407 this->libraries[i]->CheckStructure(properties);
411 bool modelCDMLib::IsLibraryIncluded(const std::string& library_name)
413 if(this->HasCMakeLists())
415 std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
416 if (CMFile.is_open())
421 std::getline(CMFile, line);
426 std::vector<std::string> lineSeg;
427 CDMUtilities::splitter::split(lineSeg,line,"()",CDMUtilities::splitter::no_empties);
428 if(lineSeg.size() > 0 && lineSeg[0] == "ADD_SUBDIRECTORY" && lineSeg[1] == library_name)
441 bool modelCDMLib::SetLibraryInclude(const std::string& library_name, const bool& toInclude)
443 if (this->HasCMakeLists())
445 std::ifstream CMFile(this->CMakeLists->GetPath().c_str());
446 if (CMFile.is_open())
448 std::stringstream outs;
453 std::getline(CMFile, line);
456 std::vector<std::string> segs;
457 CDMUtilities::splitter::split(segs, line, " ", CDMUtilities::splitter::no_empties);
459 if(segs.size() > 0 && segs[0][0] == '#')
463 CDMUtilities::splitter::split(segs, line, " #()", CDMUtilities::splitter::no_empties);
464 if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name)
467 outs << "ADD_SUBDIRECTORY(" << library_name << ")\n";
470 outs << line << "\n";
474 outs << line << "\n";
480 if (segs.size() > 0 && !toInclude)
482 CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
483 if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name)
485 outs << "#" << line << "\n";
489 outs << line << "\n";
494 CDMUtilities::splitter::split(segs, line, " ()", CDMUtilities::splitter::no_empties);
495 if (segs.size() > 1 && segs[0] == "ADD_SUBDIRECTORY" && segs[1] == library_name)
499 outs << line << "\n";
511 if(!found && toInclude)
512 outs << "ADD_SUBDIRECTORY(" << library_name << ")\n";
514 std::ofstream CMFileOut(this->CMakeLists->GetPath().c_str());
515 if (CMFileOut.is_open())
517 CMFileOut << outs.rdbuf();