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(const std::string& path, const std::string& name, const int& level)
51 std::cout << "creating package: " + path + "\n";
52 this->type = wxDIR_DIRS;
56 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
58 std::ifstream confFile;
59 confFile.open((pathMakeLists).c_str());
62 while(confFile.is_open() && !confFile.eof())
65 std::getline(confFile,word,'(');
66 std::vector<std::string> wordBits;
67 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
69 if(wordBits[wordBits.size()-1] == "SET")
72 std::getline(confFile,word,')');
73 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
74 if(wordBits[0] == "BBTK_PACKAGE_NAME")
77 for (int i = 2; i < wordBits.size(); i++)
79 word += " " + wordBits[i];
82 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
84 this->namePackage = wordBits[0];
86 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
89 for (int i = 2; i < wordBits.size(); i++)
91 word += " " + wordBits[i];
94 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
96 this->authors = wordBits[0];
98 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
101 for (int i = 2; i < wordBits.size(); i++)
103 word += " " + wordBits[i];
106 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
108 this->description = wordBits[0];
110 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
112 this->version = wordBits[1];
114 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
116 this->version += "." + wordBits[1];
118 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
120 this->version += "." + wordBits[1];
128 //check all folders and files
129 wxDir dir(crea::std2wx((path).c_str()));
135 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
138 std::string stdfileName = crea::wx2std(fileName);
139 //if src, check for black boxes
140 if(stdfileName == "src")
142 this->src = new modelCDMPackageSrc(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
143 this->children.push_back(this->src);
147 this->children.push_back(new modelCDMFolder(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
150 cont = dir.GetNext(&fileName);
154 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
157 std::string stdfileName = crea::wx2std(fileName);
159 //if CMakeLists, create CMakeLists
160 if(stdfileName == "CMakeLists.txt")
162 this->CMakeLists = new modelCDMCMakeListsFile(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
163 this->children.push_back(this->CMakeLists);
167 this->children.push_back(new modelCDMFile(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
169 //if is an unknown file, create file
170 cont = dir.GetNext(&fileName);
173 this->SortChildren();
176 modelCDMPackage::~modelCDMPackage()
180 const std::string& modelCDMPackage::GetNamePackage() const
182 return this->namePackage;
185 const std::string& modelCDMPackage::GetAuthors() const
187 return this->authors;
190 const std::string& modelCDMPackage::GetAuthorsEmail() const
192 return this->authorsEmail;
195 const std::string& modelCDMPackage::GetVersion() const
197 return this->version;
200 const std::string& modelCDMPackage::GetDescription() const
202 return this->description;
205 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
210 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
212 std::vector<std::string> words;
213 CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
214 std::string authorsReal = words[0];
215 for (int i = 1; i < words.size(); i++)
217 authorsReal += "/" + words[i];
221 //opening original cmakelists
222 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
225 result = new std::string("CMakeLists.txt file failed to open.");
228 //opening temporal cmakelists
229 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
232 result = new std::string("CMakeLists.txt.tmp file failed to open.");
235 //copying contents from original to temporal and making changes
236 while (getline(in, line))
238 if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
239 line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
240 out << line << std::endl;
244 //delete old file and rename new file
245 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
246 if(system(renameCommand.c_str()))
248 result = new std::string("An error occurred while running '" + renameCommand + "'.");
252 this->authors = authorsReal;
256 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
258 //TODO: implement method
262 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
264 std::vector<std::string> vers;
265 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
269 //opening original cmakelists
270 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
273 result = new std::string("CMakeLists.txt file failed to open.");
276 //opening temporal cmakelists
277 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
280 result = new std::string("CMakeLists.txt.tmp file failed to open.");
283 //copying contents from original to temporal and making changes
284 while (getline(in, line))
286 if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
287 line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
288 else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
289 line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
290 else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
291 line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
292 out << line << std::endl;
296 //delete old file and rename new file
297 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
298 if(system(renameCommand.c_str()))
300 result = new std::string("An error occurred while running '" + renameCommand + "'.");
304 this->version = vers[0] + "." + vers[1] + "." + vers[2];
308 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
310 std::vector<std::string> words;
311 CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
312 std::string descriptionReal = words[0];
313 for (int i = 1; i < words.size(); i++)
315 descriptionReal += " " + words[i];
319 //opening original cmakelists
320 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
323 result = new std::string("CMakeLists.txt file failed to open.");
326 //opening temporal cmakelists
327 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
330 result = new std::string("CMakeLists.txt.tmp file failed to open.");
333 //copying contents from original to temporal and making changes
334 while (getline(in, line))
336 if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
337 line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
338 out << line << std::endl;
342 //delete old file and rename new file
343 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
344 if(system(renameCommand.c_str()))
346 result = new std::string("An error occurred while running '" + renameCommand + "'.");
350 this->description = descriptionReal;
354 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
355 std::string*& result,
356 const std::string& name,
357 const std::string& type,
358 const std::string& format,
359 const std::string& categories,
360 const std::string& authors,
361 const std::string& authorsEmail,
362 const std::string& description
365 return this->src->CreateBlackBox(result,name, this->namePackage, type,format,categories,authors,authorsEmail,description);
368 const bool modelCDMPackage::Refresh(std::string*& result)
370 std::cout << "refreshing package " << this->namePackage << std::endl;
371 this->type = wxDIR_DIRS;
375 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
377 std::ifstream confFile;
378 confFile.open((pathMakeLists).c_str());
381 while(confFile.is_open() && !confFile.eof())
384 std::getline(confFile,word,'(');
385 std::vector<std::string> wordBits;
386 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
388 if(wordBits[wordBits.size()-1] == "SET")
391 std::getline(confFile,word,')');
392 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
393 if(wordBits[0] == "BBTK_PACKAGE_NAME")
396 for (int i = 2; i < wordBits.size(); i++)
398 word += " " + wordBits[i];
401 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
403 this->namePackage = wordBits[0];
405 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
408 for (int i = 2; i < wordBits.size(); i++)
410 word += " " + wordBits[i];
413 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
415 this->authors = wordBits[0];
417 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
420 for (int i = 2; i < wordBits.size(); i++)
422 word += " " + wordBits[i];
425 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
427 this->description = wordBits[0];
429 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
431 this->version = wordBits[1];
433 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
435 this->version += "." + wordBits[1];
437 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
439 this->version += "." + wordBits[1];
446 std::vector<bool> checked(this->children.size(), false);
447 bool checkedSrc = false;
450 wxDir dir(crea::std2wx((this->path).c_str()));
454 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
458 std::string stdfileName = crea::wx2std(fileName);
460 //detect black boxes in src
461 if(stdfileName == "src")
463 //check if box already exist
465 if (this->src != NULL)
468 int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
471 if(!this->src->Refresh(result))
476 this->src = new modelCDMPackageSrc(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
477 this->children.push_back(this->src);
483 //check if folder already exist
485 for (int i = 0; !found && i < this->children.size(); i++)
487 if (this->children[i]->GetName() == stdfileName)
491 if(!this->children[i]->Refresh(result))
497 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
498 this->children.push_back(folder);
501 cont = dir.GetNext(&fileName);
505 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
508 std::string stdfileName = crea::wx2std(fileName);
510 //if CMakeLists, create CMakeLists
511 if(stdfileName == "CMakeLists.txt")
513 if (this->CMakeLists == NULL)
515 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
516 this->children.push_back(this->CMakeLists);
520 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
522 if(!this->CMakeLists->Refresh(result))
526 //if is an unknown file, create file
530 for (int i = 0; !found && i < this->children.size(); i++)
532 if (this->children[i]->GetName() == stdfileName)
536 if(!this->children[i]->Refresh(result))
543 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
544 this->children.push_back(file);
548 cont = dir.GetNext(&fileName);
557 for (int i = 0; i < checked.size(); i++)
561 delete this->children[i];
562 this->children.erase(this->children.begin()+i);
563 checked.erase(checked.begin()+i);
567 this->SortChildren();