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()
48 modelCDMPackage::modelCDMPackage(const std::string& path, const int& level)
50 this->type = wxDIR_DIRS;
53 //TODO: set pathMakeLists for windows
54 std::string pathMakeLists = path + "/CMakeLists.txt";
56 std::ifstream confFile;
57 confFile.open((pathMakeLists).c_str());
60 while(confFile.is_open() && !confFile.eof())
63 std::getline(confFile,word,'(');
64 std::vector<std::string> wordBits;
65 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
67 if(wordBits[wordBits.size()-1] == "SET")
70 std::getline(confFile,word,')');
71 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
72 if(wordBits[0] == "BBTK_PACKAGE_NAME")
75 for (int i = 2; i < wordBits.size(); i++)
77 word += " " + wordBits[i];
80 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
82 this->namePackage = wordBits[0];
84 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
87 for (int i = 2; i < wordBits.size(); i++)
89 word += " " + wordBits[i];
92 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
94 this->authors = wordBits[0];
96 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
99 for (int i = 2; i < wordBits.size(); i++)
101 word += " " + wordBits[i];
104 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
106 this->description = wordBits[0];
108 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
110 this->version = wordBits[1];
112 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
114 this->version += "." + wordBits[1];
116 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
118 this->version += "." + wordBits[1];
123 std::vector<std::string> words;
124 std::string delimiters;
125 //TODO::fix for windows
127 CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
128 this->name = words[words.size()-1];
132 //check all folders and files
133 wxDir dir(crea::std2wx((path).c_str()));
139 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
142 std::string stdfileName = crea::wx2std(fileName);
143 //if src, check for black boxes
144 if(stdfileName == "src")
146 wxDir srcF(crea::std2wx((path + delimiters + "src").c_str()));
150 bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
153 if(crea::wx2std(srcName.substr(0,2)) == "bb")
155 modelCDMBlackBox* blackbox = new modelCDMBlackBox(crea::wx2std(srcName), path + delimiters + "src", this->level + 1);
156 this->blackBoxes.push_back(blackbox);
157 this->children.push_back(blackbox);
159 innerCont = srcF.GetNext(&srcName);
163 //if is an unknown folder, create folder
166 this->children.push_back(new modelCDMFolder(path + delimiters + stdfileName, this->level + 1));
168 cont = dir.GetNext(&fileName);
172 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
175 std::string stdfileName = crea::wx2std(fileName);
177 //if CMakeLists, create CMakeLists
178 if(stdfileName == "CMakeLists.txt")
180 this->CMakeLists = new modelCDMCMakeListsFile(path + delimiters + stdfileName, this->level + 1);
181 this->children.push_back(this->CMakeLists);
185 this->children.push_back(new modelCDMFile(path + delimiters + stdfileName, this->level + 1));
187 //if is an unknown file, create file
188 cont = dir.GetNext(&fileName);
191 this->SortChildren();
194 modelCDMPackage::~modelCDMPackage()
198 const std::string& modelCDMPackage::GetNamePackage() const
200 return this->namePackage;
203 const std::string& modelCDMPackage::GetAuthors() const
205 return this->authors;
208 const std::string& modelCDMPackage::GetAuthorsEmail() const
210 return this->authorsEmail;
213 const std::string& modelCDMPackage::GetVersion() const
215 return this->version;
218 const std::string& modelCDMPackage::GetDescription() const
220 return this->description;
223 const std::vector<modelCDMBlackBox*>& modelCDMPackage::GetBlackBoxes() const
225 return this->blackBoxes;
228 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
230 std::vector<std::string> words;
231 CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
232 std::string authorsReal = words[0];
233 for (int i = 1; i < words.size(); i++)
235 authorsReal += "/" + words[i];
239 //opening original cmakelists
240 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
243 result = new std::string("CMakeLists.txt file failed to open.");
246 //opening temporal cmakelists
247 std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
250 result = new std::string("CMakeLists.txt.tmp file failed to open.");
253 //copying contents from original to temporal and making changes
254 while (getline(in, line))
256 if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
257 line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
258 out << line << std::endl;
262 //delete old file and rename new file
263 std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
264 if(system(renameCommand.c_str()))
266 result = new std::string("An error occurred while running '" + renameCommand + "'.");
270 this->authors = authorsReal;
274 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
276 //TODO: implement method
280 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
282 std::vector<std::string> vers;
283 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
287 //opening original cmakelists
288 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
291 result = new std::string("CMakeLists.txt file failed to open.");
294 //opening temporal cmakelists
295 std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
298 result = new std::string("CMakeLists.txt.tmp file failed to open.");
301 //copying contents from original to temporal and making changes
302 while (getline(in, line))
304 if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
305 line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
306 else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
307 line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
308 else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
309 line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
310 out << line << std::endl;
314 //delete old file and rename new file
315 std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
316 if(system(renameCommand.c_str()))
318 result = new std::string("An error occurred while running '" + renameCommand + "'.");
322 this->version = vers[0] + "." + vers[1] + "." + vers[2];
326 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
328 std::vector<std::string> words;
329 CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
330 std::string descriptionReal = words[0];
331 for (int i = 1; i < words.size(); i++)
333 descriptionReal += " " + words[i];
337 //opening original cmakelists
338 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
341 result = new std::string("CMakeLists.txt file failed to open.");
344 //opening temporal cmakelists
345 std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
348 result = new std::string("CMakeLists.txt.tmp file failed to open.");
351 //copying contents from original to temporal and making changes
352 while (getline(in, line))
354 if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
355 line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
356 out << line << std::endl;
360 //delete old file and rename new file
361 std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
362 if(system(renameCommand.c_str()))
364 result = new std::string("An error occurred while running '" + renameCommand + "'.");
368 this->description = descriptionReal;
372 bool modelCDMPackage::CreateBlackBox(
373 const std::string& name,
374 const std::string& authors,
375 const std::string& authorsEmail,
376 const std::string& categories,
377 const std::string& description
380 //TODO: implement method
384 const bool modelCDMPackage::Refresh(std::string*& result)
386 std::cout << "refreshing package" << std::endl;
387 this->type = wxDIR_DIRS;
394 //TODO: set pathMakeLists for windows
395 std::string pathMakeLists = path + "/CMakeLists.txt";
397 std::ifstream confFile;
398 confFile.open((pathMakeLists).c_str());
401 while(confFile.is_open() && !confFile.eof())
404 std::getline(confFile,word,'(');
405 std::vector<std::string> wordBits;
406 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
408 if(wordBits[wordBits.size()-1] == "SET")
411 std::getline(confFile,word,')');
412 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
413 if(wordBits[0] == "BBTK_PACKAGE_NAME")
416 for (int i = 2; i < wordBits.size(); i++)
418 word += " " + wordBits[i];
421 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
423 this->namePackage = wordBits[0];
425 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
428 for (int i = 2; i < wordBits.size(); i++)
430 word += " " + wordBits[i];
433 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
435 this->authors = wordBits[0];
437 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
440 for (int i = 2; i < wordBits.size(); i++)
442 word += " " + wordBits[i];
445 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
447 this->description = wordBits[0];
449 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
451 this->version = wordBits[1];
453 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
455 this->version += "." + wordBits[1];
457 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
459 this->version += "." + wordBits[1];
466 std::vector<bool> checked(this->children.size(), false);
467 std::vector<bool> checkedBlackBoxes(this->blackBoxes.size(), false);
470 wxDir dir(crea::std2wx((this->path).c_str()));
474 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
478 std::string stdfileName = crea::wx2std(fileName);
481 if(stdfileName == "src")
483 wxDir srcF(crea::std2wx((path + "/" + "src").c_str()));
487 bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
490 std::string blackBoxName = crea::wx2std(srcName);
491 if(crea::wx2std(srcName.substr(0,2)) == "bb")
493 //check if box already exist
495 for (int i = 0;!found && i < this->blackBoxes.size(); i++)
497 if (this->blackBoxes[i]->GetName() == blackBoxName)
500 int pos = std::find(this->children.begin(), this->children.end(), this->blackBoxes[i]) - this->children.begin();
502 checkedBlackBoxes[i] = true;
503 if(!this->blackBoxes[i]->Refresh(result))
509 modelCDMBlackBox* blackBox = new modelCDMBlackBox(blackBoxName, path + "/" + "src", this->level +1);
510 this->blackBoxes.push_back(blackBox);
511 this->children.push_back(blackBox);
514 innerCont = srcF.GetNext(&srcName);
522 //check if they already exist
524 for (int i = 0;!found && i < this->children.size(); i++)
526 if (this->children[i]->GetName() == stdfileName)
530 if(!this->children[i]->Refresh(result))
536 modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
537 this->children.push_back(folder);
539 cont = dir.GetNext(&fileName);
543 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
546 std::string stdfileName = crea::wx2std(fileName);
548 //if CMakeLists, create CMakeLists
549 if(stdfileName == "CMakeLists.txt")
551 if (this->CMakeLists == NULL)
553 this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
554 this->children.push_back(this->CMakeLists);
558 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
560 if(!this->CMakeLists->Refresh(result))
564 //if is an unknown file, create file
568 for (int i = 0; i <!found && this->children.size(); i++)
570 if (this->children[i]->GetName() == stdfileName)
574 if(!this->children[i]->Refresh(result))
581 modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
582 this->children.push_back(file);
586 cont = dir.GetNext(&fileName);
590 for (int i = 0; i < checkedBlackBoxes.size(); i++)
592 if(!checkedBlackBoxes[i])
594 this->blackBoxes.erase(this->blackBoxes.begin()+i);
595 checkedBlackBoxes.erase(checkedBlackBoxes.begin()+i);
599 for (int i = 0; i < checked.size(); i++)
603 delete this->children[i];
604 this->children.erase(this->children.begin()+i);
605 checked.erase(checked.begin()+i);
609 this->SortChildren();