]> Creatis software - crea.git/blob - lib/creaDevManagerLib/CDMUtilities.cpp
Feature #1711
[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 namespace CDMUtilities
45 {
46   template <typename Container>
47   Container& splitter::split
48   (
49       Container& result,
50       const typename Container::value_type& s,
51       const typename Container::value_type& delimiters,
52       empties_t empties
53   )
54   {
55     result.clear();
56     size_t current;
57     size_t next = -1;
58     do
59       {
60         if (empties == no_empties)
61           {
62             next = s.find_first_not_of(delimiters, next + 1);
63             if (next == Container::value_type::npos)
64               {
65                 break;
66               }
67             next -= 1;
68           }
69         current = next + 1;
70         next = s.find_first_of(delimiters, current);
71         result.push_back(s.substr(current, next - current));
72       }
73     while (next != Container::value_type::npos);
74     return result;
75   }
76
77   const std::string fixPath(const std::string& path)
78   {
79     std::string pathFixed = "";
80
81 #if(_WIN32)
82     // ------ Windows
83     std::vector<std::string> pathSplit;
84
85     splitter::split(pathSplit, path, CDMUtilities::SLASH, splitter::no_empties);
86
87         if(0 < pathSplit.size())
88                 pathFixed = pathSplit[0];
89
90     for (int i = 1; i < (int)(pathSplit.size()); i++)
91       {
92         pathFixed += CDMUtilities::SLASH + pathSplit[i];
93       }
94 #else
95     // ------ LINUX / MacOS
96     //break path into folders
97     std::vector<std::string> pathSplit;
98
99     splitter::split(pathSplit, path, CDMUtilities::SLASH, splitter::no_empties);
100
101     for (int i = 0; i < pathSplit.size(); i++)
102       {
103         pathFixed += CDMUtilities::SLASH + pathSplit[i];
104       }
105 #endif
106     return pathFixed;
107
108   }
109
110   int openTextEditor(const std::string& file)
111   {
112     std::string command = TEXT_EDITOR;
113
114     if(file != "")
115       command += " \"" + file + "\"";
116     command += " &";
117
118     return system(command.c_str());
119   }
120
121   int openFileExplorer(const std::string& file)
122   {
123     std::string command = FILE_EXPLORER;
124
125     if(file != "")
126       command += " \"" + file + "\"";
127     command += " &";
128
129     return system(command.c_str());
130   }
131
132   int openFileWithCommand(const std::string& file, const std::string& command)
133   {
134     std::string comm = command;
135     if(file != "")
136       comm += " \"" + file + "\"";
137     comm += " &";
138
139     return system(comm.c_str());
140   }
141
142   int openBBEditor()
143   {
144     std::string comm = "bbEditor &";
145     return system(comm.c_str());
146   }
147
148   int openCreaToolsTools()
149   {
150 #ifdef _WIN32
151     std::string comm = "creaTools &";
152 #else
153     std::string comm = "creaTools.sh &";
154 #endif
155     
156     return system(comm.c_str());
157   }
158
159   int openTerminal(const std::string& command)
160   {
161     std::string comm = TERMINAL;
162     if (command != "")
163       comm += + " " + command;
164     comm += " &";
165     return system(comm.c_str());
166   }
167
168   bool createEmptyClass(const std::string& name, const std::string& path)
169   {
170     std::vector<std::string> words;
171     splitter::split(words,name," \\/\",.'`",splitter::no_empties);
172     std::string fixedName = "";
173     for (int i = 0; i < (int)(words.size()); i++)
174       {
175         fixedName += words[i];
176       }
177
178     if(fixedName == "" || path == "")
179       {
180         return false;
181       }
182
183     std::string nameupper = fixedName;
184     std::transform(nameupper.begin(), nameupper.end(),nameupper.begin(),::toupper);
185
186     std::ofstream out((path + SLASH + fixedName + ".h").c_str());
187     if( !out.is_open())
188       {
189         return false;
190       }
191
192     out << "/*" << std::endl;
193     out << "# ---------------------------------------------------------------------" << std::endl;
194     out << "#" << std::endl;
195     out << "# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image" << std::endl;
196     out << "#                        pour la Sante)" << std::endl;
197     out << "# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton" << std::endl;
198     out << "# Previous Authors : Laurent Guigues, Jean-Pierre Roux" << std::endl;
199     out << "# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil" << std::endl;
200     out << "#" << std::endl;
201     out << "#  This software is governed by the CeCILL-B license under French law and" << std::endl;
202     out << "#  abiding by the rules of distribution of free software. You can  use," << std::endl;
203     out << "#  modify and/ or redistribute the software under the terms of the CeCILL-B" << std::endl;
204     out << "#  license as circulated by CEA, CNRS and INRIA at the following URL" << std::endl;
205     out << "#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html" << std::endl;
206     out << "#  or in the file LICENSE.txt." << std::endl;
207     out << "#" << std::endl;
208     out << "#  As a counterpart to the access to the source code and  rights to copy," << std::endl;
209     out << "#  modify and redistribute granted by the license, users are provided only" << std::endl;
210     out << "#  with a limited warranty  and the software's author,  the holder of the" << std::endl;
211     out << "#  economic rights,  and the successive licensors  have only  limited" << std::endl;
212     out << "#  liability." << std::endl;
213     out << "#" << std::endl;
214     out << "#  The fact that you are presently reading this means that you have had" << std::endl;
215     out << "#  knowledge of the CeCILL-B license and that you accept its terms." << std::endl;
216     out << "# ------------------------------------------------------------------------" << std::endl;
217     out << "*/" << std::endl;
218     out << "" << std::endl;
219     out << "#ifndef _" << nameupper << "_H_" << std::endl;
220     out << "#define _" << nameupper << "_H_" << std::endl;
221     out << "" << std::endl;
222     out << "//---------------------------------------------" << std::endl;
223     out << "// Class Name: " << fixedName << "" << std::endl;
224     out << "// [classdescription]" << std::endl;
225     out << "//---------------------------------------------" << std::endl;
226     out << "" << std::endl;
227     out << "class " << fixedName << "" << std::endl;
228     out << "{" << std::endl;
229     out << "" << std::endl;
230     out << "//---------------------------------------------" << std::endl;
231     out << "//Methods and attributes exposed to other classes" << std::endl;
232     out << "//---------------------------------------------" << std::endl;
233     out << "public :" << std::endl;
234     out << "  " << fixedName << "();" << std::endl;
235     out << "  ~" << fixedName << "();" << std::endl;
236     out << "" << std::endl;
237     out << "//--Method template----------------------------" << std::endl;
238     out << "//  void FunctionName(int& parameterA);" << std::endl;
239     out << "" << std::endl;
240     out << "" << std::endl;
241     out << "//---------------------------------------------" << std::endl;
242     out << "//Methods and attributes exposed only to classes" << std::endl;
243     out << "//that are derived from this class" << std::endl;
244     out << "//---------------------------------------------" << std::endl;
245     out << "protected:" << std::endl;
246     out << "" << std::endl;
247     out << "//---------------------------------------------" << std::endl;
248     out << "//Methods and attributes only visible by this class" << std::endl;
249     out << "//---------------------------------------------" << std::endl;
250     out << "private:" << std::endl;
251     out << "" << std::endl;
252     out << "};" << std::endl;
253     out << "" << std::endl;
254     out << "//-end of _" << nameupper << "_H_------------------------------------------------------" << std::endl;
255     out << "#endif" << std::endl;
256
257     out.close();
258
259     out.open((path + CDMUtilities::SLASH + fixedName + ".cpp").c_str());
260     if( !out.is_open())
261       {
262         return false;
263       }
264
265     out << "/*" << std::endl;
266     out << "# ---------------------------------------------------------------------" << std::endl;
267     out << "#" << std::endl;
268     out << "# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image" << std::endl;
269     out << "#                        pour la Sante)" << std::endl;
270     out << "# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton" << std::endl;
271     out << "# Previous Authors : Laurent Guigues, Jean-Pierre Roux" << std::endl;
272     out << "# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil" << std::endl;
273     out << "#" << std::endl;
274     out << "#  This software is governed by the CeCILL-B license under French law and" << std::endl;
275     out << "#  abiding by the rules of distribution of free software. You can  use," << std::endl;
276     out << "#  modify and/ or redistribute the software under the terms of the CeCILL-B" << std::endl;
277     out << "#  license as circulated by CEA, CNRS and INRIA at the following URL" << std::endl;
278     out << "#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html" << std::endl;
279     out << "#  or in the file LICENSE.txt." << std::endl;
280     out << "#" << std::endl;
281     out << "#  As a counterpart to the access to the source code and  rights to copy," << std::endl;
282     out << "#  modify and redistribute granted by the license, users are provided only" << std::endl;
283     out << "#  with a limited warranty  and the software's author,  the holder of the" << std::endl;
284     out << "#  economic rights,  and the successive licensors  have only  limited" << std::endl;
285     out << "#  liability." << std::endl;
286     out << "#" << std::endl;
287     out << "#  The fact that you are presently reading this means that you have had" << std::endl;
288     out << "#  knowledge of the CeCILL-B license and that you accept its terms." << std::endl;
289     out << "# ------------------------------------------------------------------------" << std::endl;
290     out << "*/" << std::endl;
291     out << "" << std::endl;
292     out << "#include \"" << fixedName << ".h\"" << std::endl;
293     out << "" << std::endl;
294     out << "" << fixedName << "::" << fixedName << "()" << std::endl;
295     out << "{" << std::endl;
296     out << "}" << std::endl;
297     out << "" << std::endl;
298     out << "" << fixedName << "::~" << fixedName << "()" << std::endl;
299     out << "{" << std::endl;
300     out << "}" << std::endl;
301     out << "" << std::endl;
302     out << "//---------------------------------------------" << std::endl;
303     out << "//Method template" << std::endl;
304     out << "//---------------------------------------------" << std::endl;
305     out << "/*" << std::endl;
306     out << "void " << fixedName << "::FunctionName(int& parameterA)" << std::endl;
307     out << "{" << std::endl;
308     out << "  parameterA = 2 * parameterA;" << std::endl;
309     out << "  return;" << std::endl;
310     out << "}" << std::endl;
311     out << "*/" << std::endl;
312
313     return true;
314   }
315
316   std::string stringify(const std::string& line)
317   {
318         std::string res;
319         for (int i = 0; i < (int)(line.size()); i++)
320         {
321           if(line[i] == '\\')
322             res.push_back('\\');
323           if(line[i] == '\"')
324             res.push_back('\\');
325           res.push_back(line[i]);
326         }
327         return res;
328   }
329
330 }