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 < (int)(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 < (int)(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
185 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
187 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
189 if(system(renameCommand.c_str()))
191 result = new std::string("An error occurred while running '" + renameCommand + "'.");
195 this->nameLibrary = fileNameReal;
199 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
201 //TODO:: mkdir depending on OS
202 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
203 if(system(command.c_str()))
205 result = new std::string("Error executing: " + command + ".");
208 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
209 this->folders.push_back(folder);
210 this->children.push_back(folder);
215 const bool modelCDMLibrary::Refresh(std::string*& result)
217 std::cout << "refreshing library: " << this->nameLibrary << std::endl;
219 this->type = wxDIR_DIRS;
222 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
224 std::ifstream confFile;
225 confFile.open((pathMakeLists).c_str());
228 while(confFile.is_open() && !confFile.eof())
231 std::getline(confFile,word,'(');
232 std::vector<std::string> wordBits;
233 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
235 if(wordBits[wordBits.size()-1] == "SET")
238 std::getline(confFile,word,')');
239 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
240 if(wordBits[0] == "LIBRARY_NAME")
243 for (int i = 2; i < (int)(wordBits.size()); i++)
245 word += " " + wordBits[i];
248 this->nameLibrary = word;
256 std::vector<bool> checked(this->children.size(), false);
257 std::vector<bool> checkedFolders(this->folders.size(), false);
260 wxDir dir(crea::std2wx((this->path).c_str()));
264 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
267 std::string stdfileName = crea::wx2std(fileName);
268 //check if they already exist
270 for (int i = 0; !found && i < (int)(this->folders.size()); i++)
272 if (this->folders[i]->GetName() == stdfileName)
275 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
277 checkedFolders[i] = true;
278 if(!this->folders[i]->Refresh(result))
284 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
285 this->folders.push_back(folder);
286 this->children.push_back(folder);
288 cont = dir.GetNext(&fileName);
291 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
294 std::string stdfileName = crea::wx2std(fileName);
296 //if CMakeLists, create CMakeLists
297 if(stdfileName == "CMakeLists.txt")
299 if (this->CMakeLists == NULL)
301 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
302 this->children.push_back(this->CMakeLists);
306 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
308 if(!this->CMakeLists->Refresh(result))
312 //if is an unknown file, check if exist in children
316 for (int i = 0; !found && i < (int)(this->children.size()); i++)
318 if (this->children[i]->GetName() == stdfileName)
322 if(!this->children[i]->Refresh(result))
329 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
330 this->children.push_back(file);
334 cont = dir.GetNext(&fileName);
338 for (int i = 0; i < (int)(checkedFolders.size()); i++)
340 if(!checkedFolders[i])
342 this->folders.erase(this->folders.begin()+i);
343 checkedFolders.erase(checkedFolders.begin()+i);
347 for (int i = 0; i < (int)(checked.size()); i++)
351 delete this->children[i];
352 this->children.erase(this->children.begin()+i);
353 checked.erase(checked.begin()+i);
357 this->SortChildren();
358 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
362 void modelCDMLibrary::CheckStructure(std::map<std::string, bool>& properties)
365 if(this->CMakeLists != NULL)
368 properties["library " + this->name + " lib ${crea_LIBRARIES}"] = false;
369 properties["library " + this->name + " lib ${WXWIDGETS_LIBRARIES}"] = false;
370 properties["library " + this->name + " lib ${KWWidgets_LIBRARIES}"] = false;
371 properties["library " + this->name + " lib ${VTK_LIBRARIES}"] = false;
372 properties["library " + this->name + " lib ${ITK_LIBRARIES}"] = false;
373 properties["library " + this->name + " lib ${GDCM_LIBRARIES}"] = false;
374 properties["library " + this->name + " lib ${BOOST_LIBRARIES}"] = false;
378 std::ifstream confFile;
379 confFile.open((this->CMakeLists->GetPath()).c_str());
381 //take everything that is not commented
382 std::string fileContent;
385 std::vector<std::string> words;
386 while(confFile.is_open() && !confFile.eof())
388 std::getline(confFile,word, '\n');
391 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
392 if (words.size() > 0)
395 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
396 for (int i = 0; i < (int)(words.size()); i++)
398 if(words[i].substr(0,2) == "//")
400 fileContent += words[i] + " ";
406 //check every instruction
407 std::stringstream ss(fileContent);
410 std::getline(ss,word, '(');
412 //check instruction name
413 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
416 if (words.size() > 0 && words[words.size()-1] == "SET")
418 std::getline(ss,word, ')');
420 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
421 if (words.size() > 1)
423 if (words[0] == "${LIBRARY_NAME}_LINK_LIBRARIES")
425 for (int i = 1; i < (int)(words.size()); i++)
427 properties["library " + this->name + " lib " + words[i]] = true;