]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMBlackBox.cpp
c92c6777725e724c3729b679b9cfebeb8eb4c5d2
[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 < 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   std::string renameCommand = "mv \"" + pathHeader + ".tmp\" \"" + pathHeader + "\"";
176   if(system(renameCommand.c_str()))
177     {
178       result = new std::string("An error occurred while running '" + renameCommand + "'.");
179       return false;
180     }
181
182   this->authors = authorsReal;
183   return true;
184
185 }
186
187 bool modelCDMBlackBox::SetCategories(
188     const std::string& categories,
189     std::string*& result
190 )
191 {
192   std::vector<std::string> words;
193   CDMUtilities::splitter::split(words, categories, "\"\\/", CDMUtilities::splitter::no_empties);
194   std::string catsReal = words[0];
195   for (int i = 1; i < words.size(); i++)
196     {
197       catsReal += "," + words[i];
198     }
199
200   //opening original header
201   std::string pathHeader = this->header->GetPath();
202   std::ifstream in(pathHeader.c_str());
203   if( !in.is_open())
204     {
205       result = new std::string(pathHeader + " file failed to open.");
206       return false;
207     }
208   //opening temporal header
209   std::ofstream out((pathHeader + ".tmp").c_str());
210   if( !out.is_open())
211     {
212       result = new std::string(pathHeader + ".tmp file failed to open.");
213       return false;
214     }
215   //copying contents from original to temporal and making changes
216   std::string reading;
217   while (getline(in, reading, '('))
218     {
219       CDMUtilities::splitter::split(words, reading, "\n ", CDMUtilities::splitter::no_empties);
220       if(words[words.size() - 1] == "BBTK_CATEGORY")
221         {
222           out << reading << "(\"" << catsReal << "\")";
223           getline(in, reading, ')');
224         }
225       else
226         {
227           out << reading;
228           if (!in.eof())
229             out << "(";
230         }
231     }
232   in.close();
233   out.close();
234   //delete old file and rename new file
235   std::string renameCommand = "mv \"" + pathHeader + ".tmp\" \"" + pathHeader + "\"";
236   if(system(renameCommand.c_str()))
237     {
238       result = new std::string("An error occurred while running '" + renameCommand + "'.");
239       return false;
240     }
241
242   this->categories = catsReal;
243   return true;
244 }
245
246 bool modelCDMBlackBox::SetDescription(
247     const std::string& description,
248     std::string*& result
249 )
250 {
251   std::vector<std::string> words;
252   CDMUtilities::splitter::split(words, description, "\"\n\\/", CDMUtilities::splitter::no_empties);
253   std::string descReal = words[0];
254   for (int i = 1; i < words.size(); i++)
255     {
256       descReal += "-" + words[i];
257     }
258
259   //opening original header
260   std::string pathHeader = this->header->GetPath();
261   std::ifstream in(pathHeader.c_str());
262   if( !in.is_open())
263     {
264       result = new std::string(pathHeader + " file failed to open.");
265       return false;
266     }
267   //opening temporal header
268   std::ofstream out((pathHeader + ".tmp").c_str());
269   if( !out.is_open())
270     {
271       result = new std::string(pathHeader + ".tmp file failed to open.");
272       return false;
273     }
274   //copying contents from original to temporal and making changes
275   std::string reading;
276   while (getline(in, reading, '('))
277     {
278       CDMUtilities::splitter::split(words, reading, "\n ", CDMUtilities::splitter::no_empties);
279       if(words[words.size() - 1] == "BBTK_DESCRIPTION")
280         {
281           out << reading << "(\"" << descReal << "\")";
282           getline(in, reading, ')');
283         }
284       else
285         {
286           out << reading;
287           if (!in.eof())
288             out << "(";
289         }
290     }
291   in.close();
292   out.close();
293   //delete old file and rename new file
294   std::string renameCommand = "mv \"" + pathHeader + ".tmp\" \"" + pathHeader + "\"";
295   if(system(renameCommand.c_str()))
296     {
297       result = new std::string("An error occurred while running '" + renameCommand + "'.");
298       return false;
299     }
300
301   this->description = descReal;
302   return true;
303 }
304
305 void modelCDMBlackBox::SetHeaderFile(modelCDMFile* file)
306 {
307   this->header = file;
308 }
309
310 void modelCDMBlackBox::SetSourceFile(modelCDMFile* file)
311 {
312   this->source = file;
313 }
314
315 bool modelCDMBlackBox::OpenCxx(std::string*& result)
316 {
317   return !CDMUtilities::openTextEditor(this->source->GetPath());
318 }
319
320 bool modelCDMBlackBox::OpenHxx(std::string*& result)
321 {
322   return !CDMUtilities::openTextEditor(this->header->GetPath());
323 }
324
325 modelCDMFile* modelCDMBlackBox::GetHeaderFile() const
326 {
327   return this->header;
328 }
329
330 modelCDMFile* modelCDMBlackBox::GetSourceFile() const
331 {
332   return this->source;
333 }
334
335 const bool modelCDMBlackBox::Refresh(std::string*& result)
336 {
337   std::cout << "refreshing black box: " << this->nameBlackBox << std::endl;
338   std::string pathHeader = path + CDMUtilities::SLASH + "bb" + this->name + ".h";
339
340   std::ifstream confFile;
341   confFile.open((pathHeader).c_str());
342   std::string word;
343
344   if(!confFile.is_open())
345     return false;
346
347   while(confFile.is_open() && !confFile.eof())
348     {
349       //get BBTK's
350       std::getline(confFile,word,'(');
351       std::vector<std::string> wordBits;
352       CDMUtilities::splitter::split(wordBits,word," \n",CDMUtilities::splitter::no_empties);
353
354       if(wordBits[wordBits.size()-1] == "BBTK_NAME")
355         {
356           std::getline(confFile,word,'"');
357           std::getline(confFile,word,'"');
358           this->nameBlackBox = word;
359         }
360       else if(wordBits[wordBits.size()-1] == "BBTK_AUTHOR")
361         {
362           std::getline(confFile,word,'"');
363           std::getline(confFile,word,'"');
364           this->authors = word;
365         }
366       else if(wordBits[wordBits.size()-1] == "BBTK_DESCRIPTION")
367         {
368           std::getline(confFile,word,'"');
369           std::getline(confFile,word,'"');
370           this->description = word;
371         }
372       else if(wordBits[wordBits.size()-1] == "BBTK_CATEGORY")
373         {
374           std::getline(confFile,word,'"');
375           std::getline(confFile,word,'"');
376           this->categories = word;
377           if (this->categories == "")
378             this->categories = "empty";
379         }
380     }
381   confFile.close();
382   return true;
383 }