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