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 # ------------------------------------------------------------------------
29 * modelCDMApplication.cpp
31 * Created on: Nov 23, 2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "modelCDMApplication.h"
40 #include "CDMUtilities.h"
44 modelCDMApplication::modelCDMApplication()
48 modelCDMApplication::modelCDMApplication(const std::string& path, const std::string& name, const int& level)
53 this->path = CDMUtilities::fixPath(path);
55 this->type = wxDIR_DIRS;
59 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
61 std::ifstream confFile;
62 confFile.open((pathMakeLists).c_str());
65 while(confFile.is_open() && !confFile.eof())
68 std::getline(confFile,word,'(');
69 std::vector<std::string> wordBits;
70 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
72 if(wordBits[wordBits.size()-1] == "SET")
75 std::getline(confFile,word,')');
76 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
77 if(wordBits[0] == "EXE_NAME")
80 for (int i = 2; i < wordBits.size(); i++)
82 word += " " + wordBits[i];
85 this->executableName = word;
91 //add library contents
93 this->children.clear();
94 wxDir dir(crea::std2wx((this->path).c_str()));
99 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
102 std::string stdfileName = crea::wx2std(fileName);
104 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
105 this->folders.push_back(folder);
106 this->children.push_back(folder);
108 cont = dir.GetNext(&fileName);
111 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
114 std::string stdfileName = crea::wx2std(fileName);
116 //if CMakeLists, create CMakeLists
117 if(stdfileName == "CMakeLists.txt")
119 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
120 this->children.push_back(this->CMakeLists);
122 //if is an unknown file, create file
125 this->children.push_back(new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
128 cont = dir.GetNext(&fileName);
131 this->SortChildren();
134 modelCDMApplication::~modelCDMApplication()
138 const std::string& modelCDMApplication::GetExecutableName() const
140 return this->executableName;
143 bool modelCDMApplication::SetExecutableName(const std::string& fileName, std::string*& result)
145 std::vector<std::string> words;
146 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
147 std::string fileNameReal = words[0];
148 for (int i = 1; i < words.size(); i++)
150 fileNameReal += "-" + words[i];
154 //opening original cmakelists
155 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
158 result = new std::string("CMakeLists.txt file failed to open.");
161 //opening temporal cmakelists
162 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
165 result = new std::string("CMakeLists.txt.tmp file failed to open.");
168 //copying contents from original to temporal and making changes
169 while (getline(in, line))
171 if(line.find("SET ( EXE_NAME") != std::string::npos)
172 line = "SET ( EXE_NAME " + fileNameReal + " )";
173 out << line << std::endl;
177 //delete old file and rename new file
178 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
179 if(system(renameCommand.c_str()))
181 result = new std::string("An error occurred while running '" + renameCommand + "'.");
185 this->executableName = fileNameReal;
189 modelCDMFolder* modelCDMApplication::CreateFolder(const std::string& name, std::string*& result)
191 //TODO:: mkdir depending on OS
192 std::string command = "mkdir " + path + CDMUtilities::SLASH + name;
193 if(system(command.c_str()))
195 result = new std::string("Error executing: " + command + ".");
198 modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
199 this->folders.push_back(folder);
200 this->children.push_back(folder);
205 const bool modelCDMApplication::Refresh(std::string*& result)
208 this->type = wxDIR_DIRS;
211 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
213 std::ifstream confFile;
214 confFile.open((pathMakeLists).c_str());
217 while(confFile.is_open() && !confFile.eof())
220 std::getline(confFile,word,'(');
221 std::vector<std::string> wordBits;
222 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
224 if(wordBits[wordBits.size()-1] == "SET")
227 std::getline(confFile,word,')');
228 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
229 if(wordBits[0] == "EXE_NAME")
232 for (int i = 2; i < wordBits.size(); i++)
234 word += " " + wordBits[i];
237 this->executableName = word;
245 std::vector<bool> checked(this->children.size(), false);
246 std::vector<bool> checkedFolders(this->folders.size(), false);
249 wxDir dir(crea::std2wx((this->path).c_str()));
253 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
256 std::string stdfileName = crea::wx2std(fileName);
257 std::string applicationName = stdfileName;
258 //check if they already exist
260 for (int i = 0;!found && i < this->folders.size(); i++)
262 if (this->folders[i]->GetName() == applicationName)
265 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
267 checkedFolders[i] = true;
268 if(!this->folders[i]->Refresh(result))
274 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
275 this->folders.push_back(folder);
276 this->children.push_back(folder);
278 cont = dir.GetNext(&fileName);
281 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
284 std::string stdfileName = crea::wx2std(fileName);
286 //if CMakeLists, create CMakeLists
287 if(stdfileName == "CMakeLists.txt")
289 if (this->CMakeLists == NULL)
291 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
292 this->children.push_back(this->CMakeLists);
296 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
298 if(!this->CMakeLists->Refresh(result))
302 //if is an unknown file, create file
306 for (int i = 0; i <!found && this->children.size(); i++)
308 if (this->children[i]->GetName() == stdfileName)
312 if(!this->children[i]->Refresh(result))
319 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
320 this->children.push_back(file);
324 cont = dir.GetNext(&fileName);
328 for (int i = 0; i < checkedFolders.size(); i++)
330 if(!checkedFolders[i])
332 this->folders.erase(this->folders.begin()+i);
333 checkedFolders.erase(checkedFolders.begin()+i);
337 for (int i = 0; i < checked.size(); i++)
341 delete this->children[i];
342 this->children.erase(this->children.begin()+i);
343 checked.erase(checked.begin()+i);
347 this->SortChildren();