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(const std::string& path, const std::string& name, const int& level)
51 std::cout << "creating application: " + path + "\n";
52 this->mainFile = NULL;
56 this->path = CDMUtilities::fixPath(path);
58 this->type = wxDIR_DIRS;
62 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
64 std::ifstream confFile;
65 confFile.open((pathMakeLists).c_str());
68 while(confFile.is_open() && !confFile.eof())
71 std::getline(confFile,word,'(');
72 std::vector<std::string> wordBits;
73 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
75 if(wordBits[wordBits.size()-1] == "SET")
78 std::getline(confFile,word,')');
79 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
80 if(wordBits[0] == "EXE_NAME")
83 for (int i = 2; i < wordBits.size(); i++)
85 word += " " + wordBits[i];
88 this->executableName = word;
94 //add library contents
96 this->children.clear();
97 wxDir dir(crea::std2wx((this->path).c_str()));
102 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
105 std::string stdfileName = crea::wx2std(fileName);
107 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
108 this->folders.push_back(folder);
109 this->children.push_back(folder);
111 cont = dir.GetNext(&fileName);
114 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
117 std::string stdfileName = crea::wx2std(fileName);
119 //if CMakeLists, create CMakeLists
120 if(stdfileName == "CMakeLists.txt")
122 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
123 this->children.push_back(this->CMakeLists);
125 //if is an unknown file, create file
128 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
129 std::string extension = stdfileName.substr(stdfileName.size()-4);
130 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
132 std::ifstream fileStream;
134 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
135 while (fileStream.is_open() && !fileStream.eof())
138 std::getline(fileStream,word,'(');
139 std::vector<std::string> wordBits;
140 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
141 if (wordBits[wordBits.size() - 1] == "main")
143 this->mainFile = file;
148 this->children.push_back(file);
151 cont = dir.GetNext(&fileName);
154 this->SortChildren();
155 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
158 modelCDMApplication::~modelCDMApplication()
162 const std::string& modelCDMApplication::GetExecutableName() const
164 return this->executableName;
167 modelCDMFile* modelCDMApplication::GetMainFile() const
169 return this->mainFile;
172 bool modelCDMApplication::SetExecutableName(const std::string& fileName, std::string*& result)
174 std::vector<std::string> words;
175 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
176 std::string fileNameReal = words[0];
177 for (int i = 1; i < words.size(); i++)
179 fileNameReal += "-" + words[i];
183 //opening original cmakelists
184 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
187 result = new std::string("CMakeLists.txt file failed to open.");
190 //opening temporal cmakelists
191 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
194 result = new std::string("CMakeLists.txt.tmp file failed to open.");
197 //copying contents from original to temporal and making changes
198 while (getline(in, line))
200 if(line.find("SET ( EXE_NAME") != std::string::npos)
201 line = "SET ( EXE_NAME " + fileNameReal + " )";
202 out << line << std::endl;
206 //delete old file and rename new file
207 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
208 if(system(renameCommand.c_str()))
210 result = new std::string("An error occurred while running '" + renameCommand + "'.");
214 this->executableName = fileNameReal;
218 modelCDMFolder* modelCDMApplication::CreateFolder(const std::string& name, std::string*& result)
220 //TODO:: mkdir depending on OS
221 std::string command = "mkdir " + path + CDMUtilities::SLASH + name;
222 if(system(command.c_str()))
224 result = new std::string("Error executing: " + command + ".");
227 modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
228 this->folders.push_back(folder);
229 this->children.push_back(folder);
234 const bool modelCDMApplication::Refresh(std::string*& result)
236 this->mainFile = NULL;
237 std::cout << "refreshing application: " << this->executableName << std::endl;
239 this->type = wxDIR_DIRS;
242 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
244 std::ifstream confFile;
245 confFile.open((pathMakeLists).c_str());
248 while(confFile.is_open() && !confFile.eof())
251 std::getline(confFile,word,'(');
252 std::vector<std::string> wordBits;
253 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
255 if(wordBits[wordBits.size()-1] == "SET")
258 std::getline(confFile,word,')');
259 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
260 if(wordBits[0] == "EXE_NAME")
263 for (int i = 2; i < wordBits.size(); i++)
265 word += " " + wordBits[i];
268 this->executableName = word;
276 std::vector<bool> checked(this->children.size(), false);
277 std::vector<bool> checkedFolders(this->folders.size(), false);
280 wxDir dir(crea::std2wx((this->path).c_str()));
284 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
287 std::string stdfileName = crea::wx2std(fileName);
288 std::string applicationName = stdfileName;
289 //check if they already exist
291 for (int i = 0; !found && i < this->folders.size(); i++)
293 if (this->folders[i]->GetName() == applicationName)
296 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
298 checkedFolders[i] = true;
299 if(!this->folders[i]->Refresh(result))
305 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
306 this->folders.push_back(folder);
307 this->children.push_back(folder);
309 cont = dir.GetNext(&fileName);
312 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
315 std::string stdfileName = crea::wx2std(fileName);
317 //if CMakeLists, create CMakeLists
318 if(stdfileName == "CMakeLists.txt")
320 if (this->CMakeLists == NULL)
322 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
323 this->children.push_back(this->CMakeLists);
327 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
329 if(!this->CMakeLists->Refresh(result))
333 //if is an unknown file, create file
337 for (int i = 0; !found && i < this->children.size(); i++)
339 if (this->children[i]->GetName() == stdfileName)
344 if(!this->children[i]->Refresh(result))
347 std::string extension = stdfileName.substr(stdfileName.size()-4);
348 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
350 std::ifstream fileStream;
352 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
353 while (fileStream.is_open() && !fileStream.eof())
356 std::getline(fileStream,word,'(');
357 std::vector<std::string> wordBits;
358 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
359 if (wordBits[wordBits.size() - 1] == "main")
361 this->mainFile = dynamic_cast<modelCDMFile*>(children[i]);
371 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
373 std::string extension = stdfileName.substr(stdfileName.size()-4);
374 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
376 std::ifstream fileStream;
378 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
379 while (fileStream.is_open() && !fileStream.eof())
382 std::getline(fileStream,word,'(');
383 std::vector<std::string> wordBits;
384 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
385 if (wordBits[wordBits.size() - 1] == "main")
387 this->mainFile = file;
393 this->children.push_back(file);
397 cont = dir.GetNext(&fileName);
401 for (int i = 0; i < checkedFolders.size(); i++)
403 if(!checkedFolders[i])
405 this->folders.erase(this->folders.begin()+i);
406 checkedFolders.erase(checkedFolders.begin()+i);
410 for (int i = 0; i < checked.size(); i++)
414 delete this->children[i];
415 this->children.erase(this->children.begin()+i);
416 checked.erase(checked.begin()+i);
420 this->SortChildren();