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