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& name,
128 //copy template library folder with new name
129 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_lib\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
130 if(system(copyCommand.c_str()))
132 result = new std::string("An error occurred while running '" + copyCommand + "'.");
135 //set name of library in CMakeLists inside copied folder
137 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
140 result = new std::string("CMakeLists.txt file failed to open.");
143 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
144 while (getline(in, line))
146 if(line == "SET ( LIBRARY_NAME MyLib )")
147 line = "SET ( LIBRARY_NAME " + name + " )";
148 out << line << std::endl;
152 //delete old file and rename new file
153 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
154 if(system(renameCommand.c_str()))
156 result = new std::string("An error occurred while running '" + renameCommand + "'.");
160 //add library to model
161 modelCDMLibrary* library = new modelCDMLibrary(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
162 this->libraries.push_back(library);
163 this->children.push_back(library);
165 this->SortChildren();
167 result = new std::string(this->path + CDMUtilities::SLASH + name);
171 const bool modelCDMLib::Refresh(std::string*& result)
173 std::cout << "refreshing lib" << std::endl;
174 this->type = wxDIR_DIRS;
178 std::vector<bool> checked(this->children.size(), false);
179 std::vector<bool> checkedLibraries(this->libraries.size(), false);
182 wxDir dir(crea::std2wx((this->path).c_str()));
186 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
189 std::string stdfileName = crea::wx2std(fileName);
191 if(stdfileName != "template_lib")
193 std::string libraryName = stdfileName;
194 //check if library already exist
196 for (int i = 0; !found && i < this->libraries.size(); i++)
198 if (this->libraries[i]->GetName() == libraryName)
201 int pos = std::find(this->children.begin(), this->children.end(), this->libraries[i]) - this->children.begin();
203 checkedLibraries[i] = true;
204 if(!this->libraries[i]->Refresh(result))
210 modelCDMLibrary* library = new modelCDMLibrary(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
211 this->libraries.push_back(library);
212 this->children.push_back(library);
217 //check if folder already exist
219 for (int i = 0; !found && i < this->children.size(); i++)
221 if (this->children[i]->GetName() == stdfileName)
225 if(!this->children[i]->Refresh(result))
231 modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
232 this->children.push_back(folder);
235 cont = dir.GetNext(&fileName);
238 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
241 std::string stdfileName = crea::wx2std(fileName);
243 //if CMakeLists, create CMakeLists
244 if(stdfileName == "CMakeLists.txt")
246 if (this->CMakeLists == NULL)
248 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
249 this->children.push_back(this->CMakeLists);
253 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
255 if(!this->CMakeLists->Refresh(result))
259 //if is an unknown file, create file
263 for (int i = 0; !found && i < this->children.size(); i++)
265 if (this->children[i]->GetName() == stdfileName)
269 if(!this->children[i]->Refresh(result))
276 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
277 this->children.push_back(file);
281 cont = dir.GetNext(&fileName);
285 for (int i = 0; i < checkedLibraries.size(); i++)
287 if(!checkedLibraries[i])
289 this->libraries.erase(this->libraries.begin()+i);
290 checkedLibraries.erase(checkedLibraries.begin()+i);
294 for (int i = 0; i < checked.size(); i++)
298 delete this->children[i];
299 this->children.erase(this->children.begin()+i);
300 checked.erase(checked.begin()+i);
304 this->SortChildren();
305 std::sort(this->libraries.begin(), this->libraries.end(), CompareNodeItem);
309 void modelCDMLib::CheckStructure(std::map<std::string, bool>& properties)
312 if(this->CMakeLists != NULL)
314 //set properties parameters based on model
315 for (int i = 0; i < this->libraries.size(); i++)
316 properties["lib add " + libraries[i]->GetName()] = false;
319 std::ifstream confFile;
320 confFile.open((this->CMakeLists->GetPath()).c_str());
322 //take everything that is not commented
323 std::string fileContent;
326 std::vector<std::string> words;
327 while(confFile.is_open() && !confFile.eof())
329 std::getline(confFile,word, '\n');
332 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
333 if (words.size() > 0)
336 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
337 for (int i = 0; i < words.size(); i++)
339 if(words[i].substr(0,2) == "//")
341 fileContent += words[i] + " ";
347 //check every instruction
348 std::stringstream ss(fileContent);
351 std::getline(ss,word, '(');
353 //check instruction name
354 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
357 if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
359 std::getline(ss,word, ')');
360 //std::cout << word << std::endl;
361 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
363 if (words.size() > 0)
366 properties["lib add " + words[0]] = true;
374 //check libraries' structure
375 for (int i = 0; i < this->libraries.size(); i++)
377 properties["library " + this->libraries[i]->GetName()] = true;
378 this->libraries[i]->CheckStructure(properties);