]> 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 #if(_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     wxConfigBase* pConfig =  wxConfigBase::Get();
116     std::string command = crea::wx2std(pConfig->Read(wxT("TEXT_EDITOR"), crea::std2wx(CDMUtilities::TEXT_EDITOR)));
117
118     if(file != "")
119       command += " \"" + file + "\"";
120     command += " &";
121
122     return system(command.c_str());
123   }
124
125   int openFileExplorer(const std::string& file)
126   {
127     wxConfigBase* pConfig =  wxConfigBase::Get();
128     std::string command = crea::wx2std(pConfig->Read(wxT("FILE_EXPLORER"), crea::std2wx(CDMUtilities::FILE_EXPLORER)));
129
130     if(file != "")
131       command += " \"" + file + "\"";
132     command += " &";
133
134     return system(command.c_str());
135   }
136
137   int openFileWithCommand(const std::string& file, const std::string& command)
138   {
139     std::string comm = command;
140     if(file != "")
141       comm += " \"" + file + "\"";
142     comm += " &";
143
144     return system(comm.c_str());
145   }
146
147   int openBBEditor()
148   {
149     std::string comm = "bbEditor &";
150     return system(comm.c_str());
151   }
152
153   int openCreaToolsTools()
154   {
155 #ifdef _WIN32
156     std::string comm = "creaTools &";
157 #else
158     std::string comm = "creaTools.sh &";
159 #endif
160     
161     return system(comm.c_str());
162   }
163
164   int openTerminal(const std::string& command)
165   {
166     wxConfigBase* pConfig =  wxConfigBase::Get();
167     std::string comm = crea::wx2std(pConfig->Read(wxT("TERMINAl"), crea::std2wx(CDMUtilities::TERMINAL)));
168     if (command != "")
169       comm += + " " + command;
170     comm += " &";
171     return system(comm.c_str());
172   }
173
174   bool createEmptyClass(const std::string& name, const std::string& path)
175   {
176     std::vector<std::string> words;
177     splitter::split(words,name," \\/\",.'`",splitter::no_empties);
178     std::string fixedName = "";
179     for (int i = 0; i < (int)(words.size()); i++)
180       {
181         fixedName += words[i];
182       }
183
184     if(fixedName == "" || path == "")
185       {
186         return false;
187       }
188
189     std::string nameupper = fixedName;
190     std::transform(nameupper.begin(), nameupper.end(),nameupper.begin(),::toupper);
191
192     std::ofstream out((path + SLASH + fixedName + ".h").c_str());
193     if( !out.is_open())
194       {
195         return false;
196       }
197
198     out << "/*" << std::endl;
199     out << "# ---------------------------------------------------------------------" << std::endl;
200     out << "#" << std::endl;
201     out << "# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image" << std::endl;
202     out << "#                        pour la Sante)" << std::endl;
203     out << "# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton" << std::endl;
204     out << "# Previous Authors : Laurent Guigues, Jean-Pierre Roux" << std::endl;
205     out << "# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil" << std::endl;
206     out << "#" << std::endl;
207     out << "#  This software is governed by the CeCILL-B license under French law and" << std::endl;
208     out << "#  abiding by the rules of distribution of free software. You can  use," << std::endl;
209     out << "#  modify and/ or redistribute the software under the terms of the CeCILL-B" << std::endl;
210     out << "#  license as circulated by CEA, CNRS and INRIA at the following URL" << std::endl;
211     out << "#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html" << std::endl;
212     out << "#  or in the file LICENSE.txt." << std::endl;
213     out << "#" << std::endl;
214     out << "#  As a counterpart to the access to the source code and  rights to copy," << std::endl;
215     out << "#  modify and redistribute granted by the license, users are provided only" << std::endl;
216     out << "#  with a limited warranty  and the software's author,  the holder of the" << std::endl;
217     out << "#  economic rights,  and the successive licensors  have only  limited" << std::endl;
218     out << "#  liability." << std::endl;
219     out << "#" << std::endl;
220     out << "#  The fact that you are presently reading this means that you have had" << std::endl;
221     out << "#  knowledge of the CeCILL-B license and that you accept its terms." << std::endl;
222     out << "# ------------------------------------------------------------------------" << std::endl;
223     out << "*/" << std::endl;
224     out << "" << std::endl;
225     out << "#ifndef _" << nameupper << "_H_" << std::endl;
226     out << "#define _" << nameupper << "_H_" << std::endl;
227     out << "" << std::endl;
228     out << "//---------------------------------------------" << std::endl;
229     out << "// Class Name: " << fixedName << "" << std::endl;
230     out << "// [classdescription]" << std::endl;
231     out << "//---------------------------------------------" << std::endl;
232     out << "" << std::endl;
233     out << "class " << fixedName << "" << std::endl;
234     out << "{" << std::endl;
235     out << "" << std::endl;
236     out << "//---------------------------------------------" << std::endl;
237     out << "//Methods and attributes exposed to other classes" << std::endl;
238     out << "//---------------------------------------------" << std::endl;
239     out << "public :" << std::endl;
240     out << "  " << fixedName << "();" << std::endl;
241     out << "  ~" << fixedName << "();" << std::endl;
242     out << "" << std::endl;
243     out << "//--Method template----------------------------" << std::endl;
244     out << "//  void FunctionName(int& parameterA);" << std::endl;
245     out << "" << std::endl;
246     out << "" << std::endl;
247     out << "//---------------------------------------------" << std::endl;
248     out << "//Methods and attributes exposed only to classes" << std::endl;
249     out << "//that are derived from this class" << std::endl;
250     out << "//---------------------------------------------" << std::endl;
251     out << "protected:" << std::endl;
252     out << "" << std::endl;
253     out << "//---------------------------------------------" << std::endl;
254     out << "//Methods and attributes only visible by this class" << std::endl;
255     out << "//---------------------------------------------" << std::endl;
256     out << "private:" << std::endl;
257     out << "" << std::endl;
258     out << "};" << std::endl;
259     out << "" << std::endl;
260     out << "//-end of _" << nameupper << "_H_------------------------------------------------------" << std::endl;
261     out << "#endif" << std::endl;
262
263     out.close();
264
265     out.open((path + CDMUtilities::SLASH + fixedName + ".cpp").c_str());
266     if( !out.is_open())
267       {
268         return false;
269       }
270
271     out << "/*" << std::endl;
272     out << "# ---------------------------------------------------------------------" << std::endl;
273     out << "#" << std::endl;
274     out << "# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image" << std::endl;
275     out << "#                        pour la Sante)" << std::endl;
276     out << "# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton" << std::endl;
277     out << "# Previous Authors : Laurent Guigues, Jean-Pierre Roux" << std::endl;
278     out << "# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil" << std::endl;
279     out << "#" << std::endl;
280     out << "#  This software is governed by the CeCILL-B license under French law and" << std::endl;
281     out << "#  abiding by the rules of distribution of free software. You can  use," << std::endl;
282     out << "#  modify and/ or redistribute the software under the terms of the CeCILL-B" << std::endl;
283     out << "#  license as circulated by CEA, CNRS and INRIA at the following URL" << std::endl;
284     out << "#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html" << std::endl;
285     out << "#  or in the file LICENSE.txt." << std::endl;
286     out << "#" << std::endl;
287     out << "#  As a counterpart to the access to the source code and  rights to copy," << std::endl;
288     out << "#  modify and redistribute granted by the license, users are provided only" << std::endl;
289     out << "#  with a limited warranty  and the software's author,  the holder of the" << std::endl;
290     out << "#  economic rights,  and the successive licensors  have only  limited" << std::endl;
291     out << "#  liability." << std::endl;
292     out << "#" << std::endl;
293     out << "#  The fact that you are presently reading this means that you have had" << std::endl;
294     out << "#  knowledge of the CeCILL-B license and that you accept its terms." << std::endl;
295     out << "# ------------------------------------------------------------------------" << std::endl;
296     out << "*/" << std::endl;
297     out << "" << std::endl;
298     out << "#include \"" << fixedName << ".h\"" << std::endl;
299     out << "" << std::endl;
300     out << "" << fixedName << "::" << fixedName << "()" << std::endl;
301     out << "{" << std::endl;
302     out << "}" << std::endl;
303     out << "" << std::endl;
304     out << "" << fixedName << "::~" << fixedName << "()" << std::endl;
305     out << "{" << std::endl;
306     out << "}" << std::endl;
307     out << "" << std::endl;
308     out << "//---------------------------------------------" << std::endl;
309     out << "//Method template" << std::endl;
310     out << "//---------------------------------------------" << std::endl;
311     out << "/*" << std::endl;
312     out << "void " << fixedName << "::FunctionName(int& parameterA)" << std::endl;
313     out << "{" << std::endl;
314     out << "  parameterA = 2 * parameterA;" << std::endl;
315     out << "  return;" << std::endl;
316     out << "}" << std::endl;
317     out << "*/" << std::endl;
318
319     return true;
320   }
321
322   std::string stringify(const std::string& line)
323   {
324         std::string res;
325         for (int i = 0; i < (int)(line.size()); i++)
326         {
327           if(line[i] == '\\')
328             res.push_back('\\');
329           if(line[i] == '\"')
330             res.push_back('\\');
331           res.push_back(line[i]);
332         }
333         return res;
334   }
335
336 }