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