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