]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMBlackBox.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / modelCDMBlackBox.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  * modelCDMBlackBox.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMBlackBox.h"
36
37 #include<fstream>
38
39 #include "CDMUtilities.h"
40
41 #include<creaWx.h>
42 #include"wx/dir.h"
43
44 modelCDMBlackBox::modelCDMBlackBox()
45 {
46   this->source = NULL;
47   this->header = NULL;
48 }
49
50 modelCDMBlackBox::modelCDMBlackBox(const std::string& path, const std::string& name, const int& level)
51 {
52   this->name = name;
53   this->path = path;
54   this->level = level;
55   this->type = wxDIR_DIRS;
56   this->source = NULL;
57   this->header = NULL;
58
59   std::string pathHeader = path + CDMUtilities::SLASH + "bb" + this->name + ".h";
60
61   std::ifstream confFile;
62   confFile.open((pathHeader).c_str());
63   std::string word;
64   while(confFile.is_open() && !confFile.eof())
65     {
66       //get BBTK's
67       std::getline(confFile,word,'(');
68       std::vector<std::string> wordBits;
69       CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
70
71       if(wordBits[wordBits.size()-1] == "BBTK_NAME")
72         {
73           std::getline(confFile,word,'"');
74           std::getline(confFile,word,'"');
75           this->nameBlackBox = word;
76         }
77       else if(wordBits[wordBits.size()-1] == "BBTK_AUTHOR")
78         {
79           std::getline(confFile,word,'"');
80           std::getline(confFile,word,'"');
81           this->authors = word;
82         }
83       else if(wordBits[wordBits.size()-1] == "BBTK_DESCRIPTION")
84         {
85           std::getline(confFile,word,'"');
86           std::getline(confFile,word,'"');
87           this->description = word;
88         }
89       else if(wordBits[wordBits.size()-1] == "BBTK_CATEGORY")
90         {
91           std::getline(confFile,word,'"');
92           std::getline(confFile,word,'"');
93           this->categories = word;
94           if (this->categories == "")
95             this->categories = "empty";
96         }
97     }
98   confFile.close();
99
100 }
101
102 modelCDMBlackBox::~modelCDMBlackBox()
103 {
104   this->header = NULL;
105   this->source = NULL;
106 }
107
108 const std::string& modelCDMBlackBox::GetNameBlackBox() const
109 {
110   return this->nameBlackBox;
111 }
112
113 const std::string& modelCDMBlackBox::GetAuthors() const
114 {
115   return this->authors;
116 }
117
118 const std::string& modelCDMBlackBox::GetCategories() const
119 {
120   return this->categories;
121 }
122
123 const std::string& modelCDMBlackBox::GetDescription() const
124 {
125   return this->description;
126 }
127
128 bool modelCDMBlackBox::SetAuthors(const std::string& authors, std::string*& result)
129 {
130   std::vector<std::string> words;
131   CDMUtilities::splitter::split(words, authors, "/\\\"\n", CDMUtilities::splitter::no_empties);
132   std::string authorsReal = words[0];
133   for (int i = 1; i < words.size(); i++)
134     {
135       authorsReal += "," + words[i];
136     }
137
138   //opening original header
139   std::string pathHeader = this->header->GetPath();
140   std::ifstream in(pathHeader.c_str());
141   if( !in.is_open())
142     {
143       result = new std::string(pathHeader + " file failed to open.");
144       return false;
145     }
146   //opening temporal header
147   std::ofstream out((pathHeader + ".tmp").c_str());
148   if( !out.is_open())
149     {
150       result = new std::string(pathHeader + ".tmp file failed to open.");
151       return false;
152     }
153   //copying contents from original to temporal and making changes
154   std::string reading;
155   while (getline(in, reading, '('))
156     {
157       CDMUtilities::splitter::split(words, reading, "\n ", CDMUtilities::splitter::no_empties);
158       if(words[words.size() - 1] == "BBTK_AUTHOR")
159         {
160           out << reading << "(\"" << authorsReal << "\")";
161           getline(in, reading, ')');
162         }
163       else
164         {
165           out << reading;
166           if (!in.eof())
167             out << "(";
168         }
169     }
170   in.close();
171   out.close();
172   //delete old file and rename new file
173   std::string renameCommand = "mv \"" + pathHeader + ".tmp\" \"" + pathHeader + "\"";
174   if(system(renameCommand.c_str()))
175     {
176       result = new std::string("An error occurred while running '" + renameCommand + "'.");
177       return false;
178     }
179
180   this->authors = authorsReal;
181   return true;
182
183 }
184
185 bool modelCDMBlackBox::SetCategories(
186     const std::string& categories,
187     std::string*& result
188 )
189 {
190   std::vector<std::string> words;
191   CDMUtilities::splitter::split(words, categories, "\"\\/", CDMUtilities::splitter::no_empties);
192   std::string catsReal = words[0];
193   for (int i = 1; i < words.size(); i++)
194     {
195       catsReal += "," + words[i];
196     }
197
198   //opening original header
199   std::string pathHeader = this->header->GetPath();
200   std::ifstream in(pathHeader.c_str());
201   if( !in.is_open())
202     {
203       result = new std::string(pathHeader + " file failed to open.");
204       return false;
205     }
206   //opening temporal header
207   std::ofstream out((pathHeader + ".tmp").c_str());
208   if( !out.is_open())
209     {
210       result = new std::string(pathHeader + ".tmp file failed to open.");
211       return false;
212     }
213   //copying contents from original to temporal and making changes
214   std::string reading;
215   while (getline(in, reading, '('))
216     {
217       CDMUtilities::splitter::split(words, reading, "\n ", CDMUtilities::splitter::no_empties);
218       if(words[words.size() - 1] == "BBTK_CATEGORY")
219         {
220           out << reading << "(\"" << catsReal << "\")";
221           getline(in, reading, ')');
222         }
223       else
224         {
225           out << reading;
226           if (!in.eof())
227             out << "(";
228         }
229     }
230   in.close();
231   out.close();
232   //delete old file and rename new file
233   std::string renameCommand = "mv \"" + pathHeader + ".tmp\" \"" + pathHeader + "\"";
234   if(system(renameCommand.c_str()))
235     {
236       result = new std::string("An error occurred while running '" + renameCommand + "'.");
237       return false;
238     }
239
240   this->categories = catsReal;
241   return true;
242 }
243
244 bool modelCDMBlackBox::SetDescription(
245     const std::string& description,
246     std::string*& result
247 )
248 {
249   std::vector<std::string> words;
250   CDMUtilities::splitter::split(words, description, "\"\n\\/", CDMUtilities::splitter::no_empties);
251   std::string descReal = words[0];
252   for (int i = 1; i < words.size(); i++)
253     {
254       descReal += "-" + words[i];
255     }
256
257   //opening original header
258   std::string pathHeader = this->header->GetPath();
259   std::ifstream in(pathHeader.c_str());
260   if( !in.is_open())
261     {
262       result = new std::string(pathHeader + " file failed to open.");
263       return false;
264     }
265   //opening temporal header
266   std::ofstream out((pathHeader + ".tmp").c_str());
267   if( !out.is_open())
268     {
269       result = new std::string(pathHeader + ".tmp file failed to open.");
270       return false;
271     }
272   //copying contents from original to temporal and making changes
273   std::string reading;
274   while (getline(in, reading, '('))
275     {
276       CDMUtilities::splitter::split(words, reading, "\n ", CDMUtilities::splitter::no_empties);
277       if(words[words.size() - 1] == "BBTK_DESCRIPTION")
278         {
279           out << reading << "(\"" << descReal << "\")";
280           getline(in, reading, ')');
281         }
282       else
283         {
284           out << reading;
285           if (!in.eof())
286             out << "(";
287         }
288     }
289   in.close();
290   out.close();
291   //delete old file and rename new file
292   std::string renameCommand = "mv \"" + pathHeader + ".tmp\" \"" + pathHeader + "\"";
293   if(system(renameCommand.c_str()))
294     {
295       result = new std::string("An error occurred while running '" + renameCommand + "'.");
296       return false;
297     }
298
299   this->description = descReal;
300   return true;
301 }
302
303 void modelCDMBlackBox::SetHeaderFile(modelCDMFile* file)
304 {
305   this->header = file;
306 }
307
308 void modelCDMBlackBox::SetSourceFile(modelCDMFile* file)
309 {
310   this->source = file;
311 }
312
313 bool modelCDMBlackBox::OpenCxx(std::string*& result)
314 {
315   return !CDMUtilities::openTextEditor(this->source->GetPath());
316 }
317
318 bool modelCDMBlackBox::OpenHxx(std::string*& result)
319 {
320   return !CDMUtilities::openTextEditor(this->header->GetPath());
321 }
322
323 modelCDMFile* modelCDMBlackBox::GetHeaderFile() const
324 {
325   return this->header;
326 }
327
328 modelCDMFile* modelCDMBlackBox::GetSourceFile() const
329 {
330   return this->source;
331 }
332
333 const bool modelCDMBlackBox::Refresh(std::string*& result)
334 {
335   std::string pathHeader = path + CDMUtilities::SLASH + "bb" + this->name + ".h";
336
337   std::ifstream confFile;
338   confFile.open((pathHeader).c_str());
339   std::string word;
340
341   if(!confFile.is_open())
342     return false;
343
344   while(confFile.is_open() && !confFile.eof())
345     {
346       //get BBTK's
347       std::getline(confFile,word,'(');
348       std::vector<std::string> wordBits;
349       CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
350
351       if(wordBits[wordBits.size()-1] == "BBTK_NAME")
352         {
353           std::getline(confFile,word,'"');
354           std::getline(confFile,word,'"');
355           this->nameBlackBox = word;
356         }
357       else if(wordBits[wordBits.size()-1] == "BBTK_AUTHOR")
358         {
359           std::getline(confFile,word,'"');
360           std::getline(confFile,word,'"');
361           this->authors = word;
362         }
363       else if(wordBits[wordBits.size()-1] == "BBTK_DESCRIPTION")
364         {
365           std::getline(confFile,word,'"');
366           std::getline(confFile,word,'"');
367           this->description = word;
368         }
369       else if(wordBits[wordBits.size()-1] == "BBTK_CATEGORY")
370         {
371           std::getline(confFile,word,'"');
372           std::getline(confFile,word,'"');
373           this->categories = word;
374           if (this->categories == "")
375             this->categories = "empty";
376         }
377     }
378   confFile.close();
379   return true;
380 }