# ---------------------------------------------------------------------
#
# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
-# pour la Santé)
+# pour la Sant�)
# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
# Previous Authors : Laurent Guigues, Jean-Pierre Roux
# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-B license and that you accept its terms.
# ------------------------------------------------------------------------
-*/
-
-
+ */
/*
* modelCDMProject.cpp
*
* Created on: 13/11/2012
- * Author: daniel
+ * Author: Daniel Felipe Gonzalez Obando
*/
#include "modelCDMProject.h"
-modelCDMProject::modelCDMProject(std::string name, std::string path, unsigned char type, int level)
+#include <iostream>
+#include <vector>
+#include <algorithm>
+#include <fstream>
+
+#include "CDMUtilities.h"
+#include "creaWx.h"
+#include "wx/dir.h"
+
+modelCDMProject::modelCDMProject()
+{
+ std::cout << "in constructor1" << std::endl;
+ this->appli = NULL;
+ this->lib = NULL;
+ this->CMakeLists = NULL;
+}
+
+modelCDMProject::modelCDMProject(
+ const std::string& path,
+ const std::string& buildPath
+)
+{
+ this->path = CDMUtilities::fixPath(path);
+ //open makelists file
+ std::string pathFixed(CDMUtilities::fixPath(path));
+
+ //TODO: set pathMakeLists for windows
+ std::string pathMakeLists = pathFixed + "/CMakeLists.txt";
+
+ std::ifstream confFile;
+ confFile.open((pathMakeLists).c_str());
+
+ std::string word;
+ while(confFile.is_open() && !confFile.eof())
+ {
+ //std::cout << "leyendo " << word << std::endl;
+ //get project name
+ std::getline(confFile,word,'(');
+ std::vector<std::string> wordBits;
+ CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
+
+ if(wordBits[wordBits.size()-1] == "PROJECT")
+ {
+ std::getline(confFile,word,')');
+ std::vector<std::string> nameBits;
+ CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
+
+ this->name = this->nameProject = "";
+ for (int i = 0; i < nameBits.size(); i++)
+ {
+ if(i != 0)
+ this->name += " ";
+ this->name += nameBits[i];
+ }
+ this->nameProject = this->name;
+
+ }
+
+
+ if(wordBits[wordBits.size()-1] == "SET")
+ {
+ //get project version
+ std::getline(confFile,word,')');
+ CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
+ if(wordBits[0] == "PROJECT_MAJOR_VERSION")
+ {
+ version = wordBits[1];
+ }
+ if(wordBits[0] == "PROJECT_MINOR_VERSION")
+ {
+ version += "." + wordBits[1];
+ }
+ if(wordBits[0] == "PROJECT_BUILD_VERSION")
+ {
+ version += "." + wordBits[1];
+ }
+
+ //get project versionDate
+ if(wordBits[0] == "PROJECT_VERSION_DATE")
+ {
+ std::vector<std::string> versionBits;
+ CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
+ versionDate = versionBits[0];
+ }
+ //get project buildPath
+
+ if (buildPath != "")
+ {
+ this->buildPath = buildPath;
+ }
+ else
+ {
+ this->buildPath = this->path + "Bin";
+ }
+ }
+ }
+ confFile.close();
+
+ this->type = wxDIR_DIRS;
+ this->level = 0;
+
+ this->children.clear();
+ this->appli = NULL;
+ this->lib = NULL;
+ this->packages.clear();
+
+
+ //check all folders
+ wxDir dir(crea::std2wx((pathFixed).c_str()));
+ if (dir.IsOpened())
+ {
+ wxString fileName;
+ bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
+ while (cont)
+ {
+ std::string stdfileName = crea::wx2std(fileName);
+
+ //if appli, create appli
+ if(stdfileName == "appli")
+ {
+ this->appli = new modelCDMAppli(pathFixed + "/appli", this->level + 1);
+ this->children.push_back(this->appli);
+ }
+ //if lib, create lib
+ else if(stdfileName == "lib")
+ {
+ this->lib = new modelCDMLib(pathFixed + "/lib", this->level + 1);
+ this->children.push_back(this->lib);
+ }
+ //if package , create package
+ else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
+ {
+ modelCDMPackage* package = new modelCDMPackage(pathFixed + "/" + stdfileName, this->level + 1);
+ this->packages.push_back(package);
+ this->children.push_back(package);
+ }
+ //if is an unknown folder, create folder
+ else
+ {
+ this->children.push_back(new modelCDMFolder(pathFixed + "/" + stdfileName, this->level + 1));
+ }
+
+ cont = dir.GetNext(&fileName);
+ }
+
+ cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
+ while (cont)
+ {
+ std::string stdfileName = crea::wx2std(fileName);
+
+ //if CMakeLists, create CMakeLists
+ if(stdfileName == "CMakeLists.txt")
+ {
+ this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + "/" + stdfileName, this->level + 1);
+ this->children.push_back(this->CMakeLists);
+ }
+ else
+ {
+ this->children.push_back(new modelCDMFile(pathFixed + "/" + stdfileName, this->level + 1));
+ }
+ //if is an unknown file, create file
+ cont = dir.GetNext(&fileName);
+ }
+ }
+
+ this->SortChildren();
+
+}
+
+modelCDMProject::~modelCDMProject()
+{
+}
+
+const std::string& modelCDMProject::GetNameProject() const
+{
+ return this->nameProject;
+}
+
+const std::string& modelCDMProject::GetVersion() const
+{
+ return this->version;
+}
+
+const std::string& modelCDMProject::GetVersionDate() const
+{
+ return this->versionDate;
+}
+
+const std::string& modelCDMProject::GetBuildPath() const
+{
+ return this->buildPath;
+}
+
+bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
+{
+ //TODO: implement method
+ return true;
+}
+
+bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
+{
+ //TODO: implement method
+ return true;
+}
+
+modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
+ const std::string& name,
+ std::string*& result,
+ const std::string& authors,
+ const std::string& authorsEmail,
+ const std::string& description,
+ const std::string& version
+)
{
- this->name = name;
- this->path = path;
- this->type = type;
- this->level = level;
+ //fixing input parameters
+ std::vector<std::string> words;
+
+ CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
+ std::string nameFixed = "";
+ for (int i = 0; i < words.size(); i++)
+ {
+ nameFixed += words[i];
+ }
+
+ words.clear();
+ CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
+ std::string authorFixed;
+ for (int i = 0; i < words.size(); i++)
+ {
+ authorFixed += words[i];
+ }
+
+ words.clear();
+ std::string descriptionFixed;
+ CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
+ for (int i = 0; i < words.size(); i++)
+ {
+ descriptionFixed += words[i];
+ }
+ words.clear();
+ CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
+ for (int i = 0; i < words.size(); i++)
+ {
+ descriptionFixed += "_" + words[i];
+ }
+
+ //call project to create package : use bbCreatePackage <path> <name> [author] [description]
+ std::string creationCommand = "bbCreatePackage \"" + this->path + "\" " + nameFixed + " " + authorFixed + " " + descriptionFixed;
+ //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
+ if(system(creationCommand.c_str()))
+ {
+ result = new std::string("An error occurred while running '" + creationCommand + "'.");
+ return NULL;
+ }
+
+ //add library to model
+ //TODO: fix for windows
+ modelCDMPackage* package = new modelCDMPackage(this->path + "/bbtk_" + nameFixed + "_PKG", this->level + 1);
+ this->packages.push_back(package);
+ this->children.push_back(package);
+
+ //TODO: set package version
+
+ this->SortChildren();
+
+ result = new std::string(this->path + "/" + name);
+ return package;
+}
+
+modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
+ const std::string& name,
+ std::string*& result,
+ const std::string& path
+)
+{
+ if(this->lib != NULL)
+ {
+ return this->lib->CreateLibrary(name, result);
+ }
+ result = new std::string("there is no lib folder in this project.");
+ return NULL;
+}
+
+modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
+ const std::string& name,
+ std::string*& result,
+ const std::string& path
+)
+{
+ if(this->appli != NULL)
+ {
+ return this->appli->CreateApplication(name, result);
+ }
+ result = new std::string("there is no appli folder in this project.");
+ return NULL;
+}
+
+modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
+ const std::string& name,
+ const std::string& package,
+ const std::string& authors,
+ const std::string& authorsEmail,
+ const std::string& categories,
+ const std::string& description
+)
+{
+ //TODO: implement method
+ return NULL;
+}
+
+bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
+{
+ //TODO: implement method
+ return true;
+}
+
+const bool modelCDMProject::Refresh(std::string*& result)
+{
+ std::cout << "refreshing project" << std::endl;
+ //open makelists file
+ //TODO: set pathMakeLists for windows
+ std::string pathMakeLists = this->path + "/CMakeLists.txt";
+
+ std::ifstream confFile;
+ confFile.open((pathMakeLists).c_str());
+
+ std::string word;
+ while(confFile.is_open() && !confFile.eof())
+ {
+ //std::cout << "leyendo " << word << std::endl;
+ //get project name
+ std::getline(confFile,word,'(');
+ std::vector<std::string> wordBits;
+ CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
+
+ if(wordBits[wordBits.size()-1] == "PROJECT")
+ {
+ std::getline(confFile,word,')');
+ std::vector<std::string> nameBits;
+ CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
+
+ this->name = this->nameProject = "";
+ for (int i = 0; i < nameBits.size(); i++)
+ {
+ if(i != 0)
+ this->name += " ";
+ this->name += nameBits[i];
+ }
+ this->nameProject = this->name;
+
+ }
+
+
+ if(wordBits[wordBits.size()-1] == "SET")
+ {
+ //get project version
+ std::getline(confFile,word,')');
+ CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
+ if(wordBits[0] == "PROJECT_MAJOR_VERSION")
+ {
+ version = wordBits[1];
+ }
+ if(wordBits[0] == "PROJECT_MINOR_VERSION")
+ {
+ version += "." + wordBits[1];
+ }
+ if(wordBits[0] == "PROJECT_BUILD_VERSION")
+ {
+ version += "." + wordBits[1];
+ }
+
+ //get project versionDate
+ if(wordBits[0] == "PROJECT_VERSION_DATE")
+ {
+ std::vector<std::string> versionBits;
+ CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
+ versionDate = versionBits[0];
+ }
+ }
+ }
+ confFile.close();
+
+ this->type = wxDIR_DIRS;
+ this->level = 0;
+
+ std::vector<bool> checked(this->children.size(), false);
+ std::vector<bool> checkedPackages(this->packages.size(), false);
+
+ //check all folders
+ wxDir dir(crea::std2wx((this->path).c_str()));
+ if (dir.IsOpened())
+ {
+ wxString fileName;
+ bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
+ while (cont)
+ {
+ std::string stdfileName = crea::wx2std(fileName);
+
+ //if appli, create appli
+ if(stdfileName == "appli")
+ {
+ if (this->appli == NULL)
+ {
+ this->appli = new modelCDMAppli(this->path + "/appli", this->level + 1);
+ this->children.push_back(this->appli);
+ }
+ else
+ {
+ int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
+ checked[pos] = true;
+ if(!this->appli->Refresh(result))
+ return false;
+ }
+ }
+ //if lib, create lib
+ else if(stdfileName == "lib")
+ {
+ if (this->lib == NULL)
+ {
+ this->lib = new modelCDMLib(this->path + "/lib", this->level + 1);
+ this->children.push_back(this->lib);
+ }
+ else
+ {
+ int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
+ checked[pos] = true;
+ if(!this->lib->Refresh(result))
+ return false;
+ }
+
+ }
+ //if package , create package
+ else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
+ {
+ std::string packageName = stdfileName.substr(5, stdfileName.size()-9);
+ bool found = false;
+ for (int i = 0;!found && i < this->packages.size(); i++)
+ {
+ if (this->packages[i]->GetName() == packageName)
+ {
+ found = true;
+ int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
+ checked[pos] = true;
+ checkedPackages[i] = true;
+ if(!this->packages[i]->Refresh(result))
+ return false;
+ }
+ }
+ if(!found)
+ {
+ modelCDMPackage* package = new modelCDMPackage(this->path + "/" + stdfileName, this->level + 1);
+ this->packages.push_back(package);
+ this->children.push_back(package);
+ }
+
+ }
+ //if is an unknown folder, create folder
+ else
+ {
+ bool found = false;
+ for (int i = 0; !found && i < this->children.size(); i++)
+ {
+ if (this->children[i]->GetName() == stdfileName)
+ {
+ found = true;
+ checked[i] = true;
+ if(!this->children[i]->Refresh(result))
+ return false;
+ }
+ }
+
+ if(!found)
+ {
+ modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
+ this->children.push_back(folder);
+ }
+ }
+
+ cont = dir.GetNext(&fileName);
+ }
+
+ cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
+ while (cont)
+ {
+ std::string stdfileName = crea::wx2std(fileName);
+
+ //if CMakeLists, create CMakeLists
+ if(stdfileName == "CMakeLists.txt")
+ {
+ if (this->CMakeLists == NULL)
+ {
+ this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
+ this->children.push_back(this->CMakeLists);
+ }
+ else
+ {
+ int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
+ checked[pos] = true;
+ if(!this->CMakeLists->Refresh(result))
+ return false;
+ }
+ }
+ //if is an unknown file, create file
+ else
+ {
+ bool found = false;
+ for (int i = 0; i <!found && this->children.size(); i++)
+ {
+ if (this->children[i]->GetName() == stdfileName)
+ {
+ found = true;
+ checked[i] = true;
+ if(!this->children[i]->Refresh(result))
+ return false;
+ }
+ }
+
+ if(!found)
+ {
+ modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
+ this->children.push_back(file);
+ }
+ }
+
+ cont = dir.GetNext(&fileName);
+ }
+ }
+
+ for (int i = 0; i < checkedPackages.size(); i++)
+ {
+ if(!checkedPackages[i])
+ {
+ this->packages.erase(this->packages.begin()+i);
+ checkedPackages.erase(checkedPackages.begin()+i);
+ i--;
+ }
+ }
+ for (int i = 0; i < checked.size(); i++)
+ {
+ if(!checked[i])
+ {
+ delete this->children[i];
+ this->children.erase(this->children.begin()+i);
+ checked.erase(checked.begin()+i);
+ i--;
+ }
+ }
+ this->SortChildren();
+ return true;
}
+bool modelCDMProject::ConfigureBuild(std::string*& result)
+{
+ //TODO: implement method
+ return true;
+}
+
+bool modelCDMProject::Build(std::string*& result)
+{
+ //TODO: implement method
+ return true;
+}
+
+bool modelCDMProject::Connect(std::string*& result)
+{
+ //TODO: implement method
+ return true;
+}