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