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"
40 #include <boost/regex.hpp>
42 #include "CDMUtilities.h"
46 modelCDMApplication::modelCDMApplication()
51 modelCDMApplication::modelCDMApplication(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
53 std::cout << "creating application: " + path + "\n";
54 this->parent = parent;
55 this->mainFile = NULL;
59 this->path = CDMUtilities::fixPath(path);
61 this->type = wxDIR_DIRS;
65 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
67 std::ifstream confFile;
68 confFile.open((pathMakeLists).c_str());
71 while(confFile.is_open() && !confFile.eof())
74 std::getline(confFile,word,'(');
75 std::vector<std::string> wordBits;
76 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
78 if(wordBits[wordBits.size()-1] == "SET")
81 std::getline(confFile,word,')');
82 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
83 if(wordBits[0] == "EXE_NAME")
86 for (int i = 2; i < (int)(wordBits.size()); i++)
88 word += " " + wordBits[i];
91 this->executableName = word;
97 //add library contents
99 this->children.clear();
100 wxDir dir(crea::std2wx((this->path).c_str()));
105 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
108 std::string stdfileName = crea::wx2std(fileName);
110 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
111 this->folders.push_back(folder);
112 this->children.push_back(folder);
114 cont = dir.GetNext(&fileName);
117 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
120 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 is cxx or cpp check if is the main file.
133 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
135 std::ifstream fileStream((this->path + CDMUtilities::SLASH + stdfileName).c_str());
137 if (fileStream.is_open())
139 std::string fileContent = "";
140 char ch = fileStream.get();
141 while(!fileStream.eof())
143 fileContent.push_back(ch);
144 ch = fileStream.get();
148 boost::regex expression("^\\h*IMPLEMENT_APP[#\\s\\(]");
149 std::string::const_iterator start, end;
150 start = fileContent.begin();
151 end = fileContent.end();
152 boost::match_results<std::string::const_iterator> what;
153 boost::match_flag_type flags = boost::match_default;
154 if(boost::regex_search(start, end, what, expression, flags))
156 std::cout << "found main wxwidgets file: " << stdfileName << std::endl;
157 this->mainFile = file;
161 expression = boost::regex("^\\h*int\\h+main[#\\s\\(]");
162 start = fileContent.begin();
163 end = fileContent.end();
164 if(boost::regex_search(start, end, what, expression, flags))
166 std::cout << "found main console file: " << stdfileName << std::endl;
167 this->mainFile = file;
173 this->children.push_back(file);
176 cont = dir.GetNext(&fileName);
179 this->SortChildren();
180 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
183 modelCDMApplication::~modelCDMApplication()
187 const std::string& modelCDMApplication::GetExecutableName() const
189 return this->executableName;
192 modelCDMFile* modelCDMApplication::GetMainFile() const
194 return this->mainFile;
197 bool modelCDMApplication::SetExecutableName(const std::string& fileName, std::string*& result)
199 std::vector<std::string> words;
200 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
201 std::string fileNameReal = words[0];
202 for (int i = 1; i < (int)(words.size()); i++)
204 fileNameReal += "-" + words[i];
208 //opening original cmakelists
209 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
212 result = new std::string("CMakeLists.txt file failed to open.");
215 //opening temporal cmakelists
216 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
219 result = new std::string("CMakeLists.txt.tmp file failed to open.");
222 //copying contents from original to temporal and making changes
223 while (getline(in, line))
225 if(line.find("SET ( EXE_NAME") != std::string::npos)
226 line = "SET ( EXE_NAME " + fileNameReal + " )";
227 out << line << std::endl;
231 //delete old file and rename new file
233 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
235 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
238 if(system(renameCommand.c_str()))
240 result = new std::string("An error occurred while running '" + renameCommand + "'.");
244 this->executableName = fileNameReal;
248 modelCDMFolder* modelCDMApplication::CreateFolder(const std::string& name, std::string*& result)
250 //TODO:: mkdir depending on OS
251 std::string command = "mkdir " + path + CDMUtilities::SLASH + name;
252 if(system(command.c_str()))
254 result = new std::string("Error executing: " + command + ".");
257 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
258 this->folders.push_back(folder);
259 this->children.push_back(folder);
264 const bool modelCDMApplication::Refresh(std::string*& result)
266 this->mainFile = NULL;
267 std::cout << "refreshing application: " << this->executableName << std::endl;
269 this->type = wxDIR_DIRS;
272 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
274 std::ifstream confFile;
275 confFile.open((pathMakeLists).c_str());
278 while(confFile.is_open() && !confFile.eof())
281 std::getline(confFile,word,'(');
282 std::vector<std::string> wordBits;
283 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
285 if(wordBits[wordBits.size()-1] == "SET")
288 std::getline(confFile,word,')');
289 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
290 if(wordBits[0] == "EXE_NAME")
293 for (int i = 2; i < (int)(wordBits.size()); i++)
295 word += " " + wordBits[i];
298 this->executableName = word;
306 std::vector<bool> checked(this->children.size(), false);
307 std::vector<bool> checkedFolders(this->folders.size(), false);
310 wxDir dir(crea::std2wx((this->path).c_str()));
314 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
317 std::string stdfileName = crea::wx2std(fileName);
318 std::string applicationName = stdfileName;
319 //check if they already exist
321 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
323 if (this->folders[i]->GetName() == applicationName)
326 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
328 checkedFolders[i] = true;
329 if(!this->folders[i]->Refresh(result))
335 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
336 this->folders.push_back(folder);
337 this->children.push_back(folder);
339 cont = dir.GetNext(&fileName);
342 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
345 std::string stdfileName = crea::wx2std(fileName);
347 //if CMakeLists, create CMakeLists
348 if(stdfileName == "CMakeLists.txt")
350 if (this->CMakeLists == NULL)
352 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
353 this->children.push_back(this->CMakeLists);
357 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
359 if(!this->CMakeLists->Refresh(result))
363 //if is an unknown file, create file
367 for (int i = 0; !found && i < (int)(this->children.size()); i++)
369 if (this->children[i]->GetName() == stdfileName)
374 if(!this->children[i]->Refresh(result))
377 std::string extension = stdfileName.substr(stdfileName.size()-4);
378 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
380 std::ifstream fileStream;
382 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
383 while (fileStream.is_open() && !fileStream.eof())
386 std::getline(fileStream,word,'(');
387 std::vector<std::string> wordBits;
388 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
389 if (wordBits[wordBits.size() - 1] == "main" || wordBits[wordBits.size() - 1] == "IMPLEMENT_APP")
391 this->mainFile = dynamic_cast<modelCDMFile*>(children[i]);
401 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
403 std::string extension = stdfileName.substr(stdfileName.size()-4);
404 if (mainFile == NULL && (extension == ".cxx" || extension == ".cpp"))
406 std::ifstream fileStream;
408 fileStream.open((this->path + CDMUtilities::SLASH + stdfileName).c_str());
409 while (fileStream.is_open() && !fileStream.eof())
412 std::getline(fileStream,word,'(');
413 std::vector<std::string> wordBits;
414 CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
415 if (wordBits[wordBits.size() - 1] == "main" || wordBits[wordBits.size() - 1] == "IMPLEMENT_APP")
417 this->mainFile = file;
423 this->children.push_back(file);
427 cont = dir.GetNext(&fileName);
431 for (int i = 0; i < (int)(checkedFolders.size()); i++)
433 if(!checkedFolders[i])
435 this->folders.erase(this->folders.begin()+i);
436 checkedFolders.erase(checkedFolders.begin()+i);
440 for (int i = 0; i < (int)(checked.size()); i++)
444 delete this->children[i];
445 this->children.erase(this->children.begin()+i);
446 checked.erase(checked.begin()+i);
450 this->SortChildren();
454 void modelCDMApplication::CheckStructure(std::map<std::string, bool>& properties)
457 if(this->CMakeLists != NULL)
459 //set default properties
460 properties["application " + this->name + " lib ${crea_LIBRARIES}"] = false;
461 properties["application " + this->name + " lib ${WXWIDGETS_LIBRARIES}"] = false;
462 properties["application " + this->name + " lib ${KWWidgets_LIBRARIES}"] = false;
463 properties["application " + this->name + " lib ${VTK_LIBRARIES}"] = false;
464 properties["application " + this->name + " lib ${ITK_LIBRARIES}"] = false;
465 properties["application " + this->name + " lib ${GDCM_LIBRARIES}"] = false;
466 properties["application " + this->name + " lib ${BOOST_LIBRARIES}"] = false;
469 std::ifstream confFile;
470 confFile.open((this->CMakeLists->GetPath()).c_str());
472 //take everything that is not commented
473 std::string fileContent;
476 std::vector<std::string> words;
477 while(confFile.is_open() && !confFile.eof())
479 std::getline(confFile,word, '\n');
482 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
483 if (words.size() > 0)
486 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
487 for (int i = 0; i < (int)(words.size()); i++)
489 if(words[i].substr(0,2) == "//")
491 fileContent += words[i] + " ";
497 //check every instruction
498 std::stringstream ss(fileContent);
501 std::getline(ss,word, '(');
503 //check instruction name
504 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
507 if (words.size() > 0 && words[words.size()-1] == "SET")
509 std::getline(ss,word, ')');
511 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
512 if (words.size() > 1)
514 if (words[0] == "${EXE_NAME}_LINK_LIBRARIES")
516 for (int i = 1; i < (int)(words.size()); i++)
518 properties["application " + this->name + " lib " + words[i]] = true;
523 else if (words.size() > 0 && words[words.size()-1] == "INCLUDE_DIRECTORIES")
525 std::getline(ss,word, ')');
527 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
529 for (int i = 0; i < (int)(words.size()); i++)
531 properties["application " + this->name + " dir " + words[i]] = true;
541 std::map<std::string, bool> modelCDMApplication::Get3rdPartyLibraries()
543 std::map<std::string, std::string> correspondence;
544 correspondence["${crea_LIBRARIES}"] = "Crea";
545 correspondence["${WXWIDGETS_LIBRARIES}"] = "WxWidgets";
546 correspondence["${KWWidgets_LIBRARIES}"] = "KWWidgets";
547 correspondence["${VTK_LIBRARIES}"] = "VTK";
548 correspondence["${ITK_LIBRARIES}"] = "ITK";
549 correspondence["${GDCM_LIBRARIES}"] = "GDCM";
550 correspondence["${BOOST_LIBRARIES}"] = "Boost";
551 std::map<std::string, bool> res;
553 res["WxWidgets"] = false;
554 res["KWWidgets"] = false;
558 res["Boost"] = false;
560 if (this->HasCMakeLists())
563 std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
565 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{EXE_NAME\\}_LINK_LIBRARIES(([\\s]|#[^\\n]*\\n)+([\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
566 std::string::const_iterator start, end;
567 start = CMfile.begin();
569 boost::match_results<std::string::const_iterator> what;
570 boost::match_flag_type flags = boost::match_default;
571 if(boost::regex_search(start, end, what, expression, flags))
574 expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{EXE_NAME\\}_LINK_LIBRARIES");
575 std::string::const_iterator start1, end1;
576 start1 = what[0].first;
577 end1 = what[0].second;
578 boost::match_results<std::string::const_iterator> what1;
579 if(boost::regex_search(start1, end1, what1, expression, flags))
581 expression = boost::regex("(#[^\\n]*\\n|\\s*\\$\\{\\w+\\})");
582 std::string::const_iterator start2, end2;
583 start2 = what1[0].second;
584 end2 = what[0].second;
585 boost::match_results<std::string::const_iterator> what2;
586 while(boost::regex_search(start2, end2, what2, expression, flags))
588 if(what2.str()[0] != '#')
590 std::string dete = what2.str();
591 CDMUtilities::normalizeStr(dete);
592 if(correspondence.find(dete) != correspondence.end())
593 res[correspondence[dete]] = true;
595 start2 = what2[0].second;
603 bool modelCDMApplication::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude)
605 std::map<std::string, std::string> correspondence;
607 correspondence["Crea"] = "${crea_LIBRARIES}";
608 correspondence["WxWidgets"] = "${WXWIDGETS_LIBRARIES}";
609 correspondence["KWWidgets"] = "${KWWidgets_LIBRARIES}";
610 correspondence["VTK"] = "${VTK_LIBRARIES}";
611 correspondence["ITK"] = "${ITK_LIBRARIES}";
612 correspondence["GDCM"] = "${GDCM_LIBRARIES}";
613 correspondence["Boost"] = "${BOOST_LIBRARIES}";
615 if (correspondence.find(library_name) != correspondence.end())
617 std::string library_command = correspondence[library_name];
618 if (this->HasCMakeLists())
620 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath());
622 // look at every syntax element
623 for (int i = 0; i < cmlFile.size(); ++i)
625 // if the element is a command and is a SET command
626 if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET")
628 // search first parameter
630 while (pos < cmlFile[i].second.size())
632 // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES
633 if (cmlFile[i].second[pos] == "${EXE_NAME}_LINK_LIBRARIES")
637 // look for all the third party libraries included
638 while (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] != ")")
640 // if is library_command then found
641 if (cmlFile[i].second[pos] == library_command)
644 //if toInclude is false the make it a comment
647 cmlFile[i].second[pos] = "#" + cmlFile[i].second[pos];
648 std::cout << "library commented: " << library_name << std::endl;
652 else if (toInclude && cmlFile[i].second[pos][0] == '#')
654 std::vector<std::string> segments;
655 CDMUtilities::splitter::split(segments, cmlFile[i].second[pos], "#", CDMUtilities::splitter::no_empties);
658 for (int j = 0; j < segments[0].size(); ++j)
660 if(isspace(segments[0][j]))
662 segments[0].erase(j,1);
667 if(segments[0].size() && segments[0] == library_command)
670 while(cmlFile[i].second[pos][0] == '#')
672 cmlFile[i].second[pos].erase(0,1);
674 std::cout << "library uncommented: " << library_name << std::endl;
682 //if the library was not found and is an inclusion then include it
683 if (!found && toInclude && pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == ")")
685 cmlFile[i].second.insert(cmlFile[i].second.begin()+pos, library_command);
686 cmlFile[i].second.insert(cmlFile[i].second.begin()+pos+1, "\n");
687 std::cout << "library included: " << library_name << std::endl;
690 // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command
691 else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')')
700 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath(), cmlFile);
706 std::map<std::string, bool> modelCDMApplication::GetCustomLibraries()
708 std::map<std::string, bool> res;
713 bool modelCDMApplication::SetCustomLibrary(const std::string& library_name, const bool& toInclude)