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 # ------------------------------------------------------------------------
31 * Created on: Nov 23, 2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "modelCDMAppli.h"
42 #include "CDMUtilities.h"
46 modelCDMAppli::modelCDMAppli()
50 modelCDMAppli::modelCDMAppli(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
52 std::cout << "creating appli\n";
53 this->parent = parent;
54 this->type = wxDIR_DIRS;
61 this->path = CDMUtilities::fixPath(path);
62 std::string pathFixed(CDMUtilities::fixPath(path));
64 this->applications.clear();
65 wxDir dir(crea::std2wx((pathFixed).c_str()));
69 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
72 std::string stdfileName = crea::wx2std(fileName);
74 if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
76 modelCDMApplication* application = new modelCDMApplication(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
77 this->applications.push_back(application);
78 this->children.push_back(application);
82 modelCDMFolder* folder = new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
83 this->children.push_back(folder);
86 cont = dir.GetNext(&fileName);
89 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
92 std::string stdfileName = crea::wx2std(fileName);
93 std::size_t fileTypePos = stdfileName.find_last_of(".");
94 std::string fileType = stdfileName.substr(fileTypePos);
96 //if CMakeLists, create CMakeLists
97 if(stdfileName == "CMakeLists.txt")
99 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
100 this->children.push_back(this->CMakeLists);
102 //if is a code file, create modelCDMCodeFile
105 fileType == ".cxx" ||
107 fileType == ".cpp" ||
108 fileType == ".txx" ||
109 fileType == ".cmake" )
111 this->children.push_back(new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
113 //if is an unknown file, create file
116 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
119 cont = dir.GetNext(&fileName);
123 this->SortChildren();
124 std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
127 modelCDMAppli::~modelCDMAppli()
131 const std::vector<modelCDMApplication*>& modelCDMAppli::GetApplications() const
133 return this->applications;
136 modelCDMApplication* modelCDMAppli::CreateApplication(
137 const std::string& namein,
142 std::vector<std::string> words;
143 CDMUtilities::splitter::split(words,namein," '/\\*\"%",CDMUtilities::splitter::no_empties);
145 for (int i = 0; i < (int)(words.size()); i++)
151 result = new std::string("The given name is not valid: '/\\*\"% are forbidden.");
157 //copy template application folder with new name
159 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
161 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
164 if(system(copyCommand.c_str()))
166 result = new std::string("An error occurred while running '" + copyCommand + "'.");
169 //set name of library in CMakeLists inside copied folder
171 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
174 result = new std::string("CMakeLists.txt file failed to open.");
177 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
178 while (getline(in, line))
180 if(line == "SET ( EXE_NAME MyExe )")
181 line = "SET ( EXE_NAME " + name + " )";
182 out << line << std::endl;
186 //delete old file and rename new file
188 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
190 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
193 if(system(renameCommand.c_str()))
195 result = new std::string("An error occurred while running '" + renameCommand + "'.");
199 //add application to appli CMakeLists
200 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
203 out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
207 //add application to model
208 modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
209 this->applications.push_back(application);
210 this->children.push_back(application);
212 this->SortChildren();
214 result = new std::string(this->path + CDMUtilities::SLASH + name);
219 //copy template application folder with new name
221 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_wx_appli\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
223 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_wx_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
226 if(system(copyCommand.c_str()))
228 result = new std::string("An error occurred while running '" + copyCommand + "'.");
231 //set name of library in CMakeLists inside copied folder
233 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
236 result = new std::string("CMakeLists.txt file failed to open.");
239 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
240 while (getline(in, line))
242 if(line == "SET ( EXE_NAME MyExeWx )")
243 line = "SET ( EXE_NAME " + name + " )";
244 out << line << std::endl;
248 //delete old file and rename new file
250 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
252 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
255 if(system(renameCommand.c_str()))
257 result = new std::string("An error occurred while running '" + renameCommand + "'.");
261 //add application to appli CMakeLists
262 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
265 out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
269 //add application to model
270 modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
271 this->applications.push_back(application);
272 this->children.push_back(application);
274 this->SortChildren();
276 result = new std::string(this->path + CDMUtilities::SLASH + name);
281 std::string res = "Invalid application type: ";
283 res += std::string(".\n0:Console application.\n1:GUI Application (wxWidgets).");
284 result = new std::string(res);
290 const bool modelCDMAppli::Refresh(std::string*& result)
292 std::cout << "refreshing appli" << std::endl;
293 this->type = wxDIR_DIRS;
295 std::vector<bool> checked(this->children.size(), false);
296 std::vector<bool> checkedApplications(this->applications.size(), false);
299 wxDir dir(crea::std2wx((this->path).c_str()));
303 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
306 std::string stdfileName = crea::wx2std(fileName);
308 if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
310 std::string applicationName = stdfileName;
311 //check if application already exist
313 for (int i = 0; !found && i < (int)(this->applications.size()); i++)
315 if (this->applications[i]->GetName() == applicationName)
318 int pos = std::find(this->children.begin(), this->children.end(), this->applications[i]) - this->children.begin();
320 checkedApplications[i] = true;
321 if(!this->applications[i]->Refresh(result))
327 modelCDMApplication* application= new modelCDMApplication(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
328 this->applications.push_back(application);
329 this->children.push_back(application);
334 //check if folder already exist
336 for (int i = 0; !found && i < (int)(this->children.size()); i++)
338 if (this->children[i]->GetName() == stdfileName)
343 if(!this->children[i]->Refresh(result))
349 modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
350 this->children.push_back(folder);
353 cont = dir.GetNext(&fileName);
356 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
359 std::string stdfileName = crea::wx2std(fileName);
360 std::size_t fileTypePos = stdfileName.find_last_of(".");
361 std::string fileType = stdfileName.substr(fileTypePos);
363 //if CMakeLists, create CMakeLists
364 if(stdfileName == "CMakeLists.txt")
366 if (this->CMakeLists == NULL)
368 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
369 this->children.push_back(this->CMakeLists);
373 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
375 if(!this->CMakeLists->Refresh(result))
379 //if is a code file, create modelCDMCodeFile
380 //if is an unknown file, create file
384 for (int i = 0; !found && i < (int)(this->children.size()); i++)
386 if (this->children[i]->GetName() == stdfileName)
390 if(!this->children[i]->Refresh(result))
399 fileType == ".cxx" ||
401 fileType == ".cpp" ||
402 fileType == ".txx" ||
403 fileType == ".cmake" )
405 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
409 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
410 this->children.push_back(file);
415 cont = dir.GetNext(&fileName);
419 for (int i = 0; i < (int)(checkedApplications.size()); i++)
421 if(!checkedApplications[i])
423 this->applications.erase(this->applications.begin()+i);
424 checkedApplications.erase(checkedApplications.begin()+i);
428 for (int i = 0; i < (int)(checked.size()); i++)
432 delete this->children[i];
433 this->children.erase(this->children.begin()+i);
434 checked.erase(checked.begin()+i);
438 this->SortChildren();
439 std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
443 void modelCDMAppli::CheckStructure(std::map<std::string, bool>& properties)
446 if(this->CMakeLists != NULL)
448 //set properties parameters based on model
449 for (int i = 0; i < (int)(this->applications.size()); i++)
450 properties["appli add " + this->applications[i]->GetName()] = false;
453 std::ifstream confFile;
454 confFile.open((this->CMakeLists->GetPath()).c_str());
456 //take everything that is not commented
457 std::string fileContent;
460 std::vector<std::string> words;
461 while(confFile.is_open() && !confFile.eof())
463 std::getline(confFile,word, '\n');
466 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
467 if (words.size() > 0)
470 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
471 for (int i = 0; i < (int)(words.size()); i++)
473 if(words[i].substr(0,2) == "//")
475 fileContent += words[i] + " ";
481 //check every instruction
482 std::stringstream ss(fileContent);
485 std::getline(ss,word, '(');
487 //check instruction name
488 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
491 if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
493 std::getline(ss,word, ')');
494 //std::cout << word << std::endl;
495 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
497 if (words.size() > 0)
500 properties["appli add " + words[0]] = true;
508 //check libraries' structure
509 for (int i = 0; i < (int)(this->applications.size()); i++)
511 properties["application " + this->applications[i]->GetName()] = true;
512 this->applications[i]->CheckStructure(properties);
516 bool modelCDMAppli::IsApplicationIncluded(const std::string& application_name)
518 if (this->HasCMakeLists())
520 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
521 for (int i = 0; i < cmlFile.size(); ++i)
523 if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
526 while (pos < cmlFile[i].second.size())
528 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
530 if (application_name == cmlFile[i].second[pos])
542 bool modelCDMAppli::SetApplicationInclude(const std::string& application_name, const bool& toInclude)
544 if (this->HasCMakeLists())
546 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
550 for (int i = 0; i < cmlFile.size(); ++i)
552 if(toInclude && cmlFile[i].first == "comment")
554 std::vector<std::string> segments;
555 std::string line = cmlFile[i].second[0];
556 while(line[0] == '#')
559 CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
560 if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == application_name)
563 while(cmlFile[i].second[0][0] == '#')
564 cmlFile[i].second[0].erase(0,1);
567 else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
570 while (pos < cmlFile[i].second.size())
572 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
574 if (application_name == cmlFile[i].second[pos])
579 cmlFile[i].first = "comment";
580 cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
581 while (cmlFile[i].second.size() > 1)
583 cmlFile[i].second[0] += cmlFile[i].second[1];
584 cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
595 if (!found && toInclude)
597 CDMUtilities::syntaxElement element;
598 element.first = "command";
599 element.second.push_back("ADD_SUBDIRECTORY(" + application_name + ")");
600 cmlFile.push_back(element);
603 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);