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