]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMBlackBox.cpp
Black Box view and folder structure implemented
[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& hName, const std::string& path, const int& level)
51 {
52   this->name = hName.substr(2, hName.size()-4);
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 + "/" + "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 }
105
106 const std::string& modelCDMBlackBox::GetNameBlackBox() const
107 {
108   return this->nameBlackBox;
109 }
110
111 const std::string& modelCDMBlackBox::GetAuthors() const
112 {
113   return this->authors;
114 }
115
116 const std::string& modelCDMBlackBox::GetCategories() const
117 {
118   return this->categories;
119 }
120
121 const std::string& modelCDMBlackBox::GetDescription() const
122 {
123   return this->description;
124 }
125
126 bool modelCDMBlackBox::SetAuthors(const std::string& authors, std::string*& result)
127 {
128   std::vector<std::string> words;
129   CDMUtilities::splitter::split(words, authors, ",\"\n", CDMUtilities::splitter::no_empties);
130   std::string authorsReal = words[0];
131   for (int i = 1; i < words.size(); i++)
132     {
133       authorsReal += "/" + words[i];
134     }
135
136   //opening original header
137   std::string pathHeader = this->header->GetPath();
138   std::ifstream in(pathHeader.c_str());
139   if( !in.is_open())
140     {
141       result = new std::string(pathHeader + " file failed to open.");
142       return false;
143     }
144   //opening temporal header
145   std::ofstream out((pathHeader + ".tmp").c_str());
146   if( !out.is_open())
147     {
148       result = new std::string(pathHeader + ".tmp file failed to open.");
149       return false;
150     }
151   //copying contents from original to temporal and making changes
152   std::string reading;
153   while (getline(in, reading, '('))
154     {
155       CDMUtilities::splitter::split(words, reading, "\n ", CDMUtilities::splitter::no_empties);
156       if(words[words.size() - 1] == "BBTK_AUTHOR")
157         {
158           out << reading << "(\"" << authorsReal << "\")";
159           getline(in, reading, ')');
160         }
161       else
162         {
163           out << reading;
164           if (!in.eof())
165             out << "(";
166         }
167     }
168   in.close();
169   out.close();
170   //delete old file and rename new file
171   std::string renameCommand = "mv " + pathHeader + ".tmp " + pathHeader;
172   if(system(renameCommand.c_str()))
173     {
174       result = new std::string("An error occurred while running '" + renameCommand + "'.");
175       return false;
176     }
177
178   this->authors = authorsReal;
179   return true;
180
181 }
182
183 bool modelCDMBlackBox::SetCategories(
184     const std::string& categories,
185     std::string*& result
186 )
187 {
188   std::vector<std::string> words;
189     CDMUtilities::splitter::split(words, categories, "\"", CDMUtilities::splitter::no_empties);
190     std::string catsReal = words[0];
191     for (int i = 1; i < words.size(); i++)
192       {
193         catsReal += "-" + words[i];
194       }
195
196     //opening original header
197     std::string pathHeader = this->header->GetPath();
198     std::ifstream in(pathHeader.c_str());
199     if( !in.is_open())
200       {
201         result = new std::string(pathHeader + " file failed to open.");
202         return false;
203       }
204     //opening temporal header
205     std::ofstream out((pathHeader + ".tmp").c_str());
206     if( !out.is_open())
207       {
208         result = new std::string(pathHeader + ".tmp file failed to open.");
209         return false;
210       }
211     //copying contents from original to temporal and making changes
212     std::string reading;
213     while (getline(in, reading, '('))
214       {
215         CDMUtilities::splitter::split(words, reading, "\n ", CDMUtilities::splitter::no_empties);
216         if(words[words.size() - 1] == "BBTK_CATEGORY")
217           {
218             out << reading << "(\"" << catsReal << "\")";
219             getline(in, reading, ')');
220           }
221         else
222           {
223             out << reading;
224             if (!in.eof())
225               out << "(";
226           }
227       }
228     in.close();
229     out.close();
230     //delete old file and rename new file
231     std::string renameCommand = "mv " + pathHeader + ".tmp " + pathHeader;
232     if(system(renameCommand.c_str()))
233       {
234         result = new std::string("An error occurred while running '" + renameCommand + "'.");
235         return false;
236       }
237
238     this->categories = catsReal;
239     return true;
240 }
241
242 bool modelCDMBlackBox::SetDescription(
243     const std::string& description,
244     std::string*& result
245 )
246 {
247   std::vector<std::string> words;
248   CDMUtilities::splitter::split(words, description, "\"", CDMUtilities::splitter::no_empties);
249   std::string descReal = words[0];
250   for (int i = 1; i < words.size(); i++)
251     {
252       descReal += "\\\"" + words[i];
253     }
254
255   //opening original header
256   std::string pathHeader = this->header->GetPath();
257   std::ifstream in(pathHeader.c_str());
258   if( !in.is_open())
259     {
260       result = new std::string(pathHeader + " file failed to open.");
261       return false;
262     }
263   //opening temporal header
264   std::ofstream out((pathHeader + ".tmp").c_str());
265   if( !out.is_open())
266     {
267       result = new std::string(pathHeader + ".tmp file failed to open.");
268       return false;
269     }
270   //copying contents from original to temporal and making changes
271   std::string reading;
272   while (getline(in, reading, '('))
273     {
274       CDMUtilities::splitter::split(words, reading, "\n ", CDMUtilities::splitter::no_empties);
275       if(words[words.size() - 1] == "BBTK_DESCRIPTION")
276         {
277           out << reading << "(\"" << descReal << "\")";
278           getline(in, reading, ')');
279         }
280       else
281         {
282           out << reading;
283           if (!in.eof())
284             out << "(";
285         }
286     }
287   in.close();
288   out.close();
289   //delete old file and rename new file
290   std::string renameCommand = "mv " + pathHeader + ".tmp " + pathHeader;
291   if(system(renameCommand.c_str()))
292     {
293       result = new std::string("An error occurred while running '" + renameCommand + "'.");
294       return false;
295     }
296
297   this->description = descReal;
298   return true;
299 }
300
301 void modelCDMBlackBox::SetHeaderFile(modelCDMFile* file)
302 {
303   this->header = file;
304 }
305
306 void modelCDMBlackBox::SetSourceFile(modelCDMFile* file)
307 {
308   this->source = file;
309 }
310
311 bool modelCDMBlackBox::OpenCxx(std::string*& result)
312 {
313   return !CDMUtilities::openTextEditor(this->source->GetPath());
314 }
315
316 bool modelCDMBlackBox::OpenHxx(std::string*& result)
317 {
318   return !CDMUtilities::openTextEditor(this->header->GetPath());
319 }
320
321 modelCDMFile* modelCDMBlackBox::GetHeaderFile() const
322 {
323   return this->header;
324 }
325
326 modelCDMFile* modelCDMBlackBox::GetSourceFile() const
327 {
328   return this->source;
329 }
330
331 const bool modelCDMBlackBox::Refresh(std::string*& result)
332 {
333   //TODO: implement method
334   return true;
335 }