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()
49 modelCDMApplication::modelCDMApplication(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
51 std::cout << "creating application: " + path + "\n";
52 this->parent = parent;
53 this->mainFile = NULL;
57 this->path = CDMUtilities::fixPath(path);
59 this->type = wxDIR_DIRS;
63 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
65 std::ifstream confFile;
66 confFile.open((pathMakeLists).c_str());
69 while(confFile.is_open() && !confFile.eof())
72 std::getline(confFile,word,'(');
73 std::vector<std::string> wordBits;
74 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
76 if(wordBits[wordBits.size()-1] == "SET")
79 std::getline(confFile,word,')');
80 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
81 if(wordBits[0] == "EXE_NAME")
84 for (int i = 2; i < wordBits.size(); i++)
86 word += " " + wordBits[i];
89 this->executableName = word;
95 //add library contents
97 this->children.clear();
98 wxDir dir(crea::std2wx((this->path).c_str()));
103 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
106 std::string stdfileName = crea::wx2std(fileName);
108 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
109 this->folders.push_back(folder);
110 this->children.push_back(folder);
112 cont = dir.GetNext(&fileName);
115 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
118 std::string stdfileName = crea::wx2std(fileName);
120 //if CMakeLists, create CMakeLists
121 if(stdfileName == "CMakeLists.txt")
123 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
124 this->children.push_back(this->CMakeLists);
126 //if is an unknown file, create file
129 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
130 std::string extension = stdfileName.substr(stdfileName.size()-4);
131 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
133 std::ifstream fileStream;
135 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
136 while (fileStream.is_open() && !fileStream.eof())
139 std::getline(fileStream,word,'(');
140 std::vector<std::string> wordBits;
141 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
142 if (wordBits[wordBits.size() - 1] == "main")
144 this->mainFile = file;
149 this->children.push_back(file);
152 cont = dir.GetNext(&fileName);
155 this->SortChildren();
156 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
159 modelCDMApplication::~modelCDMApplication()
163 const std::string& modelCDMApplication::GetExecutableName() const
165 return this->executableName;
168 modelCDMFile* modelCDMApplication::GetMainFile() const
170 return this->mainFile;
173 bool modelCDMApplication::SetExecutableName(const std::string& fileName, std::string*& result)
175 std::vector<std::string> words;
176 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
177 std::string fileNameReal = words[0];
178 for (int i = 1; i < words.size(); i++)
180 fileNameReal += "-" + words[i];
184 //opening original cmakelists
185 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
188 result = new std::string("CMakeLists.txt file failed to open.");
191 //opening temporal cmakelists
192 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
195 result = new std::string("CMakeLists.txt.tmp file failed to open.");
198 //copying contents from original to temporal and making changes
199 while (getline(in, line))
201 if(line.find("SET ( EXE_NAME") != std::string::npos)
202 line = "SET ( EXE_NAME " + fileNameReal + " )";
203 out << line << std::endl;
207 //delete old file and rename new file
208 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
209 if(system(renameCommand.c_str()))
211 result = new std::string("An error occurred while running '" + renameCommand + "'.");
215 this->executableName = fileNameReal;
219 modelCDMFolder* modelCDMApplication::CreateFolder(const std::string& name, std::string*& result)
221 //TODO:: mkdir depending on OS
222 std::string command = "mkdir " + path + CDMUtilities::SLASH + name;
223 if(system(command.c_str()))
225 result = new std::string("Error executing: " + command + ".");
228 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
229 this->folders.push_back(folder);
230 this->children.push_back(folder);
235 const bool modelCDMApplication::Refresh(std::string*& result)
237 this->mainFile = NULL;
238 std::cout << "refreshing application: " << this->executableName << std::endl;
240 this->type = wxDIR_DIRS;
243 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
245 std::ifstream confFile;
246 confFile.open((pathMakeLists).c_str());
249 while(confFile.is_open() && !confFile.eof())
252 std::getline(confFile,word,'(');
253 std::vector<std::string> wordBits;
254 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
256 if(wordBits[wordBits.size()-1] == "SET")
259 std::getline(confFile,word,')');
260 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
261 if(wordBits[0] == "EXE_NAME")
264 for (int i = 2; i < wordBits.size(); i++)
266 word += " " + wordBits[i];
269 this->executableName = word;
277 std::vector<bool> checked(this->children.size(), false);
278 std::vector<bool> checkedFolders(this->folders.size(), false);
281 wxDir dir(crea::std2wx((this->path).c_str()));
285 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
288 std::string stdfileName = crea::wx2std(fileName);
289 std::string applicationName = stdfileName;
290 //check if they already exist
292 for (int i = 0; !found && i < this->folders.size(); i++)
294 if (this->folders[i]->GetName() == applicationName)
297 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
299 checkedFolders[i] = true;
300 if(!this->folders[i]->Refresh(result))
306 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
307 this->folders.push_back(folder);
308 this->children.push_back(folder);
310 cont = dir.GetNext(&fileName);
313 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
316 std::string stdfileName = crea::wx2std(fileName);
318 //if CMakeLists, create CMakeLists
319 if(stdfileName == "CMakeLists.txt")
321 if (this->CMakeLists == NULL)
323 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
324 this->children.push_back(this->CMakeLists);
328 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
330 if(!this->CMakeLists->Refresh(result))
334 //if is an unknown file, create file
338 for (int i = 0; !found && i < this->children.size(); i++)
340 if (this->children[i]->GetName() == stdfileName)
345 if(!this->children[i]->Refresh(result))
348 std::string extension = stdfileName.substr(stdfileName.size()-4);
349 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
351 std::ifstream fileStream;
353 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
354 while (fileStream.is_open() && !fileStream.eof())
357 std::getline(fileStream,word,'(');
358 std::vector<std::string> wordBits;
359 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
360 if (wordBits[wordBits.size() - 1] == "main")
362 this->mainFile = dynamic_cast<modelCDMFile*>(children[i]);
372 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
374 std::string extension = stdfileName.substr(stdfileName.size()-4);
375 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
377 std::ifstream fileStream;
379 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
380 while (fileStream.is_open() && !fileStream.eof())
383 std::getline(fileStream,word,'(');
384 std::vector<std::string> wordBits;
385 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
386 if (wordBits[wordBits.size() - 1] == "main")
388 this->mainFile = file;
394 this->children.push_back(file);
398 cont = dir.GetNext(&fileName);
402 for (int i = 0; i < checkedFolders.size(); i++)
404 if(!checkedFolders[i])
406 this->folders.erase(this->folders.begin()+i);
407 checkedFolders.erase(checkedFolders.begin()+i);
411 for (int i = 0; i < checked.size(); i++)
415 delete this->children[i];
416 this->children.erase(this->children.begin()+i);
417 checked.erase(checked.begin()+i);
421 this->SortChildren();