+/*
+# ---------------------------------------------------------------------
+#
+# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
+# 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
+#
+# This software is governed by the CeCILL-B license under French law and
+# abiding by the rules of distribution of free software. You can use,
+# modify and/ or redistribute the software under the terms of the CeCILL-B
+# license as circulated by CEA, CNRS and INRIA at the following URL
+# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+# or in the file LICENSE.txt.
+#
+# As a counterpart to the access to the source code and rights to copy,
+# modify and redistribute granted by the license, users are provided only
+# with a limited warranty and the software's author, the holder of the
+# economic rights, and the successive licensors have only limited
+# liability.
+#
+# 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.
+# ------------------------------------------------------------------------
+ */
+
+/*
+ * modelCDMFolder.cpp
+ *
+ * Created on: Nov 28, 2012
+ * Author: Daniel Felipe Gonzalez Obando
+ */
+
+#include "modelCDMPackageSrc.h"
+
+#include <fstream>
+#include <algorithm>
+
+#include <creaWx.h>
+#include <wx/dir.h>
+
+#include "CDMUtilities.h"
+
+modelCDMPackageSrc::modelCDMPackageSrc()
+{
+ this->CMakeLists = NULL;
+}
+
+modelCDMPackageSrc::modelCDMPackageSrc(const std::string& path, const int& level)
+{
+ //set attributes
+ this->children.clear();
+ this->level = level;
+ this->CMakeLists = NULL;
+ this->length = 0;
+ this->name = "src";
+ this->path = CDMUtilities::fixPath(path);
+ this->type = wxDIR_DIRS;
+
+ //check all folders
+ wxDir dir(crea::std2wx(path));
+ if (dir.IsOpened())
+ {
+ wxString fileName;
+ bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
+ while (cont)
+ {
+ std::string stdfileName = crea::wx2std(fileName);
+
+ //if is an unknown folder, create folder
+ this->children.push_back(new modelCDMFolder(path + "/" + stdfileName, this->level + 1));
+
+ cont = dir.GetNext(&fileName);
+ }
+
+ cont = dir.GetFirst(&fileName, wxT("CMakeLists.txt"), wxDIR_FILES);
+ if (cont)
+ {
+ std::string stdfileName = crea::wx2std(fileName);
+ this->CMakeLists = new modelCDMCMakeListsFile(path + "/" + stdfileName, this->level + 1);
+ this->children.push_back(this->CMakeLists);
+ }
+
+ cont = dir.GetFirst(&fileName, wxT("*.h"), wxDIR_FILES);
+ while (cont)
+ {
+ std::string stdfileName = crea::wx2std(fileName);
+ modelCDMFile* file;
+
+ if(stdfileName.substr(0,2) == "bb")
+ {
+ file = new modelCDMFile(path + "/" + stdfileName, this->level + 1);
+ this->children.push_back(file);
+ modelCDMBlackBox* blackBox = new modelCDMBlackBox(stdfileName, path, level + 1);
+ blackBox->SetHeaderFile(file);
+ cont = dir.GetFirst(&fileName, crea::std2wx(stdfileName.substr(0,stdfileName.size()-2) + ".cxx"), wxDIR_FILES);
+ if (cont)
+ {
+ file = new modelCDMFile(path + "/" + crea::wx2std(fileName), this->level + 1);
+ this->children.push_back(file);
+ blackBox->SetSourceFile(file);
+ }
+ this->blackBoxes.push_back(blackBox);
+ }
+ cont = dir.GetNext(&fileName);
+ }
+ }
+
+ this->SortChildren();
+}
+
+modelCDMPackageSrc::~modelCDMPackageSrc()
+{
+ for (int i = 0; i < this->blackBoxes.size(); i++)
+ {
+ if(this->blackBoxes[i] != NULL)
+ {
+ delete this->blackBoxes[i];
+ this->blackBoxes[i] = NULL;
+ }
+ }
+ this->blackBoxes.clear();
+}
+
+const std::vector<modelCDMBlackBox*>& modelCDMPackageSrc::GetBlackBoxes() const
+{
+ return this->blackBoxes;
+}
+
+const bool modelCDMPackageSrc::Refresh(std::string*& result)
+{
+ //set attributes
+ this->type = wxDIR_DIRS;
+
+ //check children
+ std::vector<bool> checked(this->children.size(), false);
+ std::vector<bool> checkedBoxes(this->blackBoxes.size(), false);
+
+ //check all boxes
+ 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);
+ std::string folderName = stdfileName;
+ //check if they already exist
+ bool found = false;
+ for (int i = 0;!found && i < this->children.size(); i++)
+ {
+ if (this->children[i]->GetName() == folderName)
+ {
+ 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);
+ }
+ }
+
+ //if is a Black Box header, check in black boxes
+ if(stdfileName.substr(stdfileName.size() - 2, 2) == ".h" && stdfileName.substr(0,2) == "bb")
+ {
+ bool found = false;
+ for (int i = 0; i < this->blackBoxes.size(); i++)
+ {
+ if(this->blackBoxes[i]->GetHeaderFile()->GetName() == stdfileName)
+ {
+ checkedBoxes[i] = true;
+ found = true;
+ if(!this->blackBoxes[i]->Refresh(result))
+ return false;
+ break;
+ }
+ }
+
+ if (!found)
+ {
+ modelCDMBlackBox* blackBox = new modelCDMBlackBox(stdfileName, path + "/" + stdfileName, level + 1);
+ this->blackBoxes.push_back(blackBox);
+ }
+
+ }
+
+ cont = dir.GetNext(&fileName);
+ }
+ }
+
+ for (int i = 0; i < checkedBoxes.size(); i++)
+ {
+ if(!checkedBoxes[i])
+ {
+ delete this->blackBoxes[i];
+ this->blackBoxes.erase(this->blackBoxes.begin()+i);
+ checkedBoxes.erase(checkedBoxes.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;
+}