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 model
187 modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
188 this->applications.push_back(application);
189 this->children.push_back(application);
191 this->SortChildren();
193 result = new std::string(this->path + CDMUtilities::SLASH + name);
198 //copy template application folder with new name
200 std::string copyCommand = "xcopy \"" + this->path + CDMUtilities::SLASH + "template_wx_appli\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "\" /Y";
202 std::string copyCommand = "cp -r \"" + this->path + CDMUtilities::SLASH + "template_wx_appli\" \"" + this->path + CDMUtilities::SLASH + name + "\"";
205 if(system(copyCommand.c_str()))
207 result = new std::string("An error occurred while running '" + copyCommand + "'.");
210 //set name of library in CMakeLists inside copied folder
212 std::ifstream in((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
215 result = new std::string("CMakeLists.txt file failed to open.");
218 std::ofstream out((this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
219 while (getline(in, line))
221 if(line == "SET ( EXE_NAME MyExeWx )")
222 line = "SET ( EXE_NAME " + name + " )";
223 out << line << std::endl;
227 //delete old file and rename new file
229 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
231 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\"";
234 if(system(renameCommand.c_str()))
236 result = new std::string("An error occurred while running '" + renameCommand + "'.");
240 //add application to model
241 modelCDMApplication* application = new modelCDMApplication(this, this->path + CDMUtilities::SLASH + name, name, this->level + 1);
242 this->applications.push_back(application);
243 this->children.push_back(application);
245 this->SortChildren();
247 result = new std::string(this->path + CDMUtilities::SLASH + name);
252 std::string res = "Invalid application type: ";
254 res += std::string(".\n0:Console application.\n1:GUI Application (wxWidgets).");
255 result = new std::string(res);
261 const bool modelCDMAppli::Refresh(std::string*& result)
263 std::cout << "refreshing appli" << std::endl;
264 this->type = wxDIR_DIRS;
266 std::vector<bool> checked(this->children.size(), false);
267 std::vector<bool> checkedApplications(this->applications.size(), false);
270 wxDir dir(crea::std2wx((this->path).c_str()));
274 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
277 std::string stdfileName = crea::wx2std(fileName);
279 if(stdfileName != "template_appli" && stdfileName != "template_wx_appli")
281 std::string applicationName = stdfileName;
282 //check if application already exist
284 for (int i = 0; !found && i < (int)(this->applications.size()); i++)
286 if (this->applications[i]->GetName() == applicationName)
289 int pos = std::find(this->children.begin(), this->children.end(), this->applications[i]) - this->children.begin();
291 checkedApplications[i] = true;
292 if(!this->applications[i]->Refresh(result))
298 modelCDMApplication* application= new modelCDMApplication(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
299 this->applications.push_back(application);
300 this->children.push_back(application);
305 //check if folder already exist
307 for (int i = 0; !found && i < (int)(this->children.size()); i++)
309 if (this->children[i]->GetName() == stdfileName)
314 if(!this->children[i]->Refresh(result))
320 modelCDMFolder* folder= new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
321 this->children.push_back(folder);
324 cont = dir.GetNext(&fileName);
327 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
330 std::string stdfileName = crea::wx2std(fileName);
332 //if CMakeLists, create CMakeLists
333 if(stdfileName == "CMakeLists.txt")
335 if (this->CMakeLists == NULL)
337 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
338 this->children.push_back(this->CMakeLists);
342 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
344 if(!this->CMakeLists->Refresh(result))
348 //if is an unknown file, create file
352 for (int i = 0; !found && i < (int)(this->children.size()); i++)
354 if (this->children[i]->GetName() == stdfileName)
358 if(!this->children[i]->Refresh(result))
365 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
366 this->children.push_back(file);
370 cont = dir.GetNext(&fileName);
374 for (int i = 0; i < (int)(checkedApplications.size()); i++)
376 if(!checkedApplications[i])
378 this->applications.erase(this->applications.begin()+i);
379 checkedApplications.erase(checkedApplications.begin()+i);
383 for (int i = 0; i < (int)(checked.size()); i++)
387 delete this->children[i];
388 this->children.erase(this->children.begin()+i);
389 checked.erase(checked.begin()+i);
393 this->SortChildren();
394 std::sort(this->applications.begin(), this->applications.end(), CompareNodeItem);
398 void modelCDMAppli::CheckStructure(std::map<std::string, bool>& properties)
401 if(this->CMakeLists != NULL)
403 //set properties parameters based on model
404 for (int i = 0; i < (int)(this->applications.size()); i++)
405 properties["appli add " + this->applications[i]->GetName()] = false;
408 std::ifstream confFile;
409 confFile.open((this->CMakeLists->GetPath()).c_str());
411 //take everything that is not commented
412 std::string fileContent;
415 std::vector<std::string> words;
416 while(confFile.is_open() && !confFile.eof())
418 std::getline(confFile,word, '\n');
421 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
422 if (words.size() > 0)
425 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
426 for (int i = 0; i < (int)(words.size()); i++)
428 if(words[i].substr(0,2) == "//")
430 fileContent += words[i] + " ";
436 //check every instruction
437 std::stringstream ss(fileContent);
440 std::getline(ss,word, '(');
442 //check instruction name
443 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
446 if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
448 std::getline(ss,word, ')');
449 //std::cout << word << std::endl;
450 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
452 if (words.size() > 0)
455 properties["appli add " + words[0]] = true;
463 //check libraries' structure
464 for (int i = 0; i < (int)(this->applications.size()); i++)
466 properties["application " + this->applications[i]->GetName()] = true;
467 this->applications[i]->CheckStructure(properties);