]> Creatis software - bbtk.git/blob - kernel/src/bbtkUtilities.h
Compil sous Windows
[bbtk.git] / kernel / src / bbtkUtilities.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkUtilities.h,v $
5   Language:  C++
6   Date:      $Date: 2008/05/06 13:45:12 $
7   Version:   $Revision: 1.15 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See doc/license.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19
20 /**
21  *  \file 
22  *  \brief struct bbtk::Utilities : various usefull methods 
23  */
24
25 /**
26  * \class bbtk::Utilities
27  * \brief various usefull methods 
28  */
29
30 #ifndef __bbtkUtilities_h_INCLUDED__
31 #define __bbtkUtilities_h_INCLUDED__
32
33 #include "bbtkConfigurationFile.h"
34 #include "bbtkSystem.h"
35 #include <string>
36 #include <sys/stat.h>
37 #ifdef CMAKE_HAVE_TERMIOS_H
38 #include <termios.h>
39 #define BBTK_USE_TERMIOS_BASED_PROMPT
40 #endif
41
42 #ifdef _MSC_VER
43    #include <windows.h> 
44    #include <direct.h>
45 #else
46    #include <dirent.h>   
47    #include <sys/types.h>
48 #endif
49
50 #include <cctype>    // std::toupper
51 //#include "bbtkMessageManager.h"
52
53 namespace bbtk
54 {
55   /// Holds various usefull methods 
56   struct BBTK_EXPORT Utilities
57   {
58     
59     // ======================================================================
60     // See : http://www.techbytes.ca/techbyte103.html for more O.S.
61     static bool FileExists(std::string strFilename);
62     
63     // =====================================================================
64     
65     static std::string ExtractPackageName(const std::string  &name, 
66                                           std::string& path);
67
68     //=====================================================================
69     static std::string ExtractScriptName(const std::string &name,
70                                          std::string& path);
71   
72     // ========================================================================
73
74     static std::string ExpandLibName(const std::string &name, bool verbose);
75    
76 // ===================================================================================
77
78   static std::string MakeLibnameFromPath(std::string path, std::string pkgname);
79  
80 // ===================================================================================
81
82   static std::string MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt);
83     //========================================================================
84     
85     static bool IsAtRoot(std::string cwd);
86     // ======================================================================
87     
88     static bool IsDirectory(std::string const &dirName);
89     // ===================================================================================
90     
91     static void SplitAroundFirstDot( const std::string& in,
92                                             std::string& left,
93                                             std::string& right);
94     //=======================================================================
95     static void SplitString ( const std::string& str, 
96                                      const std::string& delimiters, 
97                                      std::vector<std::string>& tokens);
98     //=======================================================================
99     
100     
101     // ===================================================================================
102     
103     static std::string get_file_name(const std::string& s) ;
104    
105     
106     // ===================================================================================
107     /**
108      * \brief   Explore a directory with possibility of recursion
109      *          return number of files read
110      * @param  dirpath   directory to explore
111      * @param  recursive whether we want recursion or not
112      */
113     static int Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames);
114
115
116     //=======================================================================
117     // Replaces substrings "\\n" by a real carriage return "\n"
118     static void SubsBackslashN ( std::string& s );
119     //=======================================================================
120
121
122   //========================================================================
123   // Usefull functions for html generation
124   //========================================================================
125
126     static inline void replace( std::string& str,
127                                 const std::string& what, 
128                                 const std::string& with )
129     {
130       std::string::size_type pos = str.find( what );
131           while ( pos != std::string::npos )
132       {
133         str.replace( pos, what.size(), with );
134         pos = str.find( what, pos+what.size()-1 );
135       } 
136     }
137     //========================================================================
138
139     static inline void html_format(std::string& str)
140     {
141       replace( str, "&", "&amp;" );
142       replace( str, "<", "&lt;" );
143       replace( str, ">", "&gt;" );
144     }
145     
146     
147     //========================================================================
148     // Usefull functions OPTIONS string
149     //========================================================================
150
151     
152     static bool loosematch(std::string stdLine,std::string stdOptions);
153     
154    
155     
156     
157   };
158
159   
160 } // namespace bbtk
161  
162 #endif //#ifndef __bbtkUtilities_h_INCLUDED__
163 //EOF