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);
94 //if CMakeLists, create CMakeLists
95 if(stdfileName == "CMakeLists.txt")
97 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
98 this->children.push_back(this->CMakeLists);
100 //if is an unknown file, create file
103 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
106 cont = dir.GetNext(&fileName);
110 this->SortChildren();
111 std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
114 modelCDMAppli::~modelCDMAppli()
118 const std::vector<modelCDMApplication*>& modelCDMAppli::GetApplications() const
120 return this->applications;
123 modelCDMApplication* modelCDMAppli::CreateApplication(
124 const std::string& namein,
129 std::vector<std::string> words;
130 CDMUtilities::splitter::split(words,namein," '/\\*\"%",CDMUtilities::splitter::no_empties);
132 for (int i = 0; i < (int)(words.size()); i++)
138 result = new std::string("The given name is not valid: '/\\*\"% are forbidden.");
144 //copy template application folder with new name
146 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
148 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
151 if(system(copyCommand.c_str()))
153 result = new std::string("An error occurred while running '" + copyCommand + "'.");
156 //set name of library in CMakeLists inside copied folder
158 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
161 result = new std::string("CMakeLists.txt file failed to open.");
164 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
165 while (getline(in, line))
167 if(line == "SET ( EXE_NAME MyExe )")
168 line = "SET ( EXE_NAME " + name + " )";
169 out << line << std::endl;
173 //delete old file and rename new file
175 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
177 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
180 if(system(renameCommand.c_str()))
182 result = new std::string("An error occurred while running '" + renameCommand + "'.");
186 //add application to appli CMakeLists
187 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
190 out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
194 //add application to model
195 modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
196 this->applications.push_back(application);
197 this->children.push_back(application);
199 this->SortChildren();
201 result = new std::string(this->path + CDMUtilities::SLASH + name);
206 //copy template application folder with new name
208 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_wx_appli\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
210 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_wx_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
213 if(system(copyCommand.c_str()))
215 result = new std::string("An error occurred while running '" + copyCommand + "'.");
218 //set name of library in CMakeLists inside copied folder
220 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
223 result = new std::string("CMakeLists.txt file failed to open.");
226 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
227 while (getline(in, line))
229 if(line == "SET ( EXE_NAME MyExeWx )")
230 line = "SET ( EXE_NAME " + name + " )";
231 out << line << std::endl;
235 //delete old file and rename new file
237 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
239 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
242 if(system(renameCommand.c_str()))
244 result = new std::string("An error occurred while running '" + renameCommand + "'.");
248 //add application to appli CMakeLists
249 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
252 out1 << "ADD_SUBDIRECTORY(" << name << ")" << std::endl;
256 //add application to model
257 modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
258 this->applications.push_back(application);
259 this->children.push_back(application);
261 this->SortChildren();
263 result = new std::string(this->path + CDMUtilities::SLASH + name);
268 std::string res = "Invalid application type: ";
270 res += std::string(".\n0:Console application.\n1:GUI Application (wxWidgets).");
271 result = new std::string(res);
277 const bool modelCDMAppli::Refresh(std::string*& result)
279 std::cout << "refreshing appli" << std::endl;
280 this->type = wxDIR_DIRS;
282 std::vector<bool> checked(this->children.size(), false);
283 std::vector<bool> checkedApplications(this->applications.size(), false);
286 wxDir dir(crea::std2wx((this->path).c_str()));
290 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
293 std::string stdfileName = crea::wx2std(fileName);
295 if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
297 std::string applicationName = stdfileName;
298 //check if application already exist
300 for (int i = 0; !found && i < (int)(this->applications.size()); i++)
302 if (this->applications[i]->GetName() == applicationName)
305 int pos = std::find(this->children.begin(), this->children.end(), this->applications[i]) - this->children.begin();
307 checkedApplications[i] = true;
308 if(!this->applications[i]->Refresh(result))
314 modelCDMApplication* application= new modelCDMApplication(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
315 this->applications.push_back(application);
316 this->children.push_back(application);
321 //check if folder already exist
323 for (int i = 0; !found && i < (int)(this->children.size()); i++)
325 if (this->children[i]->GetName() == stdfileName)
330 if(!this->children[i]->Refresh(result))
336 modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
337 this->children.push_back(folder);
340 cont = dir.GetNext(&fileName);
343 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
346 std::string stdfileName = crea::wx2std(fileName);
348 //if CMakeLists, create CMakeLists
349 if(stdfileName == "CMakeLists.txt")
351 if (this->CMakeLists == NULL)
353 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
354 this->children.push_back(this->CMakeLists);
358 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
360 if(!this->CMakeLists->Refresh(result))
364 //if is an unknown file, create file
368 for (int i = 0; !found && i < (int)(this->children.size()); i++)
370 if (this->children[i]->GetName() == stdfileName)
374 if(!this->children[i]->Refresh(result))
381 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
382 this->children.push_back(file);
386 cont = dir.GetNext(&fileName);
390 for (int i = 0; i < (int)(checkedApplications.size()); i++)
392 if(!checkedApplications[i])
394 this->applications.erase(this->applications.begin()+i);
395 checkedApplications.erase(checkedApplications.begin()+i);
399 for (int i = 0; i < (int)(checked.size()); i++)
403 delete this->children[i];
404 this->children.erase(this->children.begin()+i);
405 checked.erase(checked.begin()+i);
409 this->SortChildren();
410 std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
414 void modelCDMAppli::CheckStructure(std::map<std::string, bool>& properties)
417 if(this->CMakeLists != NULL)
419 //set properties parameters based on model
420 for (int i = 0; i < (int)(this->applications.size()); i++)
421 properties["appli add " + this->applications[i]->GetName()] = false;
424 std::ifstream confFile;
425 confFile.open((this->CMakeLists->GetPath()).c_str());
427 //take everything that is not commented
428 std::string fileContent;
431 std::vector<std::string> words;
432 while(confFile.is_open() && !confFile.eof())
434 std::getline(confFile,word, '\n');
437 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
438 if (words.size() > 0)
441 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
442 for (int i = 0; i < (int)(words.size()); i++)
444 if(words[i].substr(0,2) == "//")
446 fileContent += words[i] + " ";
452 //check every instruction
453 std::stringstream ss(fileContent);
456 std::getline(ss,word, '(');
458 //check instruction name
459 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
462 if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
464 std::getline(ss,word, ')');
465 //std::cout << word << std::endl;
466 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
468 if (words.size() > 0)
471 properties["appli add " + words[0]] = true;
479 //check libraries' structure
480 for (int i = 0; i < (int)(this->applications.size()); i++)
482 properties["application " + this->applications[i]->GetName()] = true;
483 this->applications[i]->CheckStructure(properties);
487 bool modelCDMAppli::IsApplicationIncluded(const std::string& application_name)
489 if (this->HasCMakeLists())
491 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
492 for (int i = 0; i < cmlFile.size(); ++i)
494 if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
497 while (pos < cmlFile[i].second.size())
499 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
501 if (application_name == cmlFile[i].second[pos])
513 bool modelCDMAppli::SetApplicationInclude(const std::string& application_name, const bool& toInclude)
515 if (this->HasCMakeLists())
517 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
521 for (int i = 0; i < cmlFile.size(); ++i)
523 if(toInclude && cmlFile[i].first == "comment")
525 std::vector<std::string> segments;
526 std::string line = cmlFile[i].second[0];
527 while(line[0] == '#')
530 CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
531 if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == application_name)
534 while(cmlFile[i].second[0][0] == '#')
535 cmlFile[i].second[0].erase(0,1);
538 else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
541 while (pos < cmlFile[i].second.size())
543 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
545 if (application_name == cmlFile[i].second[pos])
550 cmlFile[i].first = "comment";
551 cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
552 while (cmlFile[i].second.size() > 1)
554 cmlFile[i].second[0] += cmlFile[i].second[1];
555 cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
566 if (!found && toInclude)
568 CDMUtilities::syntaxElement element;
569 element.first = "command";
570 element.second.push_back("ADD_SUBDIRECTORY(" + application_name + ")");
571 cmlFile.push_back(element);
574 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);