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")
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
209 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
210 if(system(renameCommand.c_str()))
212 result = new std::string("An error occurred while running '" + renameCommand + "'.");
216 this->executableName = fileNameReal;
220 modelCDMFolder* modelCDMApplication::CreateFolder(const std::string& name, std::string*& result)
222 //TODO:: mkdir depending on OS
223 std::string command = "mkdir " + path + CDMUtilities::SLASH + name;
224 if(system(command.c_str()))
226 result = new std::string("Error executing: " + command + ".");
229 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
230 this->folders.push_back(folder);
231 this->children.push_back(folder);
236 const bool modelCDMApplication::Refresh(std::string*& result)
238 this->mainFile = NULL;
239 std::cout << "refreshing application: " << this->executableName << std::endl;
241 this->type = wxDIR_DIRS;
244 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
246 std::ifstream confFile;
247 confFile.open((pathMakeLists).c_str());
250 while(confFile.is_open() && !confFile.eof())
253 std::getline(confFile,word,'(');
254 std::vector<std::string> wordBits;
255 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
257 if(wordBits[wordBits.size()-1] == "SET")
260 std::getline(confFile,word,')');
261 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
262 if(wordBits[0] == "EXE_NAME")
265 for (int i = 2; i < (int)(wordBits.size()); i++)
267 word += " " + wordBits[i];
270 this->executableName = word;
278 std::vector<bool> checked(this->children.size(), false);
279 std::vector<bool> checkedFolders(this->folders.size(), false);
282 wxDir dir(crea::std2wx((this->path).c_str()));
286 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
289 std::string stdfileName = crea::wx2std(fileName);
290 std::string applicationName = stdfileName;
291 //check if they already exist
293 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
295 if (this->folders[i]->GetName() == applicationName)
298 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
300 checkedFolders[i] = true;
301 if(!this->folders[i]->Refresh(result))
307 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
308 this->folders.push_back(folder);
309 this->children.push_back(folder);
311 cont = dir.GetNext(&fileName);
314 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
317 std::string stdfileName = crea::wx2std(fileName);
319 //if CMakeLists, create CMakeLists
320 if(stdfileName == "CMakeLists.txt")
322 if (this->CMakeLists == NULL)
324 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
325 this->children.push_back(this->CMakeLists);
329 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
331 if(!this->CMakeLists->Refresh(result))
335 //if is an unknown file, create file
339 for (int i = 0; !found && i < (int)(this->children.size()); i++)
341 if (this->children[i]->GetName() == stdfileName)
346 if(!this->children[i]->Refresh(result))
349 std::string extension = stdfileName.substr(stdfileName.size()-4);
350 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
352 std::ifstream fileStream;
354 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
355 while (fileStream.is_open() && !fileStream.eof())
358 std::getline(fileStream,word,'(');
359 std::vector<std::string> wordBits;
360 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
361 if (wordBits[wordBits.size() - 1] == "main")
363 this->mainFile = dynamic_cast<modelCDMFile*>(children[i]);
373 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
375 std::string extension = stdfileName.substr(stdfileName.size()-4);
376 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
378 std::ifstream fileStream;
380 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
381 while (fileStream.is_open() && !fileStream.eof())
384 std::getline(fileStream,word,'(');
385 std::vector<std::string> wordBits;
386 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
387 if (wordBits[wordBits.size() - 1] == "main")
389 this->mainFile = file;
395 this->children.push_back(file);
399 cont = dir.GetNext(&fileName);
403 for (int i = 0; i < (int)(checkedFolders.size()); i++)
405 if(!checkedFolders[i])
407 this->folders.erase(this->folders.begin()+i);
408 checkedFolders.erase(checkedFolders.begin()+i);
412 for (int i = 0; i < (int)(checked.size()); i++)
416 delete this->children[i];
417 this->children.erase(this->children.begin()+i);
418 checked.erase(checked.begin()+i);
422 this->SortChildren();
426 void modelCDMApplication::CheckStructure(std::map<std::string, bool>& properties)
429 if(this->CMakeLists != NULL)
431 //set default properties
432 properties["application " + this->name + " lib ${crea_LIBRARIES}"] = false;
433 properties["application " + this->name + " lib ${WXWIDGETS_LIBRARIES}"] = false;
434 properties["application " + this->name + " lib ${KWWidgets_LIBRARIES}"] = false;
435 properties["application " + this->name + " lib ${VTK_LIBRARIES}"] = false;
436 properties["application " + this->name + " lib ${ITK_LIBRARIES}"] = false;
437 properties["application " + this->name + " lib ${GDCM_LIBRARIES}"] = false;
438 properties["application " + this->name + " lib ${BOOST_LIBRARIES}"] = false;
441 std::ifstream confFile;
442 confFile.open((this->CMakeLists->GetPath()).c_str());
444 //take everything that is not commented
445 std::string fileContent;
448 std::vector<std::string> words;
449 while(confFile.is_open() && !confFile.eof())
451 std::getline(confFile,word, '\n');
454 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
455 if (words.size() > 0)
458 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
459 for (int i = 0; i < (int)(words.size()); i++)
461 if(words[i].substr(0,2) == "//")
463 fileContent += words[i] + " ";
469 //check every instruction
470 std::stringstream ss(fileContent);
473 std::getline(ss,word, '(');
475 //check instruction name
476 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
479 if (words.size() > 0 && words[words.size()-1] == "SET")
481 std::getline(ss,word, ')');
483 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
484 if (words.size() > 1)
486 if (words[0] == "${EXE_NAME}_LINK_LIBRARIES")
488 for (int i = 1; i < (int)(words.size()); i++)
490 properties["application " + this->name + " lib " + words[i]] = true;
495 else if (words.size() > 0 && words[words.size()-1] == "INCLUDE_DIRECTORIES")
497 std::getline(ss,word, ')');
499 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
501 for (int i = 0; i < (int)(words.size()); i++)
503 properties["application " + this->name + " dir " + words[i]] = true;