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(".");
95 if(fileTypePos != std::string::npos)
96 fileType = stdfileName.substr(fileTypePos);
100 //if CMakeLists, create CMakeLists
101 if(stdfileName == "CMakeLists.txt")
103 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
104 this->children.push_back(this->CMakeLists);
106 //if is a code file, create modelCDMCodeFile
109 fileType == ".cxx" ||
111 fileType == ".cpp" ||
112 fileType == ".txx" ||
113 fileType == ".cmake" )
115 this->children.push_back(new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
117 //if is an unknown file, create file
120 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
123 cont = dir.GetNext(&fileName);
127 this->SortChildren();
128 std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
131 modelCDMAppli::~modelCDMAppli()
135 const std::vector<modelCDMApplication*>& modelCDMAppli::GetApplications() const
137 return this->applications;
140 modelCDMApplication* modelCDMAppli::CreateApplication(
141 const std::string& namein,
146 std::vector<std::string> words;
147 CDMUtilities::splitter::split(words,namein," '/\\*\"%",CDMUtilities::splitter::no_empties);
149 for (int i = 0; i < (int)(words.size()); i++)
155 result = new std::string("The given name is not valid: '/\\*\"% are forbidden.");
161 //copy template application folder with new name
163 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
165 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
168 if(system(copyCommand.c_str()))
170 result = new std::string("An error occurred while running '" + copyCommand + "'.");
173 //set name of library in CMakeLists inside copied folder
175 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
178 result = new std::string("CMakeLists.txt file failed to open.");
181 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
182 while (getline(in, line))
184 if(line == "SET ( EXE_NAME MyExe )")
185 line = "SET ( EXE_NAME " + name + " )";
186 out << line << std::endl;
190 //delete old file and rename new file
192 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
194 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
197 if(system(renameCommand.c_str()))
199 result = new std::string("An error occurred while running '" + renameCommand + "'.");
203 //add application to appli CMakeLists
204 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
207 out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
211 //add application to model
212 modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
213 this->applications.push_back(application);
214 this->children.push_back(application);
216 this->SortChildren();
218 result = new std::string(this->path + CDMUtilities::SLASH + name);
223 //copy template application folder with new name
225 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_wx_appli\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
227 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_wx_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
230 if(system(copyCommand.c_str()))
232 result = new std::string("An error occurred while running '" + copyCommand + "'.");
235 //set name of library in CMakeLists inside copied folder
237 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
240 result = new std::string("CMakeLists.txt file failed to open.");
243 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
244 while (getline(in, line))
246 if(line == "SET ( EXE_NAME MyExeWx )")
247 line = "SET ( EXE_NAME " + name + " )";
248 out << line << std::endl;
252 //delete old file and rename new file
254 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
256 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
259 if(system(renameCommand.c_str()))
261 result = new std::string("An error occurred while running '" + renameCommand + "'.");
265 //add application to appli CMakeLists
266 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
269 out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
273 //add application to model
274 modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
275 this->applications.push_back(application);
276 this->children.push_back(application);
278 this->SortChildren();
280 result = new std::string(this->path + CDMUtilities::SLASH + name);
285 std::string res = "Invalid application type: ";
287 res += std::string(".\n0:Console application.\n1:GUI Application (wxWidgets).");
288 result = new std::string(res);
294 const bool modelCDMAppli::Refresh(std::string*& result)
296 std::cout << "refreshing appli" << std::endl;
297 this->type = wxDIR_DIRS;
299 std::vector<bool> checked(this->children.size(), false);
300 std::vector<bool> checkedApplications(this->applications.size(), false);
303 wxDir dir(crea::std2wx((this->path).c_str()));
307 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
310 std::string stdfileName = crea::wx2std(fileName);
312 if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
314 std::string applicationName = stdfileName;
315 //check if application already exist
317 for (int i = 0; !found && i < (int)(this->applications.size()); i++)
319 if (this->applications[i]->GetName() == applicationName)
322 int pos = std::find(this->children.begin(), this->children.end(), this->applications[i]) - this->children.begin();
324 checkedApplications[i] = true;
325 if(!this->applications[i]->Refresh(result))
331 modelCDMApplication* application= new modelCDMApplication(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
332 this->applications.push_back(application);
333 this->children.push_back(application);
338 //check if folder already exist
340 for (int i = 0; !found && i < (int)(this->children.size()); i++)
342 if (this->children[i]->GetName() == stdfileName)
347 if(!this->children[i]->Refresh(result))
353 modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
354 this->children.push_back(folder);
357 cont = dir.GetNext(&fileName);
360 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
363 std::string stdfileName = crea::wx2std(fileName);
364 std::size_t fileTypePos = stdfileName.find_last_of(".");
365 std::string fileType;
366 if(fileTypePos != std::string::npos)
367 fileType = stdfileName.substr(fileTypePos);
371 //if CMakeLists, create CMakeLists
372 if(stdfileName == "CMakeLists.txt")
374 if (this->CMakeLists == NULL)
376 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
377 this->children.push_back(this->CMakeLists);
381 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
383 if(!this->CMakeLists->Refresh(result))
387 //if is a code file, create modelCDMCodeFile
388 //if is an unknown file, create file
392 for (int i = 0; !found && i < (int)(this->children.size()); i++)
394 if (this->children[i]->GetName() == stdfileName)
398 if(!this->children[i]->Refresh(result))
407 fileType == ".cxx" ||
409 fileType == ".cpp" ||
410 fileType == ".txx" ||
411 fileType == ".cmake" )
413 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
417 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
418 this->children.push_back(file);
423 cont = dir.GetNext(&fileName);
427 for (int i = 0; i < (int)(checkedApplications.size()); i++)
429 if(!checkedApplications[i])
431 this->applications.erase(this->applications.begin()+i);
432 checkedApplications.erase(checkedApplications.begin()+i);
436 for (int i = 0; i < (int)(checked.size()); i++)
440 delete this->children[i];
441 this->children.erase(this->children.begin()+i);
442 checked.erase(checked.begin()+i);
446 this->SortChildren();
447 std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
451 void modelCDMAppli::CheckStructure(std::map<std::string, bool>& properties)
454 if(this->CMakeLists != NULL)
456 //set properties parameters based on model
457 for (int i = 0; i < (int)(this->applications.size()); i++)
458 properties["appli add " + this->applications[i]->GetName()] = false;
461 std::ifstream confFile;
462 confFile.open((this->CMakeLists->GetPath()).c_str());
464 //take everything that is not commented
465 std::string fileContent;
468 std::vector<std::string> words;
469 while(confFile.is_open() && !confFile.eof())
471 std::getline(confFile,word, '\n');
474 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
475 if (words.size() > 0)
478 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
479 for (int i = 0; i < (int)(words.size()); i++)
481 if(words[i].substr(0,2) == "//")
483 fileContent += words[i] + " ";
489 //check every instruction
490 std::stringstream ss(fileContent);
493 std::getline(ss,word, '(');
495 //check instruction name
496 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
499 if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
501 std::getline(ss,word, ')');
502 //std::cout << word << std::endl;
503 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
505 if (words.size() > 0)
508 properties["appli add " + words[0]] = true;
516 //check libraries' structure
517 for (int i = 0; i < (int)(this->applications.size()); i++)
519 properties["application " + this->applications[i]->GetName()] = true;
520 this->applications[i]->CheckStructure(properties);
524 bool modelCDMAppli::IsApplicationIncluded(const std::string& application_name)
526 if (this->HasCMakeLists())
528 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
529 for (int i = 0; i < cmlFile.size(); ++i)
531 if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
534 while (pos < cmlFile[i].second.size())
536 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
538 if (application_name == cmlFile[i].second[pos])
550 bool modelCDMAppli::SetApplicationInclude(const std::string& application_name, const bool& toInclude)
552 if (this->HasCMakeLists())
554 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
558 for (int i = 0; i < cmlFile.size(); ++i)
560 if(toInclude && cmlFile[i].first == "comment")
562 std::vector<std::string> segments;
563 std::string line = cmlFile[i].second[0];
564 while(line[0] == '#')
567 CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
568 if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == application_name)
571 while(cmlFile[i].second[0][0] == '#')
572 cmlFile[i].second[0].erase(0,1);
575 else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
578 while (pos < cmlFile[i].second.size())
580 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
582 if (application_name == cmlFile[i].second[pos])
587 cmlFile[i].first = "comment";
588 cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
589 while (cmlFile[i].second.size() > 1)
591 cmlFile[i].second[0] += cmlFile[i].second[1];
592 cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
603 if (!found && toInclude)
605 CDMUtilities::syntaxElement element;
606 element.first = "command";
607 element.second.push_back("ADD_SUBDIRECTORY(" + application_name + ")");
608 cmlFile.push_back(element);
611 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);