]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMLibrary.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMLibrary.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sant�)
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
9 #
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.
16 #
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
21 #  liability.
22 #
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 # ------------------------------------------------------------------------
26  */
27
28 /*
29  * modelCDMLibrary.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMLibrary.h"
36
37 #include <fstream>
38 #include <algorithm>
39
40 #include "CDMUtilities.h"
41 #include "creaWx.h"
42 #include "wx/dir.h"
43
44 modelCDMLibrary::modelCDMLibrary()
45 {
46 }
47
48 modelCDMLibrary::modelCDMLibrary(const std::string& path, const std::string& name, const int& level)
49 {
50   //folder name
51   this->name = name;
52   //path
53   this->path = CDMUtilities::fixPath(path);
54   //type
55   this->type = wxDIR_DIRS;
56   //level
57   this->level = level;
58
59   //open CMakeList
60   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
61
62   std::ifstream confFile;
63   confFile.open((pathMakeLists).c_str());
64
65   std::string word;
66   while(confFile.is_open() && !confFile.eof())
67     {
68       //get sets
69       std::getline(confFile,word,'(');
70       std::vector<std::string> wordBits;
71       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
72
73       if(wordBits[wordBits.size()-1] == "SET")
74         {
75           //get library name
76           std::getline(confFile,word,')');
77           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
78           if(wordBits[0] == "LIBRARY_NAME")
79             {
80               word = wordBits[1];
81               for (int i = 2; i < wordBits.size(); i++)
82                 {
83                   word += " " + wordBits[i];
84                 }
85
86               this->nameLibrary = word;
87             }
88         }
89     }
90
91   confFile.close();
92
93   //add library contents
94
95   this->children.clear();
96   wxDir dir(crea::std2wx((this->path).c_str()));
97   if (dir.IsOpened())
98     {
99       wxString fileName;
100       //folders
101       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
102       while (cont)
103         {
104           std::string stdfileName = crea::wx2std(fileName);
105
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);
109
110           cont = dir.GetNext(&fileName);
111         }
112       //files
113       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
114       while (cont)
115         {
116           std::string stdfileName = crea::wx2std(fileName);
117
118           //if CMakeLists, create CMakeLists
119           if(stdfileName == "CMakeLists.txt")
120             {
121               this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
122               this->children.push_back(this->CMakeLists);
123             }
124           //if is an unknown file, create file
125           else
126             {
127               this->children.push_back(new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
128             }
129
130           cont = dir.GetNext(&fileName);
131         }
132     }
133   this->SortChildren();
134 }
135
136 modelCDMLibrary::~modelCDMLibrary()
137 {
138 }
139
140 const std::string& modelCDMLibrary::GetNameLibrary() const
141 {
142   return this->nameLibrary;
143 }
144
145 bool modelCDMLibrary::SetNameLibrary(const std::string& fileName, std::string*& result)
146 {
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++)
151     {
152       fileNameReal += "-" + words[i];
153     }
154
155   std::string line;
156   //opening original cmakelists
157   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
158   if( !in.is_open())
159     {
160       result = new std::string("CMakeLists.txt file failed to open.");
161       return false;
162     }
163   //opening temporal cmakelists
164   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
165   if( !out.is_open())
166     {
167       result = new std::string("CMakeLists.txt.tmp file failed to open.");
168       return false;
169     }
170   //copying contents from original to temporal and making changes
171   while (getline(in, line))
172     {
173       if(line.find("SET ( LIBRARY_NAME") != std::string::npos)
174         line = "SET ( LIBRARY_NAME  " + fileNameReal + "  )";
175       out << line << std::endl;
176     }
177   in.close();
178   out.close();
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()))
182     {
183       result = new std::string("An error occurred while running '" + renameCommand + "'.");
184       return false;
185     }
186
187   this->nameLibrary = fileNameReal;
188   return true;
189 }
190
191 modelCDMFolder* modelCDMLibrary::CreateFolder(const std::string& name, std::string*& result)
192 {
193   //TODO:: mkdir depending on OS
194     std::string command = "mkdir \"" + path + CDMUtilities::SLASH + name + "\"";
195     if(system(command.c_str()))
196       {
197         result = new std::string("Error executing: " + command + ".");
198         return NULL;
199       }
200     modelCDMFolder* folder = new modelCDMFolder(path + CDMUtilities::SLASH + name, name, level + 1);
201     this->folders.push_back(folder);
202     this->children.push_back(folder);
203
204     return folder;
205 }
206
207 const bool modelCDMLibrary::Refresh(std::string*& result)
208 {
209   //set attributes
210   this->type = wxDIR_DIRS;
211
212   //open CMakeList
213   std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
214
215   std::ifstream confFile;
216   confFile.open((pathMakeLists).c_str());
217
218   std::string word;
219   while(confFile.is_open() && !confFile.eof())
220     {
221       //get sets
222       std::getline(confFile,word,'(');
223       std::vector<std::string> wordBits;
224       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
225
226       if(wordBits[wordBits.size()-1] == "SET")
227         {
228           //get library name
229           std::getline(confFile,word,')');
230           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
231           if(wordBits[0] == "LIBRARY_NAME")
232             {
233               word = wordBits[1];
234               for (int i = 2; i < wordBits.size(); i++)
235                 {
236                   word += " " + wordBits[i];
237                 }
238
239               this->nameLibrary = word;
240             }
241         }
242     }
243
244   confFile.close();
245
246   //check children
247   std::vector<bool> checked(this->children.size(), false);
248   std::vector<bool> checkedFolders(this->folders.size(), false);
249
250   //check all folders
251   wxDir dir(crea::std2wx((this->path).c_str()));
252   if (dir.IsOpened())
253     {
254       wxString fileName;
255       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
256       while (cont)
257         {
258           std::string stdfileName = crea::wx2std(fileName);
259           std::string folderName = stdfileName;
260           //check if they already exist
261           bool found = false;
262           for (int i = 0;!found && i < this->folders.size(); i++)
263             {
264               if (this->folders[i]->GetName() == folderName)
265                 {
266                   found = true;
267                   int pos = std::find(this->children.begin(), this->children.end(), this->folders[i]) - this->children.begin();
268                   checked[pos] = true;
269                   checkedFolders[i] = true;
270                   if(!this->folders[i]->Refresh(result))
271                     return false;
272                 }
273             }
274           if(!found)
275             {
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);
279             }
280           cont = dir.GetNext(&fileName);
281         }
282
283       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
284       while (cont)
285         {
286           std::string stdfileName = crea::wx2std(fileName);
287
288           //if CMakeLists, create CMakeLists
289           if(stdfileName == "CMakeLists.txt")
290             {
291               if (this->CMakeLists == NULL)
292                 {
293                   this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
294                   this->children.push_back(this->CMakeLists);
295                 }
296               else
297                 {
298                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
299                   checked[pos] = true;
300                   if(!this->CMakeLists->Refresh(result))
301                     return false;
302                 }
303             }
304           //if is an unknown file, create file
305           else
306             {
307               bool found = false;
308               for (int i = 0; i <!found && this->children.size(); i++)
309                 {
310                   if (this->children[i]->GetName() == stdfileName)
311                     {
312                       found = true;
313                       checked[i] = true;
314                       if(!this->children[i]->Refresh(result))
315                         return false;
316                     }
317                 }
318
319               if(!found)
320                 {
321                   modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
322                   this->children.push_back(file);
323                 }
324             }
325
326           cont = dir.GetNext(&fileName);
327         }
328     }
329
330   for (int i = 0; i < checkedFolders.size(); i++)
331     {
332       if(!checkedFolders[i])
333         {
334           this->folders.erase(this->folders.begin()+i);
335           checkedFolders.erase(checkedFolders.begin()+i);
336           i--;
337         }
338     }
339   for (int i = 0; i < checked.size(); i++)
340     {
341       if(!checked[i])
342         {
343           delete this->children[i];
344           this->children.erase(this->children.begin()+i);
345           checked.erase(checked.begin()+i);
346           i--;
347         }
348     }
349   this->SortChildren();
350   return true;
351 }