]> Creatis software - bbtk.git/blob - kernel/src/bbtkUtilities.h
34d17eb508b1653aae33845c36039d241078f4f7
[bbtk.git] / kernel / src / bbtkUtilities.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkUtilities.h,v $
4   Language:  C++
5   Date:      $Date: 2008/10/22 09:16:16 $
6   Version:   $Revision: 1.18 $
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     // ======================================================================
77     // See : http://www.techbytes.ca/techbyte103.html for more O.S.
78     static bool FileExists(std::string strFilename);
79     
80     // =====================================================================
81     
82     static std::string ExtractPackageName(const std::string  &name, 
83                                           std::string& path);
84
85     //=====================================================================
86     static std::string ExtractScriptName(const std::string &name,
87                                          std::string& path);
88   
89     // ========================================================================
90
91     static std::string ExpandLibName(const std::string &name, bool verbose);
92    
93 // ===================================================================================
94
95   static std::string MakeLibnameFromPath(std::string path, std::string pkgname);
96  
97 // ===================================================================================
98
99   static std::string MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt);
100     //========================================================================
101     
102     // =======================================================================
103     /// Builds the complete path to the file 'name' located 
104   /// in user settings dir, e.g. /home/username/.bbtk/
105     static std::string MakeUserSettingsFullFileName(const std::string& name);
106
107
108     static bool IsAtRoot(std::string cwd);
109     // ======================================================================
110     
111     static bool IsDirectory(std::string const &dirName);
112     // ===================================================================================
113     
114     static void SplitAroundFirstDot( const std::string& in,
115                                             std::string& left,
116                                             std::string& right);
117     //=======================================================================
118     static void SplitString ( const std::string& str, 
119                                      const std::string& delimiters, 
120                                      std::vector<std::string>& tokens);
121     //=======================================================================
122     
123     
124     // ===================================================================================
125     
126     static std::string get_file_name(const std::string& s) ;
127    
128     
129     // ===================================================================================
130     /**
131      * \brief   Explore a directory with possibility of recursion
132      *          return number of files read
133      * @param  dirpath   directory to explore
134      * @param  recursive whether we want recursion or not
135      */
136     static int Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames);
137
138
139     //=======================================================================
140     // Replaces substrings "\\n" by a real carriage return "\n"
141     static void SubsBackslashN ( std::string& s );
142     //=======================================================================
143
144
145   //========================================================================
146   // Usefull functions for html generation
147   //========================================================================
148
149     static inline void replace( std::string& str,
150                                 const std::string& what, 
151                                 const std::string& with )
152     {
153       std::string::size_type pos = str.find( what );
154           while ( pos != std::string::npos )
155       {
156         str.replace( pos, what.size(), with );
157         pos = str.find( what, pos+what.size()-1 );
158       } 
159     }
160     //========================================================================
161
162     static inline void html_format(std::string& str)
163     {
164       replace( str, "&", "&amp;" );
165       replace( str, "<", "&lt;" );
166       replace( str, ">", "&gt;" );
167     }
168     
169     
170     //========================================================================
171     // Usefull functions OPTIONS string
172     //========================================================================
173
174     
175     static bool loosematch(std::string stdLine,std::string stdOptions);
176     
177    
178     
179     
180   };
181
182   
183 } // namespace bbtk
184  
185 #endif //#ifndef __bbtkUtilities_h_INCLUDED__
186 //EOF