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)
50 std::cout << "creating application: " + path + "\n";
54 this->path = CDMUtilities::fixPath(path);
56 this->type = wxDIR_DIRS;
60 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
62 std::ifstream confFile;
63 confFile.open((pathMakeLists).c_str());
66 while(confFile.is_open() && !confFile.eof())
69 std::getline(confFile,word,'(');
70 std::vector<std::string> wordBits;
71 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
73 if(wordBits[wordBits.size()-1] == "SET")
76 std::getline(confFile,word,')');
77 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
78 if(wordBits[0] == "EXE_NAME")
81 for (int i = 2; i < wordBits.size(); i++)
83 word += " " + wordBits[i];
86 this->executableName = word;
92 //add library contents
94 this->children.clear();
95 wxDir dir(crea::std2wx((this->path).c_str()));
100 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
103 std::string stdfileName = crea::wx2std(fileName);
105 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
106 this->folders.push_back(folder);
107 this->children.push_back(folder);
109 cont = dir.GetNext(&fileName);
112 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
115 std::string stdfileName = crea::wx2std(fileName);
117 //if CMakeLists, create CMakeLists
118 if(stdfileName == "CMakeLists.txt")
120 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
121 this->children.push_back(this->CMakeLists);
123 //if is an unknown file, create file
126 this->children.push_back(new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
129 cont = dir.GetNext(&fileName);
132 this->SortChildren();
133 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
136 modelCDMApplication::~modelCDMApplication()
140 const std::string& modelCDMApplication::GetExecutableName() const
142 return this->executableName;
145 bool modelCDMApplication::SetExecutableName(const std::string& fileName, std::string*& result)
147 std::vector<std::string> words;
148 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
149 std::string fileNameReal = words[0];
150 for (int i = 1; i < words.size(); i++)
152 fileNameReal += "-" + words[i];
156 //opening original cmakelists
157 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
160 result = new std::string("CMakeLists.txt file failed to open.");
163 //opening temporal cmakelists
164 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
167 result = new std::string("CMakeLists.txt.tmp file failed to open.");
170 //copying contents from original to temporal and making changes
171 while (getline(in, line))
173 if(line.find("SET ( EXE_NAME") != std::string::npos)
174 line = "SET ( EXE_NAME " + fileNameReal + " )";
175 out << line << std::endl;
179 //delete old file and rename new file
180 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
181 if(system(renameCommand.c_str()))
183 result = new std::string("An error occurred while running '" + renameCommand + "'.");
187 this->executableName = fileNameReal;
191 modelCDMFolder* modelCDMApplication::CreateFolder(const std::string& name, std::string*& result)
193 //TODO:: mkdir depending on OS
194 std::string command = "mkdir " + path + CDMUtilities::SLASH + name;
195 if(system(command.c_str()))
197 result = new std::string("Error executing: " + command + ".");
200 modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
201 this->folders.push_back(folder);
202 this->children.push_back(folder);
207 const bool modelCDMApplication::Refresh(std::string*& result)
209 std::cout << "refreshing application: " << this->executableName << std::endl;
211 this->type = wxDIR_DIRS;
214 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
216 std::ifstream confFile;
217 confFile.open((pathMakeLists).c_str());
220 while(confFile.is_open() && !confFile.eof())
223 std::getline(confFile,word,'(');
224 std::vector<std::string> wordBits;
225 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
227 if(wordBits[wordBits.size()-1] == "SET")
230 std::getline(confFile,word,')');
231 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
232 if(wordBits[0] == "EXE_NAME")
235 for (int i = 2; i < wordBits.size(); i++)
237 word += " " + wordBits[i];
240 this->executableName = word;
248 std::vector<bool> checked(this->children.size(), false);
249 std::vector<bool> checkedFolders(this->folders.size(), false);
252 wxDir dir(crea::std2wx((this->path).c_str()));
256 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
259 std::string stdfileName = crea::wx2std(fileName);
260 std::string applicationName = stdfileName;
261 //check if they already exist
263 for (int i = 0; !found && i < this->folders.size(); i++)
265 if (this->folders[i]->GetName() == applicationName)
268 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
270 checkedFolders[i] = true;
271 if(!this->folders[i]->Refresh(result))
277 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
278 this->folders.push_back(folder);
279 this->children.push_back(folder);
281 cont = dir.GetNext(&fileName);
284 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
287 std::string stdfileName = crea::wx2std(fileName);
289 //if CMakeLists, create CMakeLists
290 if(stdfileName == "CMakeLists.txt")
292 if (this->CMakeLists == NULL)
294 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
295 this->children.push_back(this->CMakeLists);
299 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
301 if(!this->CMakeLists->Refresh(result))
305 //if is an unknown file, create file
309 for (int i = 0; !found && i < this->children.size(); i++)
311 if (this->children[i]->GetName() == stdfileName)
315 if(!this->children[i]->Refresh(result))
322 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
323 this->children.push_back(file);
327 cont = dir.GetNext(&fileName);
331 for (int i = 0; i < checkedFolders.size(); i++)
333 if(!checkedFolders[i])
335 this->folders.erase(this->folders.begin()+i);
336 checkedFolders.erase(checkedFolders.begin()+i);
340 for (int i = 0; i < checked.size(); i++)
344 delete this->children[i];
345 this->children.erase(this->children.begin()+i);
346 checked.erase(checked.begin()+i);
350 this->SortChildren();