]> Creatis software - bbtk.git/blob - kernel/src/bbtkUtilities.h
6d0c2cfe467a12ec6cc05f76f182e0bbb62b5fa4
[bbtk.git] / kernel / src / bbtkUtilities.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkUtilities.h,v $
4   Language:  C++
5   Date:      $Date: 2009/01/27 14:22:57 $
6   Version:   $Revision: 1.19 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31
32
33 /**
34  *  \file 
35  *  \brief struct bbtk::Utilities : various usefull methods 
36  */
37
38 /**
39  * \class bbtk::Utilities
40  * \brief various usefull methods 
41  */
42
43 #ifndef __bbtkUtilities_h_INCLUDED__
44 #define __bbtkUtilities_h_INCLUDED__
45
46 #include "bbtkConfigurationFile.h"
47 #include "bbtkSystem.h"
48 #include <string>
49 #include <sys/stat.h>
50 #ifdef CMAKE_HAVE_TERMIOS_H
51 #include <termios.h>
52 #define BBTK_USE_TERMIOS_BASED_PROMPT
53 #endif
54
55 #ifdef _MSC_VER
56    #include <windows.h> 
57    #include <direct.h>
58 #else
59    #include <dirent.h>   
60    #include <sys/types.h>
61 #endif
62
63 #include <cctype>    // std::toupper
64 //#include "bbtkMessageManager.h"
65
66 #if defined(__GNUC__) // gcc 4.3
67 #include <stdlib.h>
68 #endif
69
70 namespace bbtk
71 {
72   /// Holds various usefull methods 
73   struct BBTK_EXPORT Utilities
74   {
75     //  ======================================================================
76     static std::string GetExecutablePath();
77
78     // ======================================================================
79     // See : http://www.techbytes.ca/techbyte103.html for more O.S.
80     static bool FileExists(std::string strFilename);
81     
82     // =====================================================================
83     static std::string ExtractPackageName(const std::string  &name, 
84                                           std::string& path);
85     
86     //=====================================================================
87     static std::string ExtractScriptName(const std::string &name,
88                                          std::string& path);
89     
90     // ========================================================================
91     static std::string ExpandLibName(const std::string &name, bool verbose);
92     
93     // =======================================================================
94     static std::string MakeLibnameFromPath(std::string path, std::string pkgname);
95     
96     // =====================================================================
97     static std::string MakePkgnameFromPath(std::string path, 
98                                            std::string pkgname, bool addExt);
99     //========================================================================
100     
101     
102     // =======================================================================
103     /// Makes a valid filename with string (replaces invalid file seps 
104     /// by valid ones)
105     static inline void MakeValidFileName(std::string& name)
106     {
107       replace( name, 
108                INVALID_FILE_SEPARATOR , 
109                VALID_FILE_SEPARATOR);
110     }
111
112     // =======================================================================
113     /// Returns the user settings dir, e.g. /home/username/.bbtk
114     static std::string GetUserSettingsDir();
115
116     // =======================================================================
117     /// Builds the complete path to the file 'name' located 
118     /// in user settings dir, e.g. /home/username/.bbtk/
119     static std::string MakeUserSettingsFullFileName(const std::string& name);
120     
121     
122     static bool IsAtRoot(std::string cwd);
123     // ======================================================================
124     
125     static bool IsDirectory(std::string const &dirName);
126
127     static void CreateDirectoryIfNeeded( std::string const &dirName);
128     // =======================================================================
129     
130     static void SplitAroundFirstDot( const std::string& in,
131                                      std::string& left,
132                                             std::string& right);
133     //======================================================================
134     static void SplitString ( const std::string& str, 
135                                      const std::string& delimiters, 
136                                      std::vector<std::string>& tokens);
137     //====================================================================
138     
139     
140     // ====================================================================
141     
142     static std::string get_file_name(const std::string& s) ;
143     
144     
145     // ====================================================================
146     /**
147      * \brief   Explore a directory with possibility of recursion
148      *          return number of files read
149      * @param  dirpath   directory to explore
150      * @param  recursive whether we want recursion or not
151      */
152     static int Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames);
153     
154     
155     //=======================================================================
156     // Replaces substrings "\\n" by a real carriage return "\n"
157     static void SubsBackslashN ( std::string& s );
158     //=======================================================================
159
160
161   //========================================================================
162   // Usefull functions for html generation
163   //========================================================================
164
165     static inline void replace( std::string& str,
166                                 const std::string& what, 
167                                 const std::string& with )
168     {
169       std::string::size_type pos = str.find( what );
170           while ( pos != std::string::npos )
171       {
172         str.replace( pos, what.size(), with );
173         pos = str.find( what, pos+what.size()-1 );
174       } 
175     }
176     //========================================================================
177
178     static inline void html_format(std::string& str)
179     {
180       replace( str, "&", "&amp;" );
181       replace( str, "<", "&lt;" );
182       replace( str, ">", "&gt;" );
183     }
184     
185     
186     //========================================================================
187     // Usefull functions OPTIONS string
188     //========================================================================
189
190     
191     static bool loosematch(std::string stdLine,std::string stdOptions);
192     
193    
194     
195     
196   };
197
198   
199 } // namespace bbtk
200  
201 #endif //#ifndef __bbtkUtilities_h_INCLUDED__
202 //EOF