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