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 int& level)
51 std::vector<std::string> words;
52 std::string delimiters;
53 //TODO::fix for windows
55 CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
56 this->name = words[words.size()-1];
59 this->path = CDMUtilities::fixPath(path);
61 this->type = wxDIR_DIRS;
66 std::string pathMakeLists = path + "/CMakeLists.txt";
68 std::ifstream confFile;
69 confFile.open((pathMakeLists).c_str());
72 while(confFile.is_open() && !confFile.eof())
75 std::getline(confFile,word,'(');
76 std::vector<std::string> wordBits;
77 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
79 if(wordBits[wordBits.size()-1] == "SET")
82 std::getline(confFile,word,')');
83 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
84 if(wordBits[0] == "EXE_NAME")
87 for (int i = 2; i < wordBits.size(); i++)
89 word += " " + wordBits[i];
92 this->nameApplication = this->executableName = word;
98 //add library contents
100 this->children.clear();
101 wxDir dir(crea::std2wx((this->path).c_str()));
106 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
109 std::string stdfileName = crea::wx2std(fileName);
111 modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
112 this->folders.push_back(folder);
113 this->children.push_back(folder);
115 cont = dir.GetNext(&fileName);
118 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
121 std::string stdfileName = crea::wx2std(fileName);
123 //if CMakeLists, create CMakeLists
124 if(stdfileName == "CMakeLists.txt")
126 this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
127 this->children.push_back(this->CMakeLists);
129 //if is an unknown file, create file
132 this->children.push_back(new modelCDMFile(this->path + "/" + stdfileName, this->level + 1));
135 cont = dir.GetNext(&fileName);
138 this->SortChildren();
141 modelCDMApplication::~modelCDMApplication()
145 const std::string& modelCDMApplication::GetNameApplication() const
147 return this->nameApplication;
150 const std::string& modelCDMApplication::GetExecutableName() const
152 return this->executableName;
155 bool modelCDMApplication::SetExecutableName(const std::string& fileName, std::string*& result)
157 std::vector<std::string> words;
158 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
159 std::string fileNameReal = words[0];
160 for (int i = 1; i < words.size(); i++)
162 fileNameReal += "-" + words[i];
166 //opening original cmakelists
167 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
170 result = new std::string("CMakeLists.txt file failed to open.");
173 //opening temporal cmakelists
174 std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
177 result = new std::string("CMakeLists.txt.tmp file failed to open.");
180 //copying contents from original to temporal and making changes
181 while (getline(in, line))
183 if(line.find("SET ( EXE_NAME") != std::string::npos)
184 line = "SET ( EXE_NAME " + fileNameReal + " )";
185 out << line << std::endl;
189 //delete old file and rename new file
190 std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
191 if(system(renameCommand.c_str()))
193 result = new std::string("An error occurred while running '" + renameCommand + "'.");
197 this->executableName = this->nameApplication = fileNameReal;
201 modelCDMFolder* modelCDMApplication::CreateFolder(const std::string& name, std::string*& result)
203 //TODO:: mkdir depending on OS
204 std::string command = "mkdir " + path + "/" + name;
205 if(system(command.c_str()))
207 result = new std::string("Error executing: " + command + ".");
210 modelCDMFolder* folder = new modelCDMFolder(path + "/" + name, level + 1);
211 this->folders.push_back(folder);
212 this->children.push_back(folder);
217 const bool modelCDMApplication::Refresh(std::string*& result)
220 this->type = wxDIR_DIRS;
223 std::string pathMakeLists = path + "/CMakeLists.txt";
225 std::ifstream confFile;
226 confFile.open((pathMakeLists).c_str());
229 while(confFile.is_open() && !confFile.eof())
232 std::getline(confFile,word,'(');
233 std::vector<std::string> wordBits;
234 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
236 if(wordBits[wordBits.size()-1] == "SET")
239 std::getline(confFile,word,')');
240 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
241 if(wordBits[0] == "LIBRARY_NAME")
244 for (int i = 2; i < wordBits.size(); i++)
246 word += " " + wordBits[i];
249 this->nameApplication = this->executableName = word;
257 std::vector<bool> checked(this->children.size(), false);
258 std::vector<bool> checkedFolders(this->folders.size(), false);
261 wxDir dir(crea::std2wx((this->path).c_str()));
265 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
268 std::string stdfileName = crea::wx2std(fileName);
269 std::string applicationName = stdfileName;
270 //check if they already exist
272 for (int i = 0;!found && i < this->folders.size(); i++)
274 if (this->folders[i]->GetName() == applicationName)
277 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
279 checkedFolders[i] = true;
280 if(!this->folders[i]->Refresh(result))
286 modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
287 this->folders.push_back(folder);
288 this->children.push_back(folder);
290 cont = dir.GetNext(&fileName);
293 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
296 std::string stdfileName = crea::wx2std(fileName);
298 //if CMakeLists, create CMakeLists
299 if(stdfileName == "CMakeLists.txt")
301 if (this->CMakeLists == NULL)
303 this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
304 this->children.push_back(this->CMakeLists);
308 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
310 if(!this->CMakeLists->Refresh(result))
314 //if is an unknown file, create file
318 for (int i = 0; i <!found && this->children.size(); i++)
320 if (this->children[i]->GetName() == stdfileName)
324 if(!this->children[i]->Refresh(result))
331 modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
332 this->children.push_back(file);
336 cont = dir.GetNext(&fileName);
340 for (int i = 0; i < checkedFolders.size(); i++)
342 if(!checkedFolders[i])
344 this->folders.erase(this->folders.begin()+i);
345 checkedFolders.erase(checkedFolders.begin()+i);
349 for (int i = 0; i < checked.size(); i++)
353 delete this->children[i];
354 this->children.erase(this->children.begin()+i);
355 checked.erase(checked.begin()+i);
359 this->SortChildren();