]> Creatis software - crea.git/blob - lib/creaDevManagerLib/CDMUtilities.cpp
e683862c24ec7f022dfb48b72b3c1d7289342cbb
[crea.git] / lib / creaDevManagerLib / CDMUtilities.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  * CDMUtilities.cpp
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "CDMUtilities.h"
36
37 #include<vector>
38 #include<string>
39 #include<iostream>
40 #include<fstream>
41 #include<algorithm>
42 #include<cstdlib>
43
44 #include<creaWx.h>
45 #include<wx/config.h>
46
47 namespace CDMUtilities
48 {
49   const std::string fixPath(const std::string& path)
50   {
51     std::string pathFixed = "";
52
53 #ifdef _WIN32
54     // ------ Windows
55     std::vector<std::string> pathSplit;
56
57     splitter::split(pathSplit, path, CDMUtilities::SLASH, splitter::no_empties);
58
59         if(0 < pathSplit.size())
60                 pathFixed = pathSplit[0];
61
62     for (int i = 1; i < (int)(pathSplit.size()); i++)
63       {
64         pathFixed += CDMUtilities::SLASH + pathSplit[i];
65       }
66 #else
67     // ------ LINUX / MacOS
68     //break path into folders
69     std::vector<std::string> pathSplit;
70
71     splitter::split(pathSplit, path, CDMUtilities::SLASH, splitter::no_empties);
72
73     for (int i = 0; i < pathSplit.size(); i++)
74       {
75         pathFixed += CDMUtilities::SLASH + pathSplit[i];
76       }
77 #endif
78     return pathFixed;
79
80   }
81
82   int openTextEditor(const std::string& file)
83   {
84 #ifdef _WIN32
85     wxConfigBase* pConfig =  wxConfigBase::Get();
86     std::string command = "start " + crea::wx2std(pConfig->Read(wxT("TEXT_EDITOR"), crea::std2wx(CDMUtilities::TEXT_EDITOR)));
87
88     if(file != "")
89       command += " \"" + file + "\"";
90 #else
91     wxConfigBase* pConfig =  wxConfigBase::Get();
92     std::string command = crea::wx2std(pConfig->Read(wxT("TEXT_EDITOR"), crea::std2wx(CDMUtilities::TEXT_EDITOR)));
93
94     if(file != "")
95       command += " \"" + file + "\"";
96     command += " &";
97 #endif
98     return system(command.c_str());
99   }
100
101   int openFileExplorer(const std::string& file)
102   {
103 #ifdef _WIN32
104     wxConfigBase* pConfig =  wxConfigBase::Get();
105     std::string command = "start " + crea::wx2std(pConfig->Read(wxT("FILE_EXPLORER"), crea::std2wx(CDMUtilities::FILE_EXPLORER)));
106
107     if(file != "")
108       command += " \"" + file + "\"";
109 #else
110     wxConfigBase* pConfig =  wxConfigBase::Get();
111     std::string command = crea::wx2std(pConfig->Read(wxT("FILE_EXPLORER"), crea::std2wx(CDMUtilities::FILE_EXPLORER)));
112
113     if(file != "")
114       command += " \"" + file + "\"";
115     command += " &";
116 #endif
117     return system(command.c_str());
118   }
119
120   int openFileWithCommand(const std::string& file, const std::string& command, const std::string& parameters)
121   {
122 #ifdef _WIN32
123     std::string comm = "start " + command;
124     if(file != "")
125       comm += " \"" + file + "\" " + parameters;
126 #else
127     std::string comm = command;
128     if(file != "")
129       comm += " \"" + file + "\" " + parameters;
130     comm += " &";
131 #endif
132     return system(comm.c_str());
133   }
134
135   int openBBEditor()
136   {
137 #ifdef _WIN32
138     std::string comm = "start bbEditor";
139 #else
140         std::string comm = "bbEditor &";
141 #endif
142     return system(comm.c_str());
143   }
144
145   int openCreaToolsTools()
146   {
147 #ifdef _WIN32
148     std::string comm = "start creaTools";
149 #else
150     std::string comm = "creaTools.sh &";
151 #endif
152     
153     return system(comm.c_str());
154   }
155
156   int openTerminal(const std::string& command)
157   {
158     wxConfigBase* pConfig =  wxConfigBase::Get();
159     std::string comm = crea::wx2std(pConfig->Read(wxT("TERMINAl"), crea::std2wx(CDMUtilities::TERMINAL)));
160     if (command != "")
161       comm += + " " + command;
162     comm += " &";
163     return system(comm.c_str());
164   }
165
166   bool createEmptyClass(const std::string& name, const std::string& path)
167   {
168     std::vector<std::string> words;
169     splitter::split(words,name," \\/\",.'`",splitter::no_empties);
170     std::string fixedName = "";
171     for (int i = 0; i < (int)(words.size()); i++)
172       {
173         fixedName += words[i];
174       }
175
176     if(fixedName == "" || path == "")
177       {
178         return false;
179       }
180
181     std::string nameupper = fixedName;
182     std::transform(nameupper.begin(), nameupper.end(),nameupper.begin(),::toupper);
183
184     std::ofstream out((path + SLASH + fixedName + ".h").c_str());
185     if( !out.is_open())
186       {
187         return false;
188       }
189
190     out << "/*" << std::endl;
191     out << "# ---------------------------------------------------------------------" << std::endl;
192     out << "#" << std::endl;
193     out << "# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image" << std::endl;
194     out << "#                        pour la Sante)" << std::endl;
195     out << "# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton" << std::endl;
196     out << "# Previous Authors : Laurent Guigues, Jean-Pierre Roux" << std::endl;
197     out << "# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil" << std::endl;
198     out << "#" << std::endl;
199     out << "#  This software is governed by the CeCILL-B license under French law and" << std::endl;
200     out << "#  abiding by the rules of distribution of free software. You can  use," << std::endl;
201     out << "#  modify and/ or redistribute the software under the terms of the CeCILL-B" << std::endl;
202     out << "#  license as circulated by CEA, CNRS and INRIA at the following URL" << std::endl;
203     out << "#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html" << std::endl;
204     out << "#  or in the file LICENSE.txt." << std::endl;
205     out << "#" << std::endl;
206     out << "#  As a counterpart to the access to the source code and  rights to copy," << std::endl;
207     out << "#  modify and redistribute granted by the license, users are provided only" << std::endl;
208     out << "#  with a limited warranty  and the software's author,  the holder of the" << std::endl;
209     out << "#  economic rights,  and the successive licensors  have only  limited" << std::endl;
210     out << "#  liability." << std::endl;
211     out << "#" << std::endl;
212     out << "#  The fact that you are presently reading this means that you have had" << std::endl;
213     out << "#  knowledge of the CeCILL-B license and that you accept its terms." << std::endl;
214     out << "# ------------------------------------------------------------------------" << std::endl;
215     out << "*/" << std::endl;
216     out << "" << std::endl;
217     out << "#ifndef _" << nameupper << "_H_" << std::endl;
218     out << "#define _" << nameupper << "_H_" << std::endl;
219     out << "" << std::endl;
220     out << "//---------------------------------------------" << std::endl;
221     out << "// Class Name: " << fixedName << "" << std::endl;
222     out << "// [classdescription]" << std::endl;
223     out << "//---------------------------------------------" << std::endl;
224     out << "" << std::endl;
225     out << "class " << fixedName << "" << std::endl;
226     out << "{" << std::endl;
227     out << "" << std::endl;
228     out << "//---------------------------------------------" << std::endl;
229     out << "//Methods and attributes exposed to other classes" << std::endl;
230     out << "//---------------------------------------------" << std::endl;
231     out << "public :" << std::endl;
232     out << "  " << fixedName << "();" << std::endl;
233     out << "  ~" << fixedName << "();" << std::endl;
234     out << "" << std::endl;
235     out << "//--Method template----------------------------" << std::endl;
236     out << "//  void FunctionName(int& parameterA);" << std::endl;
237     out << "" << std::endl;
238     out << "" << std::endl;
239     out << "//---------------------------------------------" << std::endl;
240     out << "//Methods and attributes exposed only to classes" << std::endl;
241     out << "//that are derived from this class" << std::endl;
242     out << "//---------------------------------------------" << std::endl;
243     out << "protected:" << std::endl;
244     out << "" << std::endl;
245     out << "//---------------------------------------------" << std::endl;
246     out << "//Methods and attributes only visible by this class" << std::endl;
247     out << "//---------------------------------------------" << std::endl;
248     out << "private:" << std::endl;
249     out << "" << std::endl;
250     out << "};" << std::endl;
251     out << "" << std::endl;
252     out << "//-end of _" << nameupper << "_H_------------------------------------------------------" << std::endl;
253     out << "#endif" << std::endl;
254
255     out.close();
256
257     out.open((path + CDMUtilities::SLASH + fixedName + ".cpp").c_str());
258     if( !out.is_open())
259       {
260         return false;
261       }
262
263     out << "/*" << std::endl;
264     out << "# ---------------------------------------------------------------------" << std::endl;
265     out << "#" << std::endl;
266     out << "# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image" << std::endl;
267     out << "#                        pour la Sante)" << std::endl;
268     out << "# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton" << std::endl;
269     out << "# Previous Authors : Laurent Guigues, Jean-Pierre Roux" << std::endl;
270     out << "# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil" << std::endl;
271     out << "#" << std::endl;
272     out << "#  This software is governed by the CeCILL-B license under French law and" << std::endl;
273     out << "#  abiding by the rules of distribution of free software. You can  use," << std::endl;
274     out << "#  modify and/ or redistribute the software under the terms of the CeCILL-B" << std::endl;
275     out << "#  license as circulated by CEA, CNRS and INRIA at the following URL" << std::endl;
276     out << "#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html" << std::endl;
277     out << "#  or in the file LICENSE.txt." << std::endl;
278     out << "#" << std::endl;
279     out << "#  As a counterpart to the access to the source code and  rights to copy," << std::endl;
280     out << "#  modify and redistribute granted by the license, users are provided only" << std::endl;
281     out << "#  with a limited warranty  and the software's author,  the holder of the" << std::endl;
282     out << "#  economic rights,  and the successive licensors  have only  limited" << std::endl;
283     out << "#  liability." << std::endl;
284     out << "#" << std::endl;
285     out << "#  The fact that you are presently reading this means that you have had" << std::endl;
286     out << "#  knowledge of the CeCILL-B license and that you accept its terms." << std::endl;
287     out << "# ------------------------------------------------------------------------" << std::endl;
288     out << "*/" << std::endl;
289     out << "" << std::endl;
290     out << "#include \"" << fixedName << ".h\"" << std::endl;
291     out << "" << std::endl;
292     out << "" << fixedName << "::" << fixedName << "()" << std::endl;
293     out << "{" << std::endl;
294     out << "}" << std::endl;
295     out << "" << std::endl;
296     out << "" << fixedName << "::~" << fixedName << "()" << std::endl;
297     out << "{" << std::endl;
298     out << "}" << std::endl;
299     out << "" << std::endl;
300     out << "//---------------------------------------------" << std::endl;
301     out << "//Method template" << std::endl;
302     out << "//---------------------------------------------" << std::endl;
303     out << "/*" << std::endl;
304     out << "void " << fixedName << "::FunctionName(int& parameterA)" << std::endl;
305     out << "{" << std::endl;
306     out << "  parameterA = 2 * parameterA;" << std::endl;
307     out << "  return;" << std::endl;
308     out << "}" << std::endl;
309     out << "*/" << std::endl;
310
311     return true;
312   }
313
314   std::string stringify(const std::string& line)
315   {
316         std::string res;
317         for (int i = 0; i < (int)(line.size()); i++)
318         {
319           if(line[i] == '\\')
320             res.push_back('\\');
321           if(line[i] == '\"')
322             res.push_back('\\');
323           res.push_back(line[i]);
324         }
325         return res;
326   }
327
328   std::string readFile(const std::string& file_path)
329   {
330     std::string res;
331     std::ifstream file(file_path.c_str());
332     if (file.is_open())
333       {
334         char ch = file.get();
335         while (!file.eof())
336           {
337             res.push_back(ch);
338             ch = file.get();
339           }
340         file.close();
341       }
342     return res;
343   }
344
345   bool writeFile(const std::string& file_path, const std::string& st)
346   {
347     std::ofstream file(file_path.c_str());
348     if (file.is_open())
349       {
350         file << st;
351         file.close();
352         return true;
353       }
354     return false;
355
356   }
357
358   CMLFile readCMLFile(const std::string& file_path)
359   {
360     CMLFile res;
361
362     std::ifstream file(file_path.c_str());
363     if (file.is_open())
364       {
365         char ch = file.get();
366         while (!file.eof())
367           {
368             syntaxElement element;
369             if (isspace(ch))
370               {
371                 //std::cout << "space" << std::endl;
372                 element.first = "space";
373                 element.second.push_back(std::string(1,ch));
374               }
375             else if (ch == '#')
376               {
377                 //std::cout << "comment" << std::endl;
378                 element.first = "comment";
379                 std::string commentValue;
380                 while (ch != '\n')
381                   {
382                     commentValue.push_back(ch);
383
384                     ch = file.get();
385                     if (file.eof())
386                       break;
387                   };
388                 if (!file.eof())
389                   commentValue.push_back('\n');
390                 element.second.push_back(commentValue);
391               }
392             else
393               {
394                 //std::cout << "command" << std::endl;
395                 element.first = "command";
396                 std::string commandValue;
397                 while (true)
398                   {
399                     //std::cout << ch;
400                     //std::cout.flush();
401                     while(!isspace(ch) && ch != '(' && ch != ')' && ch != '#')
402                       {
403                         if(ch == '"')
404                           {
405                             if (commandValue.size()) {
406                               element.second.push_back(commandValue);
407                               commandValue.clear();
408                             }
409                             commandValue.push_back(ch);
410                             ch = file.get();
411                             while(!file.eof() && ch != '"')
412                               {
413                                 if(ch == '\\')
414                                   {
415                                     commandValue.push_back(ch);
416                                     ch = file.get();
417                                     if(!file.eof())
418                                       commandValue.push_back(ch);
419                                   }
420                                 else
421                                   {
422                                     commandValue.push_back(ch);
423                                   }
424                                 ch = file.get();
425                               }
426                             if(!file.eof())
427                               commandValue.push_back(ch);
428                             element.second.push_back(commandValue);
429                             commandValue.clear();
430                           }
431                         else
432                           commandValue.push_back(ch);
433
434                         ch = file.get();
435                       }
436
437                     if (!file.eof() && (isspace(ch) || ch == '(' || ch == ')' || ch == '#'))
438                       {
439                         if (commandValue.size()) {
440                           element.second.push_back(commandValue);
441                           commandValue.clear();
442                         }
443                         commandValue.push_back(ch);
444                         if (ch == '#') {
445                           while (ch != '\n') {
446                             ch = file.get();
447                             if (file.eof())
448                               break;
449                             commandValue.push_back(ch);
450                           };
451                         }
452                         element.second.push_back(commandValue);
453                         if (commandValue == ")")
454                           {
455                             commandValue.clear();
456                             break;
457                           }
458                         commandValue.clear();
459                       }
460
461                     ch = file.get();
462                     if (file.eof())
463                       break;
464                   }
465               }
466             res.push_back(element);
467
468             ch = file.get();
469             if (file.eof())
470               break;
471           }
472
473         file.close();
474       }
475
476 /*
477     std::cout << "CMakeLists: " << file_path << std::endl;
478     for (int i = 0; i < res.size(); ++i) {
479       if (res[i].first == "command")
480         std::cout << "@";
481       for (int j = 0; j < res[i].second.size(); ++j) {
482         std::cout << "~" << res[i].second[j];
483       }
484       if (res[i].first == "command")
485         std::cout << "@";
486     }
487     std::cout << "End of file" << std::endl;
488 */
489     return res;
490   }
491
492   bool writeCMLFile(const std::string& file_path, const CMLFile& data)
493   {
494     std::ofstream file(file_path.c_str());
495     if(file.is_open())
496       {
497         for (int i = 0; i < data.size(); ++i) {
498           for (int j = 0; j < data[i].second.size(); ++j) {
499             file << data[i].second[j];
500           }
501         }
502         file.close();
503         return true;
504       }
505     return false;
506   }
507
508
509
510   void
511   normalizeStr(std::string& st)
512   {
513     while(st.size() && isspace(st[0]))
514       st.erase(0,1);
515     while(st.size() && isspace(st[st.size()-1]))
516       st.erase(st.size()-1,1);
517     return;
518   }
519
520 }