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