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 "CDMUtilities.h"
44 modelCDMLibrary::modelCDMLibrary()
48 modelCDMLibrary::modelCDMLibrary(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
50 std::cout << "creating library: " + path + "\n";
51 this->parent = parent;
55 this->path = CDMUtilities::fixPath(path);
57 this->type = wxDIR_DIRS;
62 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
64 std::ifstream confFile;
65 confFile.open((pathMakeLists).c_str());
68 while(confFile.is_open() && !confFile.eof())
71 std::getline(confFile,word,'(');
72 std::vector<std::string> wordBits;
73 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
75 if(wordBits[wordBits.size()-1] == "SET")
78 std::getline(confFile,word,')');
79 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
80 if(wordBits[0] == "LIBRARY_NAME")
83 for (int i = 2; i < wordBits.size(); i++)
85 word += " " + wordBits[i];
88 this->nameLibrary = word;
95 //add library contents
97 this->children.clear();
98 wxDir dir(crea::std2wx((this->path).c_str()));
103 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
106 std::string stdfileName = crea::wx2std(fileName);
108 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
109 this->folders.push_back(folder);
110 this->children.push_back(folder);
112 cont = dir.GetNext(&fileName);
115 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
118 std::string stdfileName = crea::wx2std(fileName);
120 //if CMakeLists, create CMakeLists
121 if(stdfileName == "CMakeLists.txt")
123 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
124 this->children.push_back(this->CMakeLists);
126 //if is an unknown file, create file
129 this->children.push_back(new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
132 cont = dir.GetNext(&fileName);
135 this->SortChildren();
136 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
139 modelCDMLibrary::~modelCDMLibrary()
143 const std::string& modelCDMLibrary::GetNameLibrary() const
145 return this->nameLibrary;
148 bool modelCDMLibrary::SetNameLibrary(const std::string& fileName, std::string*& result)
150 std::vector<std::string> words;
151 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
152 std::string fileNameReal = words[0];
153 for (int i = 1; i < words.size(); i++)
155 fileNameReal += "-" + words[i];
159 //opening original cmakelists
160 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
163 result = new std::string("CMakeLists.txt file failed to open.");
166 //opening temporal cmakelists
167 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
170 result = new std::string("CMakeLists.txt.tmp file failed to open.");
173 //copying contents from original to temporal and making changes
174 while (getline(in, line))
176 if(line.find("SET ( LIBRARY_NAME") != std::string::npos)
177 line = "SET ( LIBRARY_NAME " + fileNameReal + " )";
178 out << line << std::endl;
182 //delete old file and rename new file
183 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
184 if(system(renameCommand.c_str()))
186 result = new std::string("An error occurred while running '" + renameCommand + "'.");
190 this->nameLibrary = fileNameReal;
194 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
196 //TODO:: mkdir depending on OS
197 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
198 if(system(command.c_str()))
200 result = new std::string("Error executing: " + command + ".");
203 modelCDMFolder* folder = new modelCDMFolder(this, path + CDMUtilities::SLASH + name, name, level + 1);
204 this->folders.push_back(folder);
205 this->children.push_back(folder);
210 const bool modelCDMLibrary::Refresh(std::string*& result)
212 std::cout << "refreshing library: " << this->nameLibrary << std::endl;
214 this->type = wxDIR_DIRS;
217 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
219 std::ifstream confFile;
220 confFile.open((pathMakeLists).c_str());
223 while(confFile.is_open() && !confFile.eof())
226 std::getline(confFile,word,'(');
227 std::vector<std::string> wordBits;
228 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
230 if(wordBits[wordBits.size()-1] == "SET")
233 std::getline(confFile,word,')');
234 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
235 if(wordBits[0] == "LIBRARY_NAME")
238 for (int i = 2; i < wordBits.size(); i++)
240 word += " " + wordBits[i];
243 this->nameLibrary = word;
251 std::vector<bool> checked(this->children.size(), false);
252 std::vector<bool> checkedFolders(this->folders.size(), false);
255 wxDir dir(crea::std2wx((this->path).c_str()));
259 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
262 std::string stdfileName = crea::wx2std(fileName);
263 //check if they already exist
265 for (int i = 0; !found && i < this->folders.size(); i++)
267 if (this->folders[i]->GetName() == stdfileName)
270 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
272 checkedFolders[i] = true;
273 if(!this->folders[i]->Refresh(result))
279 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
280 this->folders.push_back(folder);
281 this->children.push_back(folder);
283 cont = dir.GetNext(&fileName);
286 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
289 std::string stdfileName = crea::wx2std(fileName);
291 //if CMakeLists, create CMakeLists
292 if(stdfileName == "CMakeLists.txt")
294 if (this->CMakeLists == NULL)
296 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
297 this->children.push_back(this->CMakeLists);
301 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
303 if(!this->CMakeLists->Refresh(result))
307 //if is an unknown file, check if exist in children
311 for (int i = 0; !found && i < this->children.size(); i++)
313 if (this->children[i]->GetName() == stdfileName)
317 if(!this->children[i]->Refresh(result))
324 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
325 this->children.push_back(file);
329 cont = dir.GetNext(&fileName);
333 for (int i = 0; i < checkedFolders.size(); i++)
335 if(!checkedFolders[i])
337 this->folders.erase(this->folders.begin()+i);
338 checkedFolders.erase(checkedFolders.begin()+i);
342 for (int i = 0; i < checked.size(); i++)
346 delete this->children[i];
347 this->children.erase(this->children.begin()+i);
348 checked.erase(checked.begin()+i);
352 this->SortChildren();
353 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);