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)
53 this->path = CDMUtilities::fixPath(path);
55 this->type = wxDIR_DIRS;
60 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
62 std::ifstream confFile;
63 confFile.open((pathMakeLists).c_str());
66 while(confFile.is_open() && !confFile.eof())
69 std::getline(confFile,word,'(');
70 std::vector<std::string> wordBits;
71 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
73 if(wordBits[wordBits.size()-1] == "SET")
76 std::getline(confFile,word,')');
77 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
78 if(wordBits[0] == "LIBRARY_NAME")
81 for (int i = 2; i < wordBits.size(); i++)
83 word += " " + wordBits[i];
86 this->nameLibrary = word;
93 //add library contents
95 this->children.clear();
96 wxDir dir(crea::std2wx((this->path).c_str()));
101 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
104 std::string stdfileName = crea::wx2std(fileName);
106 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
107 this->folders.push_back(folder);
108 this->children.push_back(folder);
110 cont = dir.GetNext(&fileName);
113 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
116 std::string stdfileName = crea::wx2std(fileName);
118 //if CMakeLists, create CMakeLists
119 if(stdfileName == "CMakeLists.txt")
121 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
122 this->children.push_back(this->CMakeLists);
124 //if is an unknown file, create file
127 this->children.push_back(new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
130 cont = dir.GetNext(&fileName);
133 this->SortChildren();
136 modelCDMLibrary::~modelCDMLibrary()
140 const std::string& modelCDMLibrary::GetNameLibrary() const
142 return this->nameLibrary;
145 bool modelCDMLibrary::SetNameLibrary(const std::string& fileName, std::string*& result)
147 std::vector<std::string> words;
148 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
149 std::string fileNameReal = words[0];
150 for (int i = 1; i < words.size(); i++)
152 fileNameReal += "-" + words[i];
156 //opening original cmakelists
157 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
160 result = new std::string("CMakeLists.txt file failed to open.");
163 //opening temporal cmakelists
164 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
167 result = new std::string("CMakeLists.txt.tmp file failed to open.");
170 //copying contents from original to temporal and making changes
171 while (getline(in, line))
173 if(line.find("SET ( LIBRARY_NAME") != std::string::npos)
174 line = "SET ( LIBRARY_NAME " + fileNameReal + " )";
175 out << line << std::endl;
179 //delete old file and rename new file
180 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
181 if(system(renameCommand.c_str()))
183 result = new std::string("An error occurred while running '" + renameCommand + "'.");
187 this->nameLibrary = fileNameReal;
191 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
193 //TODO:: mkdir depending on OS
194 std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
195 if(system(command.c_str()))
197 result = new std::string("Error executing: " + command + ".");
200 modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
201 this->folders.push_back(folder);
202 this->children.push_back(folder);
207 const bool modelCDMLibrary::Refresh(std::string*& result)
210 this->type = wxDIR_DIRS;
213 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
215 std::ifstream confFile;
216 confFile.open((pathMakeLists).c_str());
219 while(confFile.is_open() && !confFile.eof())
222 std::getline(confFile,word,'(');
223 std::vector<std::string> wordBits;
224 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
226 if(wordBits[wordBits.size()-1] == "SET")
229 std::getline(confFile,word,')');
230 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
231 if(wordBits[0] == "LIBRARY_NAME")
234 for (int i = 2; i < wordBits.size(); i++)
236 word += " " + wordBits[i];
239 this->nameLibrary = word;
247 std::vector<bool> checked(this->children.size(), false);
248 std::vector<bool> checkedFolders(this->folders.size(), false);
251 wxDir dir(crea::std2wx((this->path).c_str()));
255 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
258 std::string stdfileName = crea::wx2std(fileName);
259 std::string folderName = stdfileName;
260 //check if they already exist
262 for (int i = 0;!found && i < this->folders.size(); i++)
264 if (this->folders[i]->GetName() == folderName)
267 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
269 checkedFolders[i] = true;
270 if(!this->folders[i]->Refresh(result))
276 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
277 this->folders.push_back(folder);
278 this->children.push_back(folder);
280 cont = dir.GetNext(&fileName);
283 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
286 std::string stdfileName = crea::wx2std(fileName);
288 //if CMakeLists, create CMakeLists
289 if(stdfileName == "CMakeLists.txt")
291 if (this->CMakeLists == NULL)
293 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
294 this->children.push_back(this->CMakeLists);
298 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
300 if(!this->CMakeLists->Refresh(result))
304 //if is an unknown file, create file
308 for (int i = 0; i <!found && this->children.size(); i++)
310 if (this->children[i]->GetName() == stdfileName)
314 if(!this->children[i]->Refresh(result))
321 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
322 this->children.push_back(file);
326 cont = dir.GetNext(&fileName);
330 for (int i = 0; i < checkedFolders.size(); i++)
332 if(!checkedFolders[i])
334 this->folders.erase(this->folders.begin()+i);
335 checkedFolders.erase(checkedFolders.begin()+i);
339 for (int i = 0; i < checked.size(); i++)
343 delete this->children[i];
344 this->children.erase(this->children.begin()+i);
345 checked.erase(checked.begin()+i);
349 this->SortChildren();