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"
41 #include "CDMUtilities.h"
45 modelCDMApplication::modelCDMApplication()
50 modelCDMApplication::modelCDMApplication(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
52 std::cout << "creating application: " + path + "\n";
53 this->parent = parent;
54 this->mainFile = NULL;
58 this->path = CDMUtilities::fixPath(path);
60 this->type = wxDIR_DIRS;
64 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
66 std::ifstream confFile;
67 confFile.open((pathMakeLists).c_str());
70 while(confFile.is_open() && !confFile.eof())
73 std::getline(confFile,word,'(');
74 std::vector<std::string> wordBits;
75 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
77 if(wordBits[wordBits.size()-1] == "SET")
80 std::getline(confFile,word,')');
81 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
82 if(wordBits[0] == "EXE_NAME")
85 for (int i = 2; i < (int)(wordBits.size()); i++)
87 word += " " + wordBits[i];
90 this->executableName = word;
96 //add library contents
98 this->children.clear();
99 wxDir dir(crea::std2wx((this->path).c_str()));
104 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
107 std::string stdfileName = crea::wx2std(fileName);
109 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
110 this->folders.push_back(folder);
111 this->children.push_back(folder);
113 cont = dir.GetNext(&fileName);
116 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
119 std::string stdfileName = crea::wx2std(fileName);
121 //if CMakeLists, create CMakeLists
122 if(stdfileName == "CMakeLists.txt")
124 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
125 this->children.push_back(this->CMakeLists);
127 //if is an unknown file, create file
130 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
131 std::string extension = stdfileName.substr(stdfileName.size()-4);
132 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
134 std::ifstream fileStream;
136 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
137 while (fileStream.is_open() && !fileStream.eof())
140 std::getline(fileStream,word,'(');
141 std::vector<std::string> wordBits;
142 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
143 if (wordBits[wordBits.size() - 1] == "main" || wordBits[wordBits.size() - 1] == "IMPLEMENT_APP")
145 this->mainFile = file;
150 this->children.push_back(file);
153 cont = dir.GetNext(&fileName);
156 this->SortChildren();
157 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
160 modelCDMApplication::~modelCDMApplication()
164 const std::string& modelCDMApplication::GetExecutableName() const
166 return this->executableName;
169 modelCDMFile* modelCDMApplication::GetMainFile() const
171 return this->mainFile;
174 bool modelCDMApplication::SetExecutableName(const std::string& fileName, std::string*& result)
176 std::vector<std::string> words;
177 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
178 std::string fileNameReal = words[0];
179 for (int i = 1; i < (int)(words.size()); i++)
181 fileNameReal += "-" + words[i];
185 //opening original cmakelists
186 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
189 result = new std::string("CMakeLists.txt file failed to open.");
192 //opening temporal cmakelists
193 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
196 result = new std::string("CMakeLists.txt.tmp file failed to open.");
199 //copying contents from original to temporal and making changes
200 while (getline(in, line))
202 if(line.find("SET ( EXE_NAME") != std::string::npos)
203 line = "SET ( EXE_NAME " + fileNameReal + " )";
204 out << line << std::endl;
208 //delete old file and rename new file
210 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
212 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
215 if(system(renameCommand.c_str()))
217 result = new std::string("An error occurred while running '" + renameCommand + "'.");
221 this->executableName = fileNameReal;
225 modelCDMFolder* modelCDMApplication::CreateFolder(const std::string& name, std::string*& result)
227 //TODO:: mkdir depending on OS
228 std::string command = "mkdir " + path + CDMUtilities::SLASH + name;
229 if(system(command.c_str()))
231 result = new std::string("Error executing: " + command + ".");
234 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
235 this->folders.push_back(folder);
236 this->children.push_back(folder);
241 const bool modelCDMApplication::Refresh(std::string*& result)
243 this->mainFile = NULL;
244 std::cout << "refreshing application: " << this->executableName << std::endl;
246 this->type = wxDIR_DIRS;
249 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
251 std::ifstream confFile;
252 confFile.open((pathMakeLists).c_str());
255 while(confFile.is_open() && !confFile.eof())
258 std::getline(confFile,word,'(');
259 std::vector<std::string> wordBits;
260 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
262 if(wordBits[wordBits.size()-1] == "SET")
265 std::getline(confFile,word,')');
266 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
267 if(wordBits[0] == "EXE_NAME")
270 for (int i = 2; i < (int)(wordBits.size()); i++)
272 word += " " + wordBits[i];
275 this->executableName = word;
283 std::vector<bool> checked(this->children.size(), false);
284 std::vector<bool> checkedFolders(this->folders.size(), false);
287 wxDir dir(crea::std2wx((this->path).c_str()));
291 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
294 std::string stdfileName = crea::wx2std(fileName);
295 std::string applicationName = stdfileName;
296 //check if they already exist
298 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
300 if (this->folders[i]->GetName() == applicationName)
303 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
305 checkedFolders[i] = true;
306 if(!this->folders[i]->Refresh(result))
312 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
313 this->folders.push_back(folder);
314 this->children.push_back(folder);
316 cont = dir.GetNext(&fileName);
319 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
322 std::string stdfileName = crea::wx2std(fileName);
324 //if CMakeLists, create CMakeLists
325 if(stdfileName == "CMakeLists.txt")
327 if (this->CMakeLists == NULL)
329 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
330 this->children.push_back(this->CMakeLists);
334 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
336 if(!this->CMakeLists->Refresh(result))
340 //if is an unknown file, create file
344 for (int i = 0; !found && i < (int)(this->children.size()); i++)
346 if (this->children[i]->GetName() == stdfileName)
351 if(!this->children[i]->Refresh(result))
354 std::string extension = stdfileName.substr(stdfileName.size()-4);
355 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
357 std::ifstream fileStream;
359 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
360 while (fileStream.is_open() && !fileStream.eof())
363 std::getline(fileStream,word,'(');
364 std::vector<std::string> wordBits;
365 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
366 if (wordBits[wordBits.size() - 1] == "main" || wordBits[wordBits.size() - 1] == "IMPLEMENT_APP")
368 this->mainFile = dynamic_cast<modelCDMFile*>(children[i]);
378 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
380 std::string extension = stdfileName.substr(stdfileName.size()-4);
381 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
383 std::ifstream fileStream;
385 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
386 while (fileStream.is_open() && !fileStream.eof())
389 std::getline(fileStream,word,'(');
390 std::vector<std::string> wordBits;
391 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
392 if (wordBits[wordBits.size() - 1] == "main" || wordBits[wordBits.size() - 1] == "IMPLEMENT_APP")
394 this->mainFile = file;
400 this->children.push_back(file);
404 cont = dir.GetNext(&fileName);
408 for (int i = 0; i < (int)(checkedFolders.size()); i++)
410 if(!checkedFolders[i])
412 this->folders.erase(this->folders.begin()+i);
413 checkedFolders.erase(checkedFolders.begin()+i);
417 for (int i = 0; i < (int)(checked.size()); i++)
421 delete this->children[i];
422 this->children.erase(this->children.begin()+i);
423 checked.erase(checked.begin()+i);
427 this->SortChildren();
431 void modelCDMApplication::CheckStructure(std::map<std::string, bool>& properties)
434 if(this->CMakeLists != NULL)
436 //set default properties
437 properties["application " + this->name + " lib ${crea_LIBRARIES}"] = false;
438 properties["application " + this->name + " lib ${WXWIDGETS_LIBRARIES}"] = false;
439 properties["application " + this->name + " lib ${KWWidgets_LIBRARIES}"] = false;
440 properties["application " + this->name + " lib ${VTK_LIBRARIES}"] = false;
441 properties["application " + this->name + " lib ${ITK_LIBRARIES}"] = false;
442 properties["application " + this->name + " lib ${GDCM_LIBRARIES}"] = false;
443 properties["application " + this->name + " lib ${BOOST_LIBRARIES}"] = false;
446 std::ifstream confFile;
447 confFile.open((this->CMakeLists->GetPath()).c_str());
449 //take everything that is not commented
450 std::string fileContent;
453 std::vector<std::string> words;
454 while(confFile.is_open() && !confFile.eof())
456 std::getline(confFile,word, '\n');
459 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
460 if (words.size() > 0)
463 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
464 for (int i = 0; i < (int)(words.size()); i++)
466 if(words[i].substr(0,2) == "//")
468 fileContent += words[i] + " ";
474 //check every instruction
475 std::stringstream ss(fileContent);
478 std::getline(ss,word, '(');
480 //check instruction name
481 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
484 if (words.size() > 0 && words[words.size()-1] == "SET")
486 std::getline(ss,word, ')');
488 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
489 if (words.size() > 1)
491 if (words[0] == "${EXE_NAME}_LINK_LIBRARIES")
493 for (int i = 1; i < (int)(words.size()); i++)
495 properties["application " + this->name + " lib " + words[i]] = true;
500 else if (words.size() > 0 && words[words.size()-1] == "INCLUDE_DIRECTORIES")
502 std::getline(ss,word, ')');
504 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
506 for (int i = 0; i < (int)(words.size()); i++)
508 properties["application " + this->name + " dir " + words[i]] = true;