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"
41 #include "CDMUtilities.h"
45 modelCDMLibrary::modelCDMLibrary()
49 modelCDMLibrary::modelCDMLibrary(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
51 std::cout << "creating library: " + path + "\n";
52 this->parent = parent;
56 this->path = CDMUtilities::fixPath(path);
58 this->type = wxDIR_DIRS;
63 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
65 std::ifstream confFile;
66 confFile.open((pathMakeLists).c_str());
69 while(confFile.is_open() && !confFile.eof())
72 std::getline(confFile,word,'(');
73 std::vector<std::string> wordBits;
74 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
76 if(wordBits[wordBits.size()-1] == "SET")
79 std::getline(confFile,word,')');
80 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
81 if(wordBits[0] == "LIBRARY_NAME")
84 for (int i = 2; i < wordBits.size(); i++)
86 word += " " + wordBits[i];
89 this->nameLibrary = word;
96 //add library contents
98 this->children.clear();
99 wxDir dir(crea::std2wx((this->path).c_str()));
104 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
107 std::string stdfileName = crea::wx2std(fileName);
109 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
110 this->folders.push_back(folder);
111 this->children.push_back(folder);
113 cont = dir.GetNext(&fileName);
116 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
119 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 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
133 cont = dir.GetNext(&fileName);
136 this->SortChildren();
137 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
140 modelCDMLibrary::~modelCDMLibrary()
144 const std::string& modelCDMLibrary::GetNameLibrary() const
146 return this->nameLibrary;
149 bool modelCDMLibrary::SetNameLibrary(const std::string& fileName, std::string*& result)
151 std::vector<std::string> words;
152 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
153 std::string fileNameReal = words[0];
154 for (int i = 1; i < words.size(); i++)
156 fileNameReal += "-" + words[i];
160 //opening original cmakelists
161 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
164 result = new std::string("CMakeLists.txt file failed to open.");
167 //opening temporal cmakelists
168 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
171 result = new std::string("CMakeLists.txt.tmp file failed to open.");
174 //copying contents from original to temporal and making changes
175 while (getline(in, line))
177 if(line.find("SET ( LIBRARY_NAME") != std::string::npos)
178 line = "SET ( LIBRARY_NAME " + fileNameReal + " )";
179 out << line << std::endl;
183 //delete old file and rename new file
184 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
185 if(system(renameCommand.c_str()))
187 result = new std::string("An error occurred while running '" + renameCommand + "'.");
191 this->nameLibrary = fileNameReal;
195 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
197 //TODO:: mkdir depending on OS
198 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
199 if(system(command.c_str()))
201 result = new std::string("Error executing: " + command + ".");
204 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
205 this->folders.push_back(folder);
206 this->children.push_back(folder);
211 const bool modelCDMLibrary::Refresh(std::string*& result)
213 std::cout << "refreshing library: " << this->nameLibrary << std::endl;
215 this->type = wxDIR_DIRS;
218 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
220 std::ifstream confFile;
221 confFile.open((pathMakeLists).c_str());
224 while(confFile.is_open() && !confFile.eof())
227 std::getline(confFile,word,'(');
228 std::vector<std::string> wordBits;
229 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
231 if(wordBits[wordBits.size()-1] == "SET")
234 std::getline(confFile,word,')');
235 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
236 if(wordBits[0] == "LIBRARY_NAME")
239 for (int i = 2; i < wordBits.size(); i++)
241 word += " " + wordBits[i];
244 this->nameLibrary = word;
252 std::vector<bool> checked(this->children.size(), false);
253 std::vector<bool> checkedFolders(this->folders.size(), false);
256 wxDir dir(crea::std2wx((this->path).c_str()));
260 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
263 std::string stdfileName = crea::wx2std(fileName);
264 //check if they already exist
266 for (int i = 0; !found && i < this->folders.size(); i++)
268 if (this->folders[i]->GetName() == stdfileName)
271 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
273 checkedFolders[i] = true;
274 if(!this->folders[i]->Refresh(result))
280 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
281 this->folders.push_back(folder);
282 this->children.push_back(folder);
284 cont = dir.GetNext(&fileName);
287 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
290 std::string stdfileName = crea::wx2std(fileName);
292 //if CMakeLists, create CMakeLists
293 if(stdfileName == "CMakeLists.txt")
295 if (this->CMakeLists == NULL)
297 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
298 this->children.push_back(this->CMakeLists);
302 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
304 if(!this->CMakeLists->Refresh(result))
308 //if is an unknown file, check if exist in children
312 for (int i = 0; !found && i < this->children.size(); i++)
314 if (this->children[i]->GetName() == stdfileName)
318 if(!this->children[i]->Refresh(result))
325 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
326 this->children.push_back(file);
330 cont = dir.GetNext(&fileName);
334 for (int i = 0; i < checkedFolders.size(); i++)
336 if(!checkedFolders[i])
338 this->folders.erase(this->folders.begin()+i);
339 checkedFolders.erase(checkedFolders.begin()+i);
343 for (int i = 0; i < checked.size(); i++)
347 delete this->children[i];
348 this->children.erase(this->children.begin()+i);
349 checked.erase(checked.begin()+i);
353 this->SortChildren();
354 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
358 void modelCDMLibrary::CheckStructure(std::map<std::string, bool>& properties)
361 if(this->CMakeLists != NULL)
364 properties["library " + this->name + " lib ${crea_LIBRARIES}"] = false;
365 properties["library " + this->name + " lib ${WXWIDGETS_LIBRARIES}"] = false;
366 properties["library " + this->name + " lib ${KWWidgets_LIBRARIES}"] = false;
367 properties["library " + this->name + " lib ${VTK_LIBRARIES}"] = false;
368 properties["library " + this->name + " lib ${ITK_LIBRARIES}"] = false;
369 properties["library " + this->name + " lib ${GDCM_LIBRARIES}"] = false;
370 properties["library " + this->name + " lib ${BOOST_LIBRARIES}"] = false;
374 std::ifstream confFile;
375 confFile.open((this->CMakeLists->GetPath()).c_str());
377 //take everything that is not commented
378 std::string fileContent;
381 std::vector<std::string> words;
382 while(confFile.is_open() && !confFile.eof())
384 std::getline(confFile,word, '\n');
387 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
388 if (words.size() > 0)
391 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
392 for (int i = 0; i < words.size(); i++)
394 if(words[i].substr(0,2) == "//")
396 fileContent += words[i] + " ";
402 //check every instruction
403 std::stringstream ss(fileContent);
406 std::getline(ss,word, '(');
408 //check instruction name
409 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
412 if (words.size() > 0 && words[words.size()-1] == "SET")
414 std::getline(ss,word, ')');
416 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
417 if (words.size() > 1)
419 if (words[0] == "${LIBRARY_NAME}_LINK_LIBRARIES")
421 for (int i = 1; i < words.size(); i++)
423 properties["library " + this->name + " lib " + words[i]] = true;