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 28, 2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "modelCDMFolder.h"
43 #include "CDMUtilities.h"
45 modelCDMFolder::modelCDMFolder()
47 this->CMakeLists = NULL;
50 modelCDMFolder::modelCDMFolder(const std::string& path, const int& level)
53 this->children.clear();
55 this->CMakeLists = NULL;
58 std::vector<std::string> words;
59 std::string delimiters;
60 //TODO::fix for windows
62 CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
63 this->name = words[words.size()-1];
66 this->type = wxDIR_DIRS;
68 std::string pathFixed(CDMUtilities::fixPath(path));
70 wxDir dir(crea::std2wx((pathFixed).c_str()));
74 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
77 std::string stdfileName = crea::wx2std(fileName);
79 //if is an unknown folder, create folder
80 this->children.push_back(new modelCDMFolder(pathFixed + "/" + stdfileName, this->level + 1));
82 cont = dir.GetNext(&fileName);
85 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
88 std::string stdfileName = crea::wx2std(fileName);
90 //if CMakeLists, create CMakeLists
91 if(stdfileName == "CMakeLists.txt")
93 this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + "/" + stdfileName, this->level + 1);
94 this->children.push_back(this->CMakeLists);
98 this->children.push_back(new modelCDMFile(pathFixed + "/" + stdfileName, this->level + 1));
100 //if is an unknown file, create file
101 cont = dir.GetNext(&fileName);
105 this->SortChildren();
108 modelCDMFolder::~modelCDMFolder()
110 this->folders.clear();
111 this->CMakeLists = NULL;
112 for (int i = 0; i < this->children.size(); i++)
114 if(this->children[i] != NULL)
116 delete this->children[i];
117 this->children[i] = NULL;
120 this->children.clear();
123 modelCDMFolder* modelCDMFolder::CreateFolder(const std::string& name, std::string*& result)
125 //TODO:: mkdir depending on OS
126 std::string command = "mkdir " + path + "/" + name;
127 if(system(command.c_str()))
129 result = new std::string("Error executing: " + command + ".");
132 modelCDMFolder* folder = new modelCDMFolder(path + "/" + name, level + 1);
133 this->folders.push_back(folder);
134 this->children.push_back(folder);
139 bool modelCDMFolder::OpenCMakeListsFile(std::string*& result)
141 if (this->CMakeLists == NULL)
143 result = new std::string("There's no CMakeLists file to open.");
146 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
150 result = new std::string("Couldn't open CMakeLists file.");
155 const bool modelCDMFolder::Refresh(std::string*& result)
158 this->type = wxDIR_DIRS;
161 std::vector<bool> checked(this->children.size(), false);
162 std::vector<bool> checkedFolders(this->folders.size(), false);
165 wxDir dir(crea::std2wx((this->path).c_str()));
169 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
172 std::string stdfileName = crea::wx2std(fileName);
173 std::string folderName = stdfileName;
174 //check if they already exist
176 for (int i = 0;!found && i < this->folders.size(); i++)
178 if (this->folders[i]->GetName() == folderName)
181 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
183 checkedFolders[i] = true;
184 if(!this->folders[i]->Refresh(result))
190 modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
191 this->folders.push_back(folder);
192 this->children.push_back(folder);
194 cont = dir.GetNext(&fileName);
197 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
200 std::string stdfileName = crea::wx2std(fileName);
202 //if CMakeLists, create CMakeLists
203 if(stdfileName == "CMakeLists.txt")
205 if (this->CMakeLists == NULL)
207 this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
208 this->children.push_back(this->CMakeLists);
212 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
214 if(!this->CMakeLists->Refresh(result))
218 //if is an unknown file, create file
222 for (int i = 0; i <!found && this->children.size(); i++)
224 if (this->children[i]->GetName() == stdfileName)
228 if(!this->children[i]->Refresh(result))
235 modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
236 this->children.push_back(file);
240 cont = dir.GetNext(&fileName);
244 for (int i = 0; i < checkedFolders.size(); i++)
246 if(!checkedFolders[i])
248 this->folders.erase(this->folders.begin()+i);
249 checkedFolders.erase(checkedFolders.begin()+i);
253 for (int i = 0; i < checked.size(); i++)
257 delete this->children[i];
258 this->children.erase(this->children.begin()+i);
259 checked.erase(checked.begin()+i);
263 this->SortChildren();
267 modelCDMCMakeListsFile* modelCDMFolder::GetCMakeLists() const
269 return this->CMakeLists;
272 std::vector<modelCDMFolder*> modelCDMFolder::GetFolders() const
274 return this->folders;
278 modelCDMFolder::HasCMakeLists()
280 return this->CMakeLists != NULL;