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 this->type = wxDIR_DIRS;
55 std::string pathMakeLists = path + CDMUtilities::SLASH + "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];
127 //check all folders and files
128 wxDir dir(crea::std2wx((path).c_str()));
134 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
137 std::string stdfileName = crea::wx2std(fileName);
138 //if src, check for black boxes
139 if(stdfileName == "src")
141 this->src = new modelCDMPackageSrc(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
142 this->children.push_back(this->src);
146 this->children.push_back(new modelCDMFolder(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
149 cont = dir.GetNext(&fileName);
153 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
156 std::string stdfileName = crea::wx2std(fileName);
158 //if CMakeLists, create CMakeLists
159 if(stdfileName == "CMakeLists.txt")
161 this->CMakeLists = new modelCDMCMakeListsFile(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
162 this->children.push_back(this->CMakeLists);
166 this->children.push_back(new modelCDMFile(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
168 //if is an unknown file, create file
169 cont = dir.GetNext(&fileName);
172 this->SortChildren();
175 modelCDMPackage::~modelCDMPackage()
179 const std::string& modelCDMPackage::GetNamePackage() const
181 return this->namePackage;
184 const std::string& modelCDMPackage::GetAuthors() const
186 return this->authors;
189 const std::string& modelCDMPackage::GetAuthorsEmail() const
191 return this->authorsEmail;
194 const std::string& modelCDMPackage::GetVersion() const
196 return this->version;
199 const std::string& modelCDMPackage::GetDescription() const
201 return this->description;
204 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
209 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
211 std::vector<std::string> words;
212 CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
213 std::string authorsReal = words[0];
214 for (int i = 1; i < words.size(); i++)
216 authorsReal += "/" + words[i];
220 //opening original cmakelists
221 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
224 result = new std::string("CMakeLists.txt file failed to open.");
227 //opening temporal cmakelists
228 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
231 result = new std::string("CMakeLists.txt.tmp file failed to open.");
234 //copying contents from original to temporal and making changes
235 while (getline(in, line))
237 if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
238 line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
239 out << line << std::endl;
243 //delete old file and rename new file
244 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
245 if(system(renameCommand.c_str()))
247 result = new std::string("An error occurred while running '" + renameCommand + "'.");
251 this->authors = authorsReal;
255 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
257 //TODO: implement method
261 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
263 std::vector<std::string> vers;
264 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
268 //opening original cmakelists
269 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
272 result = new std::string("CMakeLists.txt file failed to open.");
275 //opening temporal cmakelists
276 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
279 result = new std::string("CMakeLists.txt.tmp file failed to open.");
282 //copying contents from original to temporal and making changes
283 while (getline(in, line))
285 if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
286 line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
287 else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
288 line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
289 else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
290 line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
291 out << line << std::endl;
295 //delete old file and rename new file
296 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
297 if(system(renameCommand.c_str()))
299 result = new std::string("An error occurred while running '" + renameCommand + "'.");
303 this->version = vers[0] + "." + vers[1] + "." + vers[2];
307 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
309 std::vector<std::string> words;
310 CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
311 std::string descriptionReal = words[0];
312 for (int i = 1; i < words.size(); i++)
314 descriptionReal += " " + words[i];
318 //opening original cmakelists
319 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
322 result = new std::string("CMakeLists.txt file failed to open.");
325 //opening temporal cmakelists
326 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
329 result = new std::string("CMakeLists.txt.tmp file failed to open.");
332 //copying contents from original to temporal and making changes
333 while (getline(in, line))
335 if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
336 line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
337 out << line << std::endl;
341 //delete old file and rename new file
342 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
343 if(system(renameCommand.c_str()))
345 result = new std::string("An error occurred while running '" + renameCommand + "'.");
349 this->description = descriptionReal;
353 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
354 const std::string& name,
355 const std::string& type,
356 const std::string& format,
357 const std::string& authors,
358 const std::string& authorsEmail,
359 const std::string& categories,
360 const std::string& description
363 //TODO: implement method
367 const bool modelCDMPackage::Refresh(std::string*& result)
369 std::cout << "refreshing package" << std::endl;
370 this->type = wxDIR_DIRS;
374 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
376 std::ifstream confFile;
377 confFile.open((pathMakeLists).c_str());
380 while(confFile.is_open() && !confFile.eof())
383 std::getline(confFile,word,'(');
384 std::vector<std::string> wordBits;
385 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
387 if(wordBits[wordBits.size()-1] == "SET")
390 std::getline(confFile,word,')');
391 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
392 if(wordBits[0] == "BBTK_PACKAGE_NAME")
395 for (int i = 2; i < wordBits.size(); i++)
397 word += " " + wordBits[i];
400 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
402 this->namePackage = wordBits[0];
404 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
407 for (int i = 2; i < wordBits.size(); i++)
409 word += " " + wordBits[i];
412 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
414 this->authors = wordBits[0];
416 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
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->description = wordBits[0];
428 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
430 this->version = wordBits[1];
432 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
434 this->version += "." + wordBits[1];
436 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
438 this->version += "." + wordBits[1];
445 std::vector<bool> checked(this->children.size(), false);
446 bool checkedSrc = false;
449 wxDir dir(crea::std2wx((this->path).c_str()));
453 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
457 std::string stdfileName = crea::wx2std(fileName);
459 //detect black boxes in src
460 if(stdfileName == "src")
462 //check if box already exist
464 if (this->src != NULL)
467 int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
470 if(!this->src->Refresh(result))
475 this->src = new modelCDMPackageSrc(path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
476 this->children.push_back(this->src);
482 //check if folder already exist
484 for (int i = 0;!found && i < this->children.size(); i++)
486 if (this->children[i]->GetName() == stdfileName)
490 if(!this->children[i]->Refresh(result))
496 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
497 this->children.push_back(folder);
500 cont = dir.GetNext(&fileName);
504 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
507 std::string stdfileName = crea::wx2std(fileName);
509 //if CMakeLists, create CMakeLists
510 if(stdfileName == "CMakeLists.txt")
512 if (this->CMakeLists == NULL)
514 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
515 this->children.push_back(this->CMakeLists);
519 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
521 if(!this->CMakeLists->Refresh(result))
525 //if is an unknown file, create file
529 for (int i = 0; i <!found && this->children.size(); i++)
531 if (this->children[i]->GetName() == stdfileName)
535 if(!this->children[i]->Refresh(result))
542 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
543 this->children.push_back(file);
547 cont = dir.GetNext(&fileName);
556 for (int i = 0; i < checked.size(); i++)
560 delete this->children[i];
561 this->children.erase(this->children.begin()+i);
562 checked.erase(checked.begin()+i);
566 this->SortChildren();