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 "modelCDMLibrary.h"
40 #include <boost/regex.hpp>
42 #include "CDMUtilities.h"
46 modelCDMLibrary::modelCDMLibrary()
50 modelCDMLibrary::modelCDMLibrary(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
52 std::cout << "creating library: " + path + "\n";
53 this->parent = parent;
57 this->path = CDMUtilities::fixPath(path);
59 this->type = wxDIR_DIRS;
64 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
66 std::ifstream confFile;
67 confFile.open((pathMakeLists).c_str());
70 while(confFile.is_open() && !confFile.eof())
73 std::getline(confFile,word,'(');
74 std::vector<std::string> wordBits;
75 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
77 if(wordBits[wordBits.size()-1] == "SET")
80 std::getline(confFile,word,')');
81 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
82 if(wordBits[0] == "LIBRARY_NAME")
85 for (int i = 2; i < (int)(wordBits.size()); i++)
87 word += " " + wordBits[i];
90 this->nameLibrary = 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);
122 //if CMakeLists, create CMakeLists
123 if(stdfileName == "CMakeLists.txt")
125 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
126 this->children.push_back(this->CMakeLists);
128 //if is an unknown file, create file
131 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
134 cont = dir.GetNext(&fileName);
137 this->SortChildren();
138 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
141 modelCDMLibrary::~modelCDMLibrary()
145 const std::string& modelCDMLibrary::GetNameLibrary() const
147 return this->nameLibrary;
150 bool modelCDMLibrary::SetNameLibrary(const std::string& fileName, std::string*& result)
152 std::vector<std::string> words;
153 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
154 std::string fileNameReal = words[0];
155 for (int i = 1; i < (int)(words.size()); i++)
157 fileNameReal += "-" + words[i];
161 //opening original cmakelists
162 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
165 result = new std::string("CMakeLists.txt file failed to open.");
168 //opening temporal cmakelists
169 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
172 result = new std::string("CMakeLists.txt.tmp file failed to open.");
175 //copying contents from original to temporal and making changes
176 while (getline(in, line))
178 if(line.find("SET ( LIBRARY_NAME") != std::string::npos)
179 line = "SET ( LIBRARY_NAME " + fileNameReal + " )";
180 out << line << std::endl;
184 //delete old file and rename new file
186 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
188 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
190 if(system(renameCommand.c_str()))
192 result = new std::string("An error occurred while running '" + renameCommand + "'.");
196 this->nameLibrary = fileNameReal;
200 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
202 //TODO:: mkdir depending on OS
203 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
204 if(system(command.c_str()))
206 result = new std::string("Error executing: " + command + ".");
209 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
210 this->folders.push_back(folder);
211 this->children.push_back(folder);
216 const bool modelCDMLibrary::Refresh(std::string*& result)
218 std::cout << "refreshing library: " << this->nameLibrary << std::endl;
220 this->type = wxDIR_DIRS;
223 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
225 std::ifstream confFile;
226 confFile.open((pathMakeLists).c_str());
229 while(confFile.is_open() && !confFile.eof())
232 std::getline(confFile,word,'(');
233 std::vector<std::string> wordBits;
234 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
236 if(wordBits[wordBits.size()-1] == "SET")
239 std::getline(confFile,word,')');
240 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
241 if(wordBits[0] == "LIBRARY_NAME")
244 for (int i = 2; i < (int)(wordBits.size()); i++)
246 word += " " + wordBits[i];
249 this->nameLibrary = word;
257 std::vector<bool> checked(this->children.size(), false);
258 std::vector<bool> checkedFolders(this->folders.size(), false);
261 wxDir dir(crea::std2wx((this->path).c_str()));
265 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
268 std::string stdfileName = crea::wx2std(fileName);
269 //check if they already exist
271 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
273 if (this->folders[i]->GetName() == stdfileName)
276 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
278 checkedFolders[i] = true;
279 if(!this->folders[i]->Refresh(result))
285 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
286 this->folders.push_back(folder);
287 this->children.push_back(folder);
289 cont = dir.GetNext(&fileName);
292 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
295 std::string stdfileName = crea::wx2std(fileName);
297 //if CMakeLists, create CMakeLists
298 if(stdfileName == "CMakeLists.txt")
300 if (this->CMakeLists == NULL)
302 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
303 this->children.push_back(this->CMakeLists);
307 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
309 if(!this->CMakeLists->Refresh(result))
313 //if is an unknown file, check if exist in children
317 for (int i = 0; !found && i < (int)(this->children.size()); i++)
319 if (this->children[i]->GetName() == stdfileName)
323 if(!this->children[i]->Refresh(result))
330 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
331 this->children.push_back(file);
335 cont = dir.GetNext(&fileName);
339 for (int i = 0; i < (int)(checkedFolders.size()); i++)
341 if(!checkedFolders[i])
343 this->folders.erase(this->folders.begin()+i);
344 checkedFolders.erase(checkedFolders.begin()+i);
348 for (int i = 0; i < (int)(checked.size()); i++)
352 delete this->children[i];
353 this->children.erase(this->children.begin()+i);
354 checked.erase(checked.begin()+i);
358 this->SortChildren();
359 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
363 void modelCDMLibrary::CheckStructure(std::map<std::string, bool>& properties)
366 if(this->CMakeLists != NULL)
369 properties["library " + this->name + " lib ${crea_LIBRARIES}"] = false;
370 properties["library " + this->name + " lib ${WXWIDGETS_LIBRARIES}"] = false;
371 properties["library " + this->name + " lib ${KWWidgets_LIBRARIES}"] = false;
372 properties["library " + this->name + " lib ${VTK_LIBRARIES}"] = false;
373 properties["library " + this->name + " lib ${ITK_LIBRARIES}"] = false;
374 properties["library " + this->name + " lib ${GDCM_LIBRARIES}"] = false;
375 properties["library " + this->name + " lib ${BOOST_LIBRARIES}"] = false;
379 std::ifstream confFile;
380 confFile.open((this->CMakeLists->GetPath()).c_str());
382 //take everything that is not commented
383 std::string fileContent;
386 std::vector<std::string> words;
387 while(confFile.is_open() && !confFile.eof())
389 std::getline(confFile,word, '\n');
392 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
393 if (words.size() > 0)
396 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
397 for (int i = 0; i < (int)(words.size()); i++)
399 if(words[i].substr(0,2) == "//")
401 fileContent += words[i] + " ";
407 //check every instruction
408 std::stringstream ss(fileContent);
411 std::getline(ss,word, '(');
413 //check instruction name
414 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
417 if (words.size() > 0 && words[words.size()-1] == "SET")
419 std::getline(ss,word, ')');
421 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
422 if (words.size() > 1)
424 if (words[0] == "${LIBRARY_NAME}_LINK_LIBRARIES")
426 for (int i = 1; i < (int)(words.size()); i++)
428 properties["library " + this->name + " lib " + words[i]] = true;
438 std::map<std::string, bool> modelCDMLibrary::Get3rdPartyLibraries()
440 std::map<std::string, std::string> correspondence;
441 correspondence["${crea_LIBRARIES}"] = "Crea";
442 correspondence["${WXWIDGETS_LIBRARIES}"] = "WxWidgets";
443 correspondence["${KWWidgets_LIBRARIES}"] = "KWWidgets";
444 correspondence["${VTK_LIBRARIES}"] = "VTK";
445 correspondence["${ITK_LIBRARIES}"] = "ITK";
446 correspondence["${GDCM_LIBRARIES}"] = "GDCM";
447 correspondence["${BOOST_LIBRARIES}"] = "Boost";
448 std::map<std::string, bool> res;
450 res["WxWidgets"] = false;
451 res["KWWidgets"] = false;
455 res["Boost"] = false;
457 if (this->HasCMakeLists())
459 std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
461 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{LIBRARY_NAME\\}_LINK_LIBRARIES(([\\s]|#[^\\n]*\\n)+([\\$\\{\\}\\w\\d]+|\"(?:[^\"\\\\]|\\\\.)*\"))*([\\s]|#[^\\n]*\\n)*\\)");
462 std::string::const_iterator start, end;
463 start = CMfile.begin();
465 boost::match_results<std::string::const_iterator> what;
466 boost::match_flag_type flags = boost::match_default;
467 if(boost::regex_search(start, end, what, expression, flags))
470 expression = boost::regex("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*\\$\\{LIBRARY_NAME\\}_LINK_LIBRARIES");
471 std::string::const_iterator start1, end1;
472 start1 = what[0].first;
473 end1 = what[0].second;
474 boost::match_results<std::string::const_iterator> what1;
475 if(boost::regex_search(start1, end1, what1, expression, flags))
477 expression = boost::regex("(#[^\\n]*\\n|\\s*\\$\\{\\w+\\})");
478 std::string::const_iterator start2, end2;
479 start2 = what1[0].second;
480 end2 = what[0].second;
481 boost::match_results<std::string::const_iterator> what2;
482 while(boost::regex_search(start2, end2, what2, expression, flags))
484 if(what2.str()[0] != '#')
486 std::string dete = what2.str();
487 CDMUtilities::normalizeStr(dete);
488 if(correspondence.find(dete) != correspondence.end())
489 res[correspondence[dete]] = true;
491 start2 = what2[0].second;
499 bool modelCDMLibrary::Set3rdPartyLibrary(const std::string& library_name, const bool& toInclude)
501 std::map<std::string, std::string> correspondence;
503 correspondence["Crea"] = "${crea_LIBRARIES}";
504 correspondence["WxWidgets"] = "${WXWIDGETS_LIBRARIES}";
505 correspondence["KWWidgets"] = "${KWWidgets_LIBRARIES}";
506 correspondence["VTK"] = "${VTK_LIBRARIES}";
507 correspondence["ITK"] = "${ITK_LIBRARIES}";
508 correspondence["GDCM"] = "${GDCM_LIBRARIES}";
509 correspondence["Boost"] = "${BOOST_LIBRARIES}";
511 if (correspondence.find(library_name) != correspondence.end())
513 std::string library_command = correspondence[library_name];
514 if (this->HasCMakeLists())
516 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath());
518 // look at every syntax element
519 for (int i = 0; i < cmlFile.size(); ++i)
521 // if the element is a command and is a SET command
522 if (cmlFile[i].first == "command" && cmlFile[i].second[0] == "SET")
524 // search first parameter
526 while (pos < cmlFile[i].second.size())
528 // see if it is ${LIBRARY_NAME}_LINK_LIBRARIES
529 if (cmlFile[i].second[pos] == "${LIBRARY_NAME}_LINK_LIBRARIES")
533 // look for all the third party libraries included
534 while (pos < cmlFile[i].second.size() && cmlFile[i].second[pos] != ")")
536 // if is library_command then found
537 if (cmlFile[i].second[pos] == library_command)
540 //if toInclude is false the make it a comment
543 cmlFile[i].second[pos] = "#" + cmlFile[i].second[pos];
544 std::cout << "library commented: " << library_name << std::endl;
548 else if (toInclude && cmlFile[i].second[pos][0] == '#')
550 std::vector<std::string> segments;
551 CDMUtilities::splitter::split(segments, cmlFile[i].second[pos], "#", CDMUtilities::splitter::no_empties);
554 for (int j = 0; j < segments[0].size(); ++j)
556 if(isspace(segments[0][j]))
558 segments[0].erase(j,1);
563 if(segments[0].size() && segments[0] == library_command)
565 std::cout << "library uncommented: " << library_name << std::endl;
567 while(cmlFile[i].second[pos][0] == '#')
569 cmlFile[i].second[pos].erase(0,1);
578 // if the library was not found and is an inclusion then include it.
579 if (!found && toInclude && pos < cmlFile[i].second.size() && cmlFile[i].second[pos] == ")")
581 cmlFile[i].second.insert(cmlFile[i].second.begin()+pos, library_command);
582 cmlFile[i].second.insert(cmlFile[i].second.begin()+pos+1, "\n");
583 std::cout << "library included: " << library_name << std::endl;
586 // if it is the first parameter but is not ${LIBRARY_NAME}_LINK_LIBRARIES then finish with this command
587 else if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '#' && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != ')')
596 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath(), cmlFile);
602 std::map<std::string, bool> modelCDMLibrary::GetCustomLibraries()
604 std::map<std::string, bool> res;
609 bool modelCDMLibrary::SetCustomLibrary(const std::string& library_name, const bool& toInclude)