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 int& level)
51 this->type = wxDIR_DIRS;
54 //TODO: set pathMakeLists for windows
55 std::string pathMakeLists = path + "/CMakeLists.txt";
57 std::ifstream confFile;
58 confFile.open((pathMakeLists).c_str());
61 while(confFile.is_open() && !confFile.eof())
64 std::getline(confFile,word,'(');
65 std::vector<std::string> wordBits;
66 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
68 if(wordBits[wordBits.size()-1] == "SET")
71 std::getline(confFile,word,')');
72 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
73 if(wordBits[0] == "BBTK_PACKAGE_NAME")
76 for (int i = 2; i < wordBits.size(); i++)
78 word += " " + wordBits[i];
81 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
83 this->namePackage = wordBits[0];
85 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
88 for (int i = 2; i < wordBits.size(); i++)
90 word += " " + wordBits[i];
93 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
95 this->authors = wordBits[0];
97 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
100 for (int i = 2; i < wordBits.size(); i++)
102 word += " " + wordBits[i];
105 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
107 this->description = wordBits[0];
109 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
111 this->version = wordBits[1];
113 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
115 this->version += "." + wordBits[1];
117 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
119 this->version += "." + wordBits[1];
124 std::vector<std::string> words;
125 std::string delimiters;
126 //TODO::fix for windows
128 CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
129 this->name = words[words.size()-1];
133 //check all folders and files
134 wxDir dir(crea::std2wx((path).c_str()));
140 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
143 std::string stdfileName = crea::wx2std(fileName);
144 //if src, check for black boxes
145 if(stdfileName == "src")
147 this->src = new modelCDMPackageSrc(path + delimiters + "src", this->level + 1);
148 this->children.push_back(this->src);
149 /*wxDir srcF(crea::std2wx((path + delimiters + "src").c_str()));
153 bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
156 if(crea::wx2std(srcName.substr(0,2)) == "bb")
158 modelCDMBlackBox* blackbox = new modelCDMBlackBox(crea::wx2std(srcName), path + delimiters + "src", this->level + 1);
159 this->blackBoxes.push_back(blackbox);
160 this->children.push_back(blackbox);
162 innerCont = srcF.GetNext(&srcName);
168 this->children.push_back(new modelCDMFolder(path + delimiters + stdfileName, this->level + 1));
171 cont = dir.GetNext(&fileName);
175 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
178 std::string stdfileName = crea::wx2std(fileName);
180 //if CMakeLists, create CMakeLists
181 if(stdfileName == "CMakeLists.txt")
183 this->CMakeLists = new modelCDMCMakeListsFile(path + delimiters + stdfileName, this->level + 1);
184 this->children.push_back(this->CMakeLists);
188 this->children.push_back(new modelCDMFile(path + delimiters + stdfileName, this->level + 1));
190 //if is an unknown file, create file
191 cont = dir.GetNext(&fileName);
194 this->SortChildren();
197 modelCDMPackage::~modelCDMPackage()
201 const std::string& modelCDMPackage::GetNamePackage() const
203 return this->namePackage;
206 const std::string& modelCDMPackage::GetAuthors() const
208 return this->authors;
211 const std::string& modelCDMPackage::GetAuthorsEmail() const
213 return this->authorsEmail;
216 const std::string& modelCDMPackage::GetVersion() const
218 return this->version;
221 const std::string& modelCDMPackage::GetDescription() const
223 return this->description;
226 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
231 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
233 std::vector<std::string> words;
234 CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
235 std::string authorsReal = words[0];
236 for (int i = 1; i < words.size(); i++)
238 authorsReal += "/" + words[i];
242 //opening original cmakelists
243 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
246 result = new std::string("CMakeLists.txt file failed to open.");
249 //opening temporal cmakelists
250 std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
253 result = new std::string("CMakeLists.txt.tmp file failed to open.");
256 //copying contents from original to temporal and making changes
257 while (getline(in, line))
259 if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
260 line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
261 out << line << std::endl;
265 //delete old file and rename new file
266 std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
267 if(system(renameCommand.c_str()))
269 result = new std::string("An error occurred while running '" + renameCommand + "'.");
273 this->authors = authorsReal;
277 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
279 //TODO: implement method
283 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
285 std::vector<std::string> vers;
286 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
290 //opening original cmakelists
291 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
294 result = new std::string("CMakeLists.txt file failed to open.");
297 //opening temporal cmakelists
298 std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
301 result = new std::string("CMakeLists.txt.tmp file failed to open.");
304 //copying contents from original to temporal and making changes
305 while (getline(in, line))
307 if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
308 line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
309 else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
310 line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
311 else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
312 line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
313 out << line << std::endl;
317 //delete old file and rename new file
318 std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
319 if(system(renameCommand.c_str()))
321 result = new std::string("An error occurred while running '" + renameCommand + "'.");
325 this->version = vers[0] + "." + vers[1] + "." + vers[2];
329 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
331 std::vector<std::string> words;
332 CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
333 std::string descriptionReal = words[0];
334 for (int i = 1; i < words.size(); i++)
336 descriptionReal += " " + words[i];
340 //opening original cmakelists
341 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
344 result = new std::string("CMakeLists.txt file failed to open.");
347 //opening temporal cmakelists
348 std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
351 result = new std::string("CMakeLists.txt.tmp file failed to open.");
354 //copying contents from original to temporal and making changes
355 while (getline(in, line))
357 if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
358 line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
359 out << line << std::endl;
363 //delete old file and rename new file
364 std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
365 if(system(renameCommand.c_str()))
367 result = new std::string("An error occurred while running '" + renameCommand + "'.");
371 this->description = descriptionReal;
375 bool modelCDMPackage::CreateBlackBox(
376 const std::string& name,
377 const std::string& authors,
378 const std::string& authorsEmail,
379 const std::string& categories,
380 const std::string& description
383 //TODO: implement method
387 const bool modelCDMPackage::Refresh(std::string*& result)
389 std::cout << "refreshing package" << std::endl;
390 this->type = wxDIR_DIRS;
397 //TODO: set pathMakeLists for windows
398 std::string pathMakeLists = path + "/CMakeLists.txt";
400 std::ifstream confFile;
401 confFile.open((pathMakeLists).c_str());
404 while(confFile.is_open() && !confFile.eof())
407 std::getline(confFile,word,'(');
408 std::vector<std::string> wordBits;
409 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
411 if(wordBits[wordBits.size()-1] == "SET")
414 std::getline(confFile,word,')');
415 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
416 if(wordBits[0] == "BBTK_PACKAGE_NAME")
419 for (int i = 2; i < wordBits.size(); i++)
421 word += " " + wordBits[i];
424 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
426 this->namePackage = wordBits[0];
428 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
431 for (int i = 2; i < wordBits.size(); i++)
433 word += " " + wordBits[i];
436 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
438 this->authors = wordBits[0];
440 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
443 for (int i = 2; i < wordBits.size(); i++)
445 word += " " + wordBits[i];
448 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
450 this->description = wordBits[0];
452 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
454 this->version = wordBits[1];
456 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
458 this->version += "." + wordBits[1];
460 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
462 this->version += "." + wordBits[1];
469 std::vector<bool> checked(this->children.size(), false);
470 bool checkedSrc = false;
473 wxDir dir(crea::std2wx((this->path).c_str()));
477 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
481 std::string stdfileName = crea::wx2std(fileName);
483 //detect black boxes in src
484 if(stdfileName == "src")
486 //check if box already exist
488 if (this->src != NULL)
491 int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
494 if(!this->src->Refresh(result))
499 this->src = new modelCDMPackageSrc(path + "/" + "src", this->level +1);
500 this->children.push_back(this->src);
502 /*wxDir srcF(crea::std2wx((path + "/" + "src").c_str()));
506 bool innerCont = srcF.GetFirst(&srcName, wxT("*.h"), wxDIR_FILES);
509 std::string blackBoxName = crea::wx2std(srcName);
510 if(crea::wx2std(srcName.substr(0,2)) == "bb")
512 //check if box already exist
514 for (int i = 0;!found && i < this->blackBoxes.size(); i++)
516 if (this->blackBoxes[i]->GetName() == blackBoxName)
519 int pos = std::find(this->children.begin(), this->children.end(), this->blackBoxes[i]) - this->children.begin();
521 checkedBlackBoxes[i] = true;
522 if(!this->blackBoxes[i]->Refresh(result))
528 modelCDMBlackBox* blackBox = new modelCDMBlackBox(blackBoxName, path + "/" + "src", this->level +1);
529 this->blackBoxes.push_back(blackBox);
530 this->children.push_back(blackBox);
533 innerCont = srcF.GetNext(&srcName);
541 //check if folder already exist
543 for (int i = 0;!found && i < this->children.size(); i++)
545 if (this->children[i]->GetName() == stdfileName)
549 if(!this->children[i]->Refresh(result))
555 modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
556 this->children.push_back(folder);
559 cont = dir.GetNext(&fileName);
563 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
566 std::string stdfileName = crea::wx2std(fileName);
568 //if CMakeLists, create CMakeLists
569 if(stdfileName == "CMakeLists.txt")
571 if (this->CMakeLists == NULL)
573 this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
574 this->children.push_back(this->CMakeLists);
578 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
580 if(!this->CMakeLists->Refresh(result))
584 //if is an unknown file, create file
588 for (int i = 0; i <!found && this->children.size(); i++)
590 if (this->children[i]->GetName() == stdfileName)
594 if(!this->children[i]->Refresh(result))
601 modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
602 this->children.push_back(file);
606 cont = dir.GetNext(&fileName);
615 for (int i = 0; i < checked.size(); i++)
619 delete this->children[i];
620 this->children.erase(this->children.begin()+i);
621 checked.erase(checked.begin()+i);
625 this->SortChildren();