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