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 "modelCDMPackage.h"
42 #include "CDMUtilities.h"
44 modelCDMPackage::modelCDMPackage()
49 modelCDMPackage::modelCDMPackage(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
51 std::cout << "creating package: " + path + "\n";
52 this->parent = parent;
53 this->type = wxDIR_DIRS;
57 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
59 std::ifstream confFile;
60 confFile.open((pathMakeLists).c_str());
63 while(confFile.is_open() && !confFile.eof())
66 std::getline(confFile,word,'(');
67 std::vector<std::string> wordBits;
68 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
70 if(wordBits[wordBits.size()-1] == "SET")
73 std::getline(confFile,word,')');
74 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
75 if(wordBits[0] == "BBTK_PACKAGE_NAME")
78 for (int i = 2; i < wordBits.size(); i++)
80 word += " " + wordBits[i];
83 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
85 this->namePackage = wordBits[0];
87 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
90 for (int i = 2; i < wordBits.size(); i++)
92 word += " " + wordBits[i];
95 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
97 this->authors = wordBits[0];
99 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
102 for (int i = 2; i < wordBits.size(); i++)
104 word += " " + wordBits[i];
107 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
109 this->description = wordBits[0];
111 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
113 this->version = wordBits[1];
115 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
117 this->version += "." + wordBits[1];
119 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
121 this->version += "." + wordBits[1];
129 //check all folders and files
130 wxDir dir(crea::std2wx((path).c_str()));
136 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
139 std::string stdfileName = crea::wx2std(fileName);
140 //if src, check for black boxes
141 if(stdfileName == "src")
143 this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
144 this->children.push_back(this->src);
148 this->children.push_back(new modelCDMFolder(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
151 cont = dir.GetNext(&fileName);
155 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
158 std::string stdfileName = crea::wx2std(fileName);
160 //if CMakeLists, create CMakeLists
161 if(stdfileName == "CMakeLists.txt")
163 this->CMakeLists = new modelCDMCMakeListsFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
164 this->children.push_back(this->CMakeLists);
168 this->children.push_back(new modelCDMFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
170 //if is an unknown file, create file
171 cont = dir.GetNext(&fileName);
174 this->SortChildren();
177 modelCDMPackage::~modelCDMPackage()
181 const std::string& modelCDMPackage::GetNamePackage() const
183 return this->namePackage;
186 const std::string& modelCDMPackage::GetAuthors() const
188 return this->authors;
191 const std::string& modelCDMPackage::GetAuthorsEmail() const
193 return this->authorsEmail;
196 const std::string& modelCDMPackage::GetVersion() const
198 return this->version;
201 const std::string& modelCDMPackage::GetDescription() const
203 return this->description;
206 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
211 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
213 std::vector<std::string> words;
214 CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
215 std::string authorsReal = words[0];
216 for (int i = 1; i < words.size(); i++)
218 authorsReal += "/" + words[i];
222 //opening original cmakelists
223 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
226 result = new std::string("CMakeLists.txt file failed to open.");
229 //opening temporal cmakelists
230 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
233 result = new std::string("CMakeLists.txt.tmp file failed to open.");
236 //copying contents from original to temporal and making changes
237 while (getline(in, line))
239 if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
240 line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
241 out << line << std::endl;
245 //delete old file and rename new file
246 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
247 if(system(renameCommand.c_str()))
249 result = new std::string("An error occurred while running '" + renameCommand + "'.");
253 this->authors = authorsReal;
257 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
259 //TODO: implement method
263 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
265 std::vector<std::string> vers;
266 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
270 //opening original cmakelists
271 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
274 result = new std::string("CMakeLists.txt file failed to open.");
277 //opening temporal cmakelists
278 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
281 result = new std::string("CMakeLists.txt.tmp file failed to open.");
284 //copying contents from original to temporal and making changes
285 while (getline(in, line))
287 if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
288 line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
289 else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
290 line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
291 else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
292 line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
293 out << line << std::endl;
297 //delete old file and rename new file
298 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
299 if(system(renameCommand.c_str()))
301 result = new std::string("An error occurred while running '" + renameCommand + "'.");
305 this->version = vers[0] + "." + vers[1] + "." + vers[2];
309 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
311 std::vector<std::string> words;
312 CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
313 std::string descriptionReal = words[0];
314 for (int i = 1; i < words.size(); i++)
316 descriptionReal += " " + words[i];
320 //opening original cmakelists
321 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
324 result = new std::string("CMakeLists.txt file failed to open.");
327 //opening temporal cmakelists
328 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
331 result = new std::string("CMakeLists.txt.tmp file failed to open.");
334 //copying contents from original to temporal and making changes
335 while (getline(in, line))
337 if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
338 line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
339 out << line << std::endl;
343 //delete old file and rename new file
344 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
345 if(system(renameCommand.c_str()))
347 result = new std::string("An error occurred while running '" + renameCommand + "'.");
351 this->description = descriptionReal;
355 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
356 std::string*& result,
357 const std::string& name,
358 const std::string& type,
359 const std::string& format,
360 const std::string& categories,
361 const std::string& authors,
362 const std::string& authorsEmail,
363 const std::string& description
366 return this->src->CreateBlackBox(result,name, this->namePackage, type,format,categories,authors,authorsEmail,description);
369 const bool modelCDMPackage::Refresh(std::string*& result)
371 std::cout << "refreshing package " << this->namePackage << std::endl;
372 this->type = wxDIR_DIRS;
376 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
378 std::ifstream confFile;
379 confFile.open((pathMakeLists).c_str());
382 while(confFile.is_open() && !confFile.eof())
385 std::getline(confFile,word,'(');
386 std::vector<std::string> wordBits;
387 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
389 if(wordBits[wordBits.size()-1] == "SET")
392 std::getline(confFile,word,')');
393 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
394 if(wordBits[0] == "BBTK_PACKAGE_NAME")
397 for (int i = 2; i < wordBits.size(); i++)
399 word += " " + wordBits[i];
402 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
404 this->namePackage = wordBits[0];
406 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
409 for (int i = 2; i < wordBits.size(); i++)
411 word += " " + wordBits[i];
414 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
416 this->authors = wordBits[0];
418 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
421 for (int i = 2; i < wordBits.size(); i++)
423 word += " " + wordBits[i];
426 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
428 this->description = wordBits[0];
430 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
432 this->version = wordBits[1];
434 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
436 this->version += "." + wordBits[1];
438 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
440 this->version += "." + wordBits[1];
447 std::vector<bool> checked(this->children.size(), false);
448 bool checkedSrc = false;
451 wxDir dir(crea::std2wx((this->path).c_str()));
455 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
459 std::string stdfileName = crea::wx2std(fileName);
461 //detect black boxes in src
462 if(stdfileName == "src")
464 //check if box already exist
466 if (this->src != NULL)
469 int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
472 if(!this->src->Refresh(result))
477 this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
478 this->children.push_back(this->src);
484 //check if folder already exist
486 for (int i = 0; !found && i < this->children.size(); i++)
488 if (this->children[i]->GetName() == stdfileName)
492 if(!this->children[i]->Refresh(result))
498 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
499 this->children.push_back(folder);
502 cont = dir.GetNext(&fileName);
506 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
509 std::string stdfileName = crea::wx2std(fileName);
511 //if CMakeLists, create CMakeLists
512 if(stdfileName == "CMakeLists.txt")
514 if (this->CMakeLists == NULL)
516 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
517 this->children.push_back(this->CMakeLists);
521 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
523 if(!this->CMakeLists->Refresh(result))
527 //if is an unknown file, create file
531 for (int i = 0; !found && i < this->children.size(); i++)
533 if (this->children[i]->GetName() == stdfileName)
537 if(!this->children[i]->Refresh(result))
544 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
545 this->children.push_back(file);
549 cont = dir.GetNext(&fileName);
558 for (int i = 0; i < checked.size(); i++)
562 delete this->children[i];
563 this->children.erase(this->children.begin()+i);
564 checked.erase(checked.begin()+i);
568 this->SortChildren();