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(const std::string& path, const std::string& name, const int& level)
50 std::cout << "creating library: " + path + "\n";
54 this->path = CDMUtilities::fixPath(path);
56 this->type = wxDIR_DIRS;
61 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
63 std::ifstream confFile;
64 confFile.open((pathMakeLists).c_str());
67 while(confFile.is_open() && !confFile.eof())
70 std::getline(confFile,word,'(');
71 std::vector<std::string> wordBits;
72 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
74 if(wordBits[wordBits.size()-1] == "SET")
77 std::getline(confFile,word,')');
78 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
79 if(wordBits[0] == "LIBRARY_NAME")
82 for (int i = 2; i < wordBits.size(); i++)
84 word += " " + wordBits[i];
87 this->nameLibrary = word;
94 //add library contents
96 this->children.clear();
97 wxDir dir(crea::std2wx((this->path).c_str()));
102 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
105 std::string stdfileName = crea::wx2std(fileName);
107 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
108 this->folders.push_back(folder);
109 this->children.push_back(folder);
111 cont = dir.GetNext(&fileName);
114 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
117 std::string stdfileName = crea::wx2std(fileName);
119 //if CMakeLists, create CMakeLists
120 if(stdfileName == "CMakeLists.txt")
122 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
123 this->children.push_back(this->CMakeLists);
125 //if is an unknown file, create file
128 this->children.push_back(new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
131 cont = dir.GetNext(&fileName);
134 this->SortChildren();
135 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);
138 modelCDMLibrary::~modelCDMLibrary()
142 const std::string& modelCDMLibrary::GetNameLibrary() const
144 return this->nameLibrary;
147 bool modelCDMLibrary::SetNameLibrary(const std::string& fileName, std::string*& result)
149 std::vector<std::string> words;
150 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
151 std::string fileNameReal = words[0];
152 for (int i = 1; i < words.size(); i++)
154 fileNameReal += "-" + words[i];
158 //opening original cmakelists
159 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
162 result = new std::string("CMakeLists.txt file failed to open.");
165 //opening temporal cmakelists
166 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
169 result = new std::string("CMakeLists.txt.tmp file failed to open.");
172 //copying contents from original to temporal and making changes
173 while (getline(in, line))
175 if(line.find("SET ( LIBRARY_NAME") != std::string::npos)
176 line = "SET ( LIBRARY_NAME " + fileNameReal + " )";
177 out << line << std::endl;
181 //delete old file and rename new file
182 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
183 if(system(renameCommand.c_str()))
185 result = new std::string("An error occurred while running '" + renameCommand + "'.");
189 this->nameLibrary = fileNameReal;
193 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
195 //TODO:: mkdir depending on OS
196 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
197 if(system(command.c_str()))
199 result = new std::string("Error executing: " + command + ".");
202 modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
203 this->folders.push_back(folder);
204 this->children.push_back(folder);
209 const bool modelCDMLibrary::Refresh(std::string*& result)
211 std::cout << "refreshing library: " << this->nameLibrary << std::endl;
213 this->type = wxDIR_DIRS;
216 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
218 std::ifstream confFile;
219 confFile.open((pathMakeLists).c_str());
222 while(confFile.is_open() && !confFile.eof())
225 std::getline(confFile,word,'(');
226 std::vector<std::string> wordBits;
227 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
229 if(wordBits[wordBits.size()-1] == "SET")
232 std::getline(confFile,word,')');
233 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
234 if(wordBits[0] == "LIBRARY_NAME")
237 for (int i = 2; i < wordBits.size(); i++)
239 word += " " + wordBits[i];
242 this->nameLibrary = word;
250 std::vector<bool> checked(this->children.size(), false);
251 std::vector<bool> checkedFolders(this->folders.size(), false);
254 wxDir dir(crea::std2wx((this->path).c_str()));
258 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
261 std::string stdfileName = crea::wx2std(fileName);
262 //check if they already exist
264 for (int i = 0; !found && i < this->folders.size(); i++)
266 if (this->folders[i]->GetName() == stdfileName)
269 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
271 checkedFolders[i] = true;
272 if(!this->folders[i]->Refresh(result))
278 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
279 this->folders.push_back(folder);
280 this->children.push_back(folder);
282 cont = dir.GetNext(&fileName);
285 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
288 std::string stdfileName = crea::wx2std(fileName);
290 //if CMakeLists, create CMakeLists
291 if(stdfileName == "CMakeLists.txt")
293 if (this->CMakeLists == NULL)
295 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
296 this->children.push_back(this->CMakeLists);
300 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
302 if(!this->CMakeLists->Refresh(result))
306 //if is an unknown file, check if exist in children
310 for (int i = 0; !found && i < this->children.size(); i++)
312 if (this->children[i]->GetName() == stdfileName)
316 if(!this->children[i]->Refresh(result))
323 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
324 this->children.push_back(file);
328 cont = dir.GetNext(&fileName);
332 for (int i = 0; i < checkedFolders.size(); i++)
334 if(!checkedFolders[i])
336 this->folders.erase(this->folders.begin()+i);
337 checkedFolders.erase(checkedFolders.begin()+i);
341 for (int i = 0; i < checked.size(); i++)
345 delete this->children[i];
346 this->children.erase(this->children.begin()+i);
347 checked.erase(checked.begin()+i);
351 this->SortChildren();
352 std::sort(this->folders.begin(), this->folders.end(), CompareNodeItem);