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 int& level)
51 std::vector<std::string> words;
52 std::string delimiters;
53 //TODO::fix for windows
55 CDMUtilities::splitter::split(words, path, delimiters, CDMUtilities::splitter::no_empties);
56 this->name = words[words.size()-1];
59 this->path = CDMUtilities::fixPath(path);
61 this->type = wxDIR_DIRS;
66 std::string pathMakeLists = path + "/CMakeLists.txt";
68 std::ifstream confFile;
69 confFile.open((pathMakeLists).c_str());
72 while(confFile.is_open() && !confFile.eof())
75 std::getline(confFile,word,'(');
76 std::vector<std::string> wordBits;
77 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
79 if(wordBits[wordBits.size()-1] == "SET")
82 std::getline(confFile,word,')');
83 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
84 if(wordBits[0] == "LIBRARY_NAME")
87 for (int i = 2; i < wordBits.size(); i++)
89 word += " " + wordBits[i];
92 this->nameLibrary = word;
99 //add library contents
101 this->children.clear();
102 wxDir dir(crea::std2wx((this->path).c_str()));
107 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
110 std::string stdfileName = crea::wx2std(fileName);
112 modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
113 this->folders.push_back(folder);
114 this->children.push_back(folder);
116 cont = dir.GetNext(&fileName);
119 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
122 std::string stdfileName = crea::wx2std(fileName);
124 //if CMakeLists, create CMakeLists
125 if(stdfileName == "CMakeLists.txt")
127 this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
128 this->children.push_back(this->CMakeLists);
130 //if is an unknown file, create file
133 this->children.push_back(new modelCDMFile(this->path + "/" + stdfileName, this->level + 1));
136 cont = dir.GetNext(&fileName);
139 this->SortChildren();
142 modelCDMLibrary::~modelCDMLibrary()
146 const std::string& modelCDMLibrary::GetNameLibrary() const
148 return this->nameLibrary;
151 bool modelCDMLibrary::SetNameLibrary(const std::string& fileName, std::string*& result)
153 std::vector<std::string> words;
154 CDMUtilities::splitter::split(words, fileName, ", /\\\"", CDMUtilities::splitter::no_empties);
155 std::string fileNameReal = words[0];
156 for (int i = 1; i < words.size(); i++)
158 fileNameReal += "-" + words[i];
162 //opening original cmakelists
163 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
166 result = new std::string("CMakeLists.txt file failed to open.");
169 //opening temporal cmakelists
170 std::ofstream out((this->path + "/CMakeLists.txt.tmp").c_str());
173 result = new std::string("CMakeLists.txt.tmp file failed to open.");
176 //copying contents from original to temporal and making changes
177 while (getline(in, line))
179 if(line.find("SET ( LIBRARY_NAME") != std::string::npos)
180 line = "SET ( LIBRARY_NAME " + fileNameReal + " )";
181 out << line << std::endl;
185 //delete old file and rename new file
186 std::string renameCommand = "mv " + this->path + "/CMakeLists.txt.tmp " + this->path + "/CMakeLists.txt";
187 if(system(renameCommand.c_str()))
189 result = new std::string("An error occurred while running '" + renameCommand + "'.");
193 this->nameLibrary = fileNameReal;
197 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
199 //TODO:: mkdir depending on OS
200 std::string command = "mkdir " + path + "/" + name;
201 if(system(command.c_str()))
203 result = new std::string("Error executing: " + command + ".");
206 modelCDMFolder* folder = new modelCDMFolder(path + "/" + name, level + 1);
207 this->folders.push_back(folder);
208 this->children.push_back(folder);
213 const bool modelCDMLibrary::Refresh(std::string*& result)
216 this->type = wxDIR_DIRS;
219 std::string pathMakeLists = path + "/CMakeLists.txt";
221 std::ifstream confFile;
222 confFile.open((pathMakeLists).c_str());
225 while(confFile.is_open() && !confFile.eof())
228 std::getline(confFile,word,'(');
229 std::vector<std::string> wordBits;
230 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
232 if(wordBits[wordBits.size()-1] == "SET")
235 std::getline(confFile,word,')');
236 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
237 if(wordBits[0] == "LIBRARY_NAME")
240 for (int i = 2; i < wordBits.size(); i++)
242 word += " " + wordBits[i];
245 this->nameLibrary = word;
253 std::vector<bool> checked(this->children.size(), false);
254 std::vector<bool> checkedFolders(this->folders.size(), false);
257 wxDir dir(crea::std2wx((this->path).c_str()));
261 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
264 std::string stdfileName = crea::wx2std(fileName);
265 std::string folderName = stdfileName;
266 //check if they already exist
268 for (int i = 0;!found && i < this->folders.size(); i++)
270 if (this->folders[i]->GetName() == folderName)
273 int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
275 checkedFolders[i] = true;
276 if(!this->folders[i]->Refresh(result))
282 modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
283 this->folders.push_back(folder);
284 this->children.push_back(folder);
286 cont = dir.GetNext(&fileName);
289 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
292 std::string stdfileName = crea::wx2std(fileName);
294 //if CMakeLists, create CMakeLists
295 if(stdfileName == "CMakeLists.txt")
297 if (this->CMakeLists == NULL)
299 this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
300 this->children.push_back(this->CMakeLists);
304 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
306 if(!this->CMakeLists->Refresh(result))
310 //if is an unknown file, create file
314 for (int i = 0; i <!found && this->children.size(); i++)
316 if (this->children[i]->GetName() == stdfileName)
320 if(!this->children[i]->Refresh(result))
327 modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
328 this->children.push_back(file);
332 cont = dir.GetNext(&fileName);
336 for (int i = 0; i < checkedFolders.size(); i++)
338 if(!checkedFolders[i])
340 this->folders.erase(this->folders.begin()+i);
341 checkedFolders.erase(checkedFolders.begin()+i);
345 for (int i = 0; i < checked.size(); i++)
349 delete this->children[i];
350 this->children.erase(this->children.begin()+i);
351 checked.erase(checked.begin()+i);
355 this->SortChildren();