1 /*=========================================================================
3 Module: $RCSfile: bbtkUtilities.h,v $
5 Date: $Date: 2009/01/27 14:22:57 $
6 Version: $Revision: 1.19 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
35 * \brief struct bbtk::Utilities : various usefull methods
39 * \class bbtk::Utilities
40 * \brief various usefull methods
43 #ifndef __bbtkUtilities_h_INCLUDED__
44 #define __bbtkUtilities_h_INCLUDED__
46 #include "bbtkConfigurationFile.h"
47 #include "bbtkSystem.h"
50 #ifdef CMAKE_HAVE_TERMIOS_H
52 #define BBTK_USE_TERMIOS_BASED_PROMPT
60 #include <sys/types.h>
63 #include <cctype> // std::toupper
64 //#include "bbtkMessageManager.h"
66 #if defined(__GNUC__) // gcc 4.3
72 /// Holds various usefull methods
73 struct BBTK_EXPORT Utilities
75 // ======================================================================
76 static std::string GetExecutablePath();
78 // ======================================================================
79 // See : http://www.techbytes.ca/techbyte103.html for more O.S.
80 static bool FileExists(std::string strFilename);
82 // =====================================================================
83 static std::string ExtractPackageName(const std::string &name,
86 //=====================================================================
87 static std::string ExtractScriptName(const std::string &name,
90 // ========================================================================
91 static std::string ExpandLibName(const std::string &name, bool verbose);
93 // =======================================================================
94 static std::string MakeLibnameFromPath(std::string path, std::string pkgname);
96 // =====================================================================
97 static std::string MakePkgnameFromPath(std::string path,
98 std::string pkgname, bool addExt);
99 //========================================================================
102 // =======================================================================
103 /// Makes a valid filename with string (replaces invalid file seps
105 static inline void MakeValidFileName(std::string& name)
108 INVALID_FILE_SEPARATOR ,
109 VALID_FILE_SEPARATOR);
112 // =======================================================================
113 /// Returns the user settings dir, e.g. /home/username/.bbtk
114 static std::string GetUserSettingsDir();
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);
122 static bool IsAtRoot(std::string cwd);
123 // ======================================================================
125 static bool IsDirectory(std::string const &dirName);
127 static void CreateDirectoryIfNeeded( std::string const &dirName);
128 // =======================================================================
130 static void SplitAroundFirstDot( const std::string& in,
133 //======================================================================
134 static void SplitString ( const std::string& str,
135 const std::string& delimiters,
136 std::vector<std::string>& tokens);
137 //====================================================================
140 // ====================================================================
142 static std::string get_file_name(const std::string& s) ;
145 // ====================================================================
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
152 static int Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames);
155 //=======================================================================
156 // Replaces substrings "\\n" by a real carriage return "\n"
157 static void SubsBackslashN ( std::string& s );
158 //=======================================================================
161 //========================================================================
162 // Usefull functions for html generation
163 //========================================================================
165 static inline void replace( std::string& str,
166 const std::string& what,
167 const std::string& with )
169 std::string::size_type pos = str.find( what );
170 while ( pos != std::string::npos )
172 str.replace( pos, what.size(), with );
173 pos = str.find( what, pos+what.size()-1 );
176 //========================================================================
178 static inline void html_format(std::string& str)
180 replace( str, "&", "&" );
181 replace( str, "<", "<" );
182 replace( str, ">", ">" );
186 //========================================================================
187 // Usefull functions OPTIONS string
188 //========================================================================
191 static bool loosematch(std::string stdLine,std::string stdOptions);
201 #endif //#ifndef __bbtkUtilities_h_INCLUDED__