]> Creatis software - bbtk.git/blob - kernel/src/bbtkUtilities.h
#2536 BBTK Feature New Normal wt-version Package
[bbtk.git] / kernel / src / bbtkUtilities.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   Program:   bbtk
30   Module:    $RCSfile: bbtkUtilities.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.20 $
34 =========================================================================*/
35
36
37
38
39
40 /**
41  *  \file 
42  *  \brief struct bbtk::Utilities : various usefull methods 
43  */
44
45 /**
46  * \class bbtk::Utilities
47  * \brief various usefull methods 
48  */
49
50 #ifndef __bbtkUtilities_h_INCLUDED__
51 #define __bbtkUtilities_h_INCLUDED__
52
53 #include "bbtkConfigurationFile.h"
54 #include "bbtkSystem.h"
55 #include <string>
56 #include <sys/stat.h>
57 #ifdef CMAKE_HAVE_TERMIOS_H
58 #include <termios.h>
59 #define BBTK_USE_TERMIOS_BASED_PROMPT
60 #endif
61
62 #ifdef _MSC_VER
63    #include <windows.h> 
64    #include <direct.h>
65 #else
66    #include <dirent.h>   
67    #include <sys/types.h>
68 #endif
69
70 #include <cctype>    // std::toupper
71 //#include "bbtkMessageManager.h"
72
73 #if defined(__GNUC__) // gcc 4.3
74 #include <stdlib.h>
75 #endif
76
77 namespace bbtk
78 {
79   /// Holds various usefull methods 
80   struct BBTK_EXPORT Utilities
81   {
82     //  ======================================================================
83     static std::string GetExecutablePath();
84
85     // ======================================================================
86     // See : http://www.techbytes.ca/techbyte103.html for more O.S.
87     static bool FileExists(std::string strFilename);
88     
89     // =====================================================================
90     static std::string ExtractPackageName(const std::string  &name, 
91                                           std::string& path);
92     
93     //=====================================================================
94     static std::string ExtractScriptName(const std::string &name,
95                                          std::string& path);
96     
97     // ========================================================================
98     static std::string ExpandLibName(const std::string &name, bool verbose);
99     
100     // =======================================================================
101     static std::string MakeLibnameFromPath(std::string path, std::string pkgname);
102     
103     // =====================================================================
104     static std::string MakePkgnameFromPath(std::string path, 
105                                            std::string pkgname, bool addExt);
106     //========================================================================
107     
108     
109     // =======================================================================
110     /// Makes a valid filename with string (replaces invalid file seps 
111     /// by valid ones)
112     static inline void MakeValidFileName(std::string& name)
113     {
114       replace( name, 
115                INVALID_FILE_SEPARATOR , 
116                VALID_FILE_SEPARATOR);
117     }
118
119     // =======================================================================
120     /// Returns the user settings dir, e.g. /home/username/.bbtk
121     static std::string GetUserSettingsDir();
122
123     // =======================================================================
124     /// Builds the complete path to the file 'name' located 
125     /// in user settings dir, e.g. /home/username/.bbtk/
126     static std::string MakeUserSettingsFullFileName(const std::string& name);
127     
128     
129     static bool IsAtRoot(std::string cwd);
130     // ======================================================================
131     
132     static bool IsDirectory(std::string const &dirName);
133
134     static void CreateDirectoryIfNeeded( std::string const &dirName);
135     // =======================================================================
136     
137     static void SplitAroundFirstDot( const std::string& in,
138                                      std::string& left,
139                                             std::string& right);
140     //======================================================================
141     static void SplitString ( const std::string& str, 
142                                      const std::string& delimiters, 
143                                      std::vector<std::string>& tokens);
144     //====================================================================
145     
146     
147     // ====================================================================
148     
149     static std::string get_file_name(const std::string& s) ;
150     
151     
152     // ====================================================================
153     /**
154      * \brief   Explore a directory with possibility of recursion
155      *          return number of files read
156      * @param  dirpath   directory to explore
157      * @param  recursive whether we want recursion or not
158      */
159     static int Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames);
160     
161     
162     //=======================================================================
163     // Replaces substrings "\\n" by a real carriage return "\n"
164     static void SubsBackslashN ( std::string& s );
165     //=======================================================================
166
167
168   //========================================================================
169   // Usefull functions for html generation
170   //========================================================================
171
172     static inline void replace( std::string& str,
173                                 const std::string& what, 
174                                 const std::string& with )
175     {
176       std::string::size_type pos = str.find( what );
177           while ( pos != std::string::npos )
178       {
179         str.replace( pos, what.size(), with );
180         pos = str.find( what, pos+what.size()-1 );
181       } 
182     }
183     //========================================================================
184
185     static inline void html_format(std::string& str)
186     {
187       replace( str, "&", "&amp;" );
188       replace( str, "<", "&lt;" );
189       replace( str, ">", "&gt;" );
190     }
191     
192     
193     //========================================================================
194     // Usefull functions OPTIONS string
195     //========================================================================
196
197     
198     static bool loosematch(std::string stdLine,std::string stdOptions);
199     
200
201     static std::string GetEnvHome();
202     
203     
204   };
205 class BBTK_EXPORT NodeTreeC 
206 {
207 public:
208         std::string data;
209         std::vector<NodeTreeC> childs;
210
211         NodeTreeC();
212         NodeTreeC(std::string _data);
213         ~NodeTreeC();
214         void insertChild(std::string _data);
215         void treeTour(int lvl);
216         void setData(std::string _data);
217         void deleteTree();
218 };
219   
220 } // namespace bbtk
221  
222 #endif //#ifndef __bbtkUtilities_h_INCLUDED__
223 //EOF