]> Creatis software - crea.git/blob - lib/creaDevManagerLib/CDMUtilities.h
#3214 creaFeature New Normal - vtk8itk4wx3-mingw64
[crea.git] / lib / creaDevManagerLib / CDMUtilities.h
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.h
30  *
31  *  Created on: Nov 23, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #ifndef CDMUTILITIES_H_
36 #define CDMUTILITIES_H_
37
38 #include<iostream>
39 #include<vector>
40 #include<cstddef>
41
42 namespace CDMUtilities
43 {
44   /**
45    * Path slash
46    */
47 #ifdef _WIN32
48   // ------ Windows
49   static std::string SLASH = "\\";
50 #elif __APPLE__
51   // ------ Apple
52   static std::string SLASH = "/";
53 #else
54   static std::string SLASH = "/";
55 #endif
56
57   /**
58    * Text editor program
59    */
60 #ifdef _WIN32
61   // ------ Windows
62   static std::string TEXT_EDITOR = "notepad";
63 #elif __APPLE__
64   // ------ Apple
65   //TODO: implementation for apple
66   static std::string TEXT_EDITOR = "open -e";
67 #else
68   static std::string TEXT_EDITOR = "gedit";
69 #endif
70
71   /**
72    * File explorer program
73    */
74 #ifdef _WIN32
75   // ------ Windows
76   static std::string FILE_EXPLORER = "explorer";
77 #elif __APPLE__
78   // ------ Apple
79   //TODO: implementation for apple
80   static std::string FILE_EXPLORER = "open ";
81 #else
82   static std::string FILE_EXPLORER = "nautilus";
83 #endif
84
85   /**
86    * Terminal program
87    */
88 #ifdef _WIN32
89   // ------ Windows
90   static std::string TERMINAL = "start cmd.exe";
91 #elif __APPLE__
92   // ------ Apple
93   //TODO: implementation for apple
94   static std::string TERMINAL = "open -a Terminal ";  
95 #else
96   static std::string TERMINAL = "gnome-terminal";
97 #endif
98
99
100 //EED 10/07/2013
101 //#ifndef _WIN32
102   /**
103    * Build Command
104    */
105   static std::string BUILD_COMMAND = "make";
106 //#endif
107
108   /**
109    * Structure that handles the split method for c++
110    * It calls the split method to split a string given certain delimiters.
111    */
112   struct splitter
113   {
114     /**
115      * Enum to allow or not empty resulting strings after performing splits.
116      */
117     enum empties_t { empties_ok, no_empties };
118     /**
119      * Method to split a string given a set of delimiter characters.
120      * @param result Resulting container.
121      * @param s String to be splitted.
122      * @param delimiters Delimiter characters to split the string.
123      * @param empties Either allow or not empty resulting strings after performing split.
124      * @return Resulting container.
125      */
126     template <typename Container>
127     static Container& split
128     (
129         Container& result,
130         const typename Container::value_type& s,
131         const typename Container::value_type& delimiters,
132         empties_t empties = empties_ok
133       )
134       {
135         result.clear();
136         size_t current;
137         size_t next = -1;
138         do
139         {
140           if (empties == no_empties)
141           {
142             next = s.find_first_not_of(delimiters, next + 1);
143             if (next == Container::value_type::npos)
144             {
145               break;
146             }
147             next -= 1;
148           }
149           current = next + 1;
150           next = s.find_first_of(delimiters, current);
151           result.push_back(s.substr(current, next - current));
152         }
153         while (next != Container::value_type::npos);
154         return result;
155       }
156   };
157
158   /**
159    * Fixes a given path to avoid double slash directories
160    * @param path Unfixed path.
161    * @return Fixed path.
162    */
163   const std::string fixPath(const std::string& path);
164
165   /**
166    * Opens the default text editor. If a file is given, then it tries to open the given file.
167    * @param file Full path to the file.
168    * @return True if there was an error on the execution of the operation.
169    */
170   int openTextEditor(const std::string& file = "");
171   /**
172    * Opens the system file explorer on the given file path
173    * @param file Path of the desired folder to open.
174    * @return True if there was an error on the execution of the operation.
175    */
176   int openFileExplorer(const std::string& file = "");
177   /**
178    * Opens a file with a given command.
179    * @param file Full path of the file to open.
180    * @param command Command to execute the file with.
181    * @param parameters Parameters to open file.
182    * @return True if there was an error on the execution of the operation.
183    */
184   int openFileWithCommand(const std::string& file, const std::string& command, const std::string& parameters = "");
185   /**
186    * Opens the BBTK Graphical Editor
187    * @return True if there was an error on the execution of the operation.
188    */
189   int openBBEditor();
190   /**
191    * Opens the minitools or the creaTools
192    * @return True if there was an error on the execution of the operation.
193    */
194   int openCreaToolsTools();
195   /**
196    * Open a command line interpreter and executes the given command if any.
197    * @param command Command to execute.
198    * @return True if there was an error on the execution of the operation.
199    */
200   int openTerminal(const std::string& command = "");
201   /**
202    * Creates a blank class(.h and .cpp files).
203    * @param name Name of the new class.
204    * @param path Path where the class is to be created.
205    * @return True if the class was successfully created.
206    */
207   bool createEmptyClass(const std::string& name, const std::string& path);
208   /**
209    * Creates a string replacing each \ by double \ .
210    * @param line String to stringify.
211    * @return line stringified.
212    */
213   std::string stringify(const std::string& line);
214
215   //CMakeLists file handling
216   /**
217    * Type definition for the value of a syntax element for CMakeLists files
218    */
219   typedef std::vector<std::string> cmdValue;
220
221   /**
222    * Type definition for the type of a syntax element for CMakeLists files
223    */
224   typedef std::string cmdType;
225
226   /**
227    * Type definition for syntax elements of a CMakeLists file
228    */
229   typedef std::pair<cmdType,cmdValue> syntaxElement;
230
231   /**
232    * Type definition for describing a CMakeLists file content
233    */
234   typedef std::vector<syntaxElement> CMLFile;
235
236   /**
237    * Reads a file as string and returns the read data.
238    * @param file_path Full path of the CMakeLists file.
239    * @return A string with the contents of the given file.
240    */
241   std::string readFile(const std::string& file_path);
242   /**
243    * Writes the given string into a file and returns whether the operation is successful.
244    * @param file_path Full path of the CMakeLists file.
245    * @param st string to write.
246    * @return True if the operation was successful.
247    */
248   bool writeFile(const std::string& file_path, const std::string& st);
249
250   /**
251    * Reads a CMakeLists file and returns the read data.
252    * @param file_path Full path of the CMakeLists file.
253    * @return A CMLFile with the contents of the given file.
254    */
255   CMLFile readCMLFile(const std::string& file_path);
256
257   /**
258    * Writes the given data into specified CMakeLists file.
259    * @param file_path Full path of the CMakeLists file.
260    * @param data CMakeLists data.
261    * @return True if the operation was successful.
262    */
263   bool writeCMLFile(const std::string& file_path, const CMLFile& data);
264
265   /**
266    * @param st Strips all space character at the beginning and at the end of the string.
267    */
268   void normalizeStr(std::string& st);
269
270 };
271
272 #endif /* CDMUTILITIES_H_ */