2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
31 * Created on: Nov 23, 2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "CDMUtilities.h"
47 namespace CDMUtilities
49 const std::string fixPath(const std::string& path)
51 std::string pathFixed = "";
55 std::vector<std::string> pathSplit;
57 splitter::split(pathSplit, path, CDMUtilities::SLASH, splitter::no_empties);
59 if(0 < pathSplit.size())
60 pathFixed = pathSplit[0];
62 for (int i = 1; i < (int)(pathSplit.size()); i++)
64 pathFixed += CDMUtilities::SLASH + pathSplit[i];
67 // ------ LINUX / MacOS
68 //break path into folders
69 std::vector<std::string> pathSplit;
71 splitter::split(pathSplit, path, CDMUtilities::SLASH, splitter::no_empties);
73 for (int i = 0; i < pathSplit.size(); i++)
75 pathFixed += CDMUtilities::SLASH + pathSplit[i];
82 int openTextEditor(const std::string& file)
85 wxConfigBase* pConfig = wxConfigBase::Get();
86 std::string command = "start " + crea::wx2std(pConfig->Read(wxT("TEXT_EDITOR"), crea::std2wx(CDMUtilities::TEXT_EDITOR)));
89 command += " \"" + file + "\"";
91 wxConfigBase* pConfig = wxConfigBase::Get();
92 std::string command = crea::wx2std(pConfig->Read(wxT("TEXT_EDITOR"), crea::std2wx(CDMUtilities::TEXT_EDITOR)));
95 command += " \"" + file + "\"";
98 return system(command.c_str());
101 int openFileExplorer(const std::string& file)
104 wxConfigBase* pConfig = wxConfigBase::Get();
105 std::string command = "start " + crea::wx2std(pConfig->Read(wxT("FILE_EXPLORER"), crea::std2wx(CDMUtilities::FILE_EXPLORER)));
108 command += " \"" + file + "\"";
110 wxConfigBase* pConfig = wxConfigBase::Get();
111 std::string command = crea::wx2std(pConfig->Read(wxT("FILE_EXPLORER"), crea::std2wx(CDMUtilities::FILE_EXPLORER)));
114 command += " \"" + file + "\"";
117 return system(command.c_str());
120 int openFileWithCommand(const std::string& file, const std::string& command, const std::string& parameters)
123 std::string comm = "start " + command;
125 comm += " \"" + file + "\" " + parameters;
127 std::string comm = command;
129 comm += " \"" + file + "\" " + parameters;
132 std::cout << "executing: " << comm << std::endl;
133 return system(comm.c_str());
139 std::string comm = "start bbEditor";
141 std::string comm = "bbEditor &";
143 return system(comm.c_str());
146 int openCreaToolsTools()
149 std::string comm = "start creaTools";
151 std::string comm = "creaTools.sh &";
154 return system(comm.c_str());
157 int openTerminal(const std::string& command)
159 wxConfigBase* pConfig = wxConfigBase::Get();
160 std::string comm = crea::wx2std(pConfig->Read(wxT("TERMINAl"), crea::std2wx(CDMUtilities::TERMINAL)));
162 comm += + " " + command;
164 return system(comm.c_str());
167 bool createEmptyClass(const std::string& name, const std::string& path)
169 std::vector<std::string> words;
170 splitter::split(words,name," \\/\",.'`",splitter::no_empties);
171 std::string fixedName = "";
172 for (int i = 0; i < (int)(words.size()); i++)
174 fixedName += words[i];
177 if(fixedName == "" || path == "")
182 std::string nameupper = fixedName;
183 std::transform(nameupper.begin(), nameupper.end(),nameupper.begin(),::toupper);
185 std::ofstream out((path + SLASH + fixedName + ".h").c_str());
191 out << "/*" << std::endl;
192 out << "# ---------------------------------------------------------------------" << std::endl;
193 out << "#" << std::endl;
194 out << "# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image" << std::endl;
195 out << "# pour la Sante)" << std::endl;
196 out << "# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton" << std::endl;
197 out << "# Previous Authors : Laurent Guigues, Jean-Pierre Roux" << std::endl;
198 out << "# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil" << std::endl;
199 out << "#" << std::endl;
200 out << "# This software is governed by the CeCILL-B license under French law and" << std::endl;
201 out << "# abiding by the rules of distribution of free software. You can use," << std::endl;
202 out << "# modify and/ or redistribute the software under the terms of the CeCILL-B" << std::endl;
203 out << "# license as circulated by CEA, CNRS and INRIA at the following URL" << std::endl;
204 out << "# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html" << std::endl;
205 out << "# or in the file LICENSE.txt." << std::endl;
206 out << "#" << std::endl;
207 out << "# As a counterpart to the access to the source code and rights to copy," << std::endl;
208 out << "# modify and redistribute granted by the license, users are provided only" << std::endl;
209 out << "# with a limited warranty and the software's author, the holder of the" << std::endl;
210 out << "# economic rights, and the successive licensors have only limited" << std::endl;
211 out << "# liability." << std::endl;
212 out << "#" << std::endl;
213 out << "# The fact that you are presently reading this means that you have had" << std::endl;
214 out << "# knowledge of the CeCILL-B license and that you accept its terms." << std::endl;
215 out << "# ------------------------------------------------------------------------" << std::endl;
216 out << "*/" << std::endl;
217 out << "" << std::endl;
218 out << "#ifndef _" << nameupper << "_H_" << std::endl;
219 out << "#define _" << nameupper << "_H_" << std::endl;
220 out << "" << std::endl;
221 out << "//---------------------------------------------" << std::endl;
222 out << "// Class Name: " << fixedName << "" << std::endl;
223 out << "// [classdescription]" << std::endl;
224 out << "//---------------------------------------------" << std::endl;
225 out << "" << std::endl;
226 out << "class " << fixedName << "" << std::endl;
227 out << "{" << std::endl;
228 out << "" << std::endl;
229 out << "//---------------------------------------------" << std::endl;
230 out << "//Methods and attributes exposed to other classes" << std::endl;
231 out << "//---------------------------------------------" << std::endl;
232 out << "public :" << std::endl;
233 out << " " << fixedName << "();" << std::endl;
234 out << " ~" << fixedName << "();" << std::endl;
235 out << "" << std::endl;
236 out << "//--Method template----------------------------" << std::endl;
237 out << "// void FunctionName(int& parameterA);" << std::endl;
238 out << "" << std::endl;
239 out << "" << std::endl;
240 out << "//---------------------------------------------" << std::endl;
241 out << "//Methods and attributes exposed only to classes" << std::endl;
242 out << "//that are derived from this class" << std::endl;
243 out << "//---------------------------------------------" << std::endl;
244 out << "protected:" << std::endl;
245 out << "" << std::endl;
246 out << "//---------------------------------------------" << std::endl;
247 out << "//Methods and attributes only visible by this class" << std::endl;
248 out << "//---------------------------------------------" << std::endl;
249 out << "private:" << std::endl;
250 out << "" << std::endl;
251 out << "};" << std::endl;
252 out << "" << std::endl;
253 out << "//-end of _" << nameupper << "_H_------------------------------------------------------" << std::endl;
254 out << "#endif" << std::endl;
258 out.open((path + CDMUtilities::SLASH + fixedName + ".cpp").c_str());
264 out << "/*" << std::endl;
265 out << "# ---------------------------------------------------------------------" << std::endl;
266 out << "#" << std::endl;
267 out << "# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image" << std::endl;
268 out << "# pour la Sante)" << std::endl;
269 out << "# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton" << std::endl;
270 out << "# Previous Authors : Laurent Guigues, Jean-Pierre Roux" << std::endl;
271 out << "# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil" << std::endl;
272 out << "#" << std::endl;
273 out << "# This software is governed by the CeCILL-B license under French law and" << std::endl;
274 out << "# abiding by the rules of distribution of free software. You can use," << std::endl;
275 out << "# modify and/ or redistribute the software under the terms of the CeCILL-B" << std::endl;
276 out << "# license as circulated by CEA, CNRS and INRIA at the following URL" << std::endl;
277 out << "# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html" << std::endl;
278 out << "# or in the file LICENSE.txt." << std::endl;
279 out << "#" << std::endl;
280 out << "# As a counterpart to the access to the source code and rights to copy," << std::endl;
281 out << "# modify and redistribute granted by the license, users are provided only" << std::endl;
282 out << "# with a limited warranty and the software's author, the holder of the" << std::endl;
283 out << "# economic rights, and the successive licensors have only limited" << std::endl;
284 out << "# liability." << std::endl;
285 out << "#" << std::endl;
286 out << "# The fact that you are presently reading this means that you have had" << std::endl;
287 out << "# knowledge of the CeCILL-B license and that you accept its terms." << std::endl;
288 out << "# ------------------------------------------------------------------------" << std::endl;
289 out << "*/" << std::endl;
290 out << "" << std::endl;
291 out << "#include \"" << fixedName << ".h\"" << std::endl;
292 out << "" << std::endl;
293 out << "" << fixedName << "::" << fixedName << "()" << std::endl;
294 out << "{" << std::endl;
295 out << "}" << std::endl;
296 out << "" << std::endl;
297 out << "" << fixedName << "::~" << fixedName << "()" << std::endl;
298 out << "{" << std::endl;
299 out << "}" << std::endl;
300 out << "" << std::endl;
301 out << "//---------------------------------------------" << std::endl;
302 out << "//Method template" << std::endl;
303 out << "//---------------------------------------------" << std::endl;
304 out << "/*" << std::endl;
305 out << "void " << fixedName << "::FunctionName(int& parameterA)" << std::endl;
306 out << "{" << std::endl;
307 out << " parameterA = 2 * parameterA;" << std::endl;
308 out << " return;" << std::endl;
309 out << "}" << std::endl;
310 out << "*/" << std::endl;
315 std::string stringify(const std::string& line)
318 for (int i = 0; i < (int)(line.size()); i++)
324 res.push_back(line[i]);
329 std::string readFile(const std::string& file_path)
332 std::ifstream file(file_path.c_str());
335 char ch = file.get();
346 bool writeFile(const std::string& file_path, const std::string& st)
348 std::ofstream file(file_path.c_str());
359 CMLFile readCMLFile(const std::string& file_path)
363 std::ifstream file(file_path.c_str());
366 char ch = file.get();
369 syntaxElement element;
372 //std::cout << "space" << std::endl;
373 element.first = "space";
374 element.second.push_back(std::string(1,ch));
378 //std::cout << "comment" << std::endl;
379 element.first = "comment";
380 std::string commentValue;
383 commentValue.push_back(ch);
390 commentValue.push_back('\n');
391 element.second.push_back(commentValue);
395 //std::cout << "command" << std::endl;
396 element.first = "command";
397 std::string commandValue;
402 while(!isspace(ch) && ch != '(' && ch != ')' && ch != '#')
406 if (commandValue.size()) {
407 element.second.push_back(commandValue);
408 commandValue.clear();
410 commandValue.push_back(ch);
412 while(!file.eof() && ch != '"')
416 commandValue.push_back(ch);
419 commandValue.push_back(ch);
423 commandValue.push_back(ch);
428 commandValue.push_back(ch);
429 element.second.push_back(commandValue);
430 commandValue.clear();
433 commandValue.push_back(ch);
438 if (!file.eof() && (isspace(ch) || ch == '(' || ch == ')' || ch == '#'))
440 if (commandValue.size()) {
441 element.second.push_back(commandValue);
442 commandValue.clear();
444 commandValue.push_back(ch);
450 commandValue.push_back(ch);
453 element.second.push_back(commandValue);
454 if (commandValue == ")")
456 commandValue.clear();
459 commandValue.clear();
467 res.push_back(element);
478 std::cout << "CMakeLists: " << file_path << std::endl;
479 for (int i = 0; i < res.size(); ++i) {
480 if (res[i].first == "command")
482 for (int j = 0; j < res[i].second.size(); ++j) {
483 std::cout << "~" << res[i].second[j];
485 if (res[i].first == "command")
488 std::cout << "End of file" << std::endl;
493 bool writeCMLFile(const std::string& file_path, const CMLFile& data)
495 std::ofstream file(file_path.c_str());
498 for (int i = 0; i < data.size(); ++i) {
499 for (int j = 0; j < data[i].second.size(); ++j) {
500 file << data[i].second[j];
512 normalizeStr(std::string& st)
514 while(st.size() && isspace(st[0]))
516 while(st.size() && isspace(st[st.size()-1]))
517 st.erase(st.size()-1,1);