1 /*=========================================================================
4 Module: $RCSfile: bbtkUtilities.h,v $
6 Date: $Date: 2008/01/28 15:08:53 $
7 Version: $Revision: 1.5 $
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.
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.
17 =========================================================================*/
22 * \brief struct bbtk::Utilities : various usefull methods
26 * \class bbtk::Utilities
27 * \brief various usefull methods
30 #ifndef __bbtkUtilities_h_INCLUDED__
31 #define __bbtkUtilities_h_INCLUDED__
33 #include "bbtkConfigurationFile.h"
34 #include "bbtkSystem.h"
37 #ifdef CMAKE_HAVE_TERMIOS_H
39 #define BBTK_USE_TERMIOS_BASED_PROMPT
42 //#include "bbtkMessageManager.h"
46 /// Holds various usefull methods
47 struct BBTK_EXPORT Utilities
51 // ===================================================================================
53 // See : http://www.techbytes.ca/techbyte103.html for more O.S.
54 static inline bool FileExists(std::string strFilename)
56 struct stat stFileInfo;
60 // Attempt to get the file attributes
61 intStat = stat(strFilename.c_str(),&stFileInfo);
63 // We were able to get the file attributes
64 // so the file obviously exists.
67 // We were not able to get the file attributes.
68 // This may mean that we don't have permission to
69 // access the folder which contains this file. If you
70 // need to do that level of checking, lookup the
71 // return values of stat which will give you
72 // more details on why stat failed.
80 // ===================================================================================
82 static std::string ExtractPackageName(const std::string &name,
88 std::string::size_type slash_position = name.find_last_of("/\\");
89 if (slash_position != std::string::npos)
91 pkgname = name.substr(slash_position+1,std::string::npos);
92 path = name.substr(0,slash_position);
93 // std::cout << "F:P='"<<path<<"'"<<std::endl;//+1,std::string::npos);
100 // remove {.so | dll} if any
101 std::string::size_type dot_position = pkgname.find_last_of('.');
102 if (dot_position != std::string::npos){
103 pkgname = pkgname.substr(0,dot_position);
105 #if defined(__GNUC__)
108 // shared lib name = libbb<name>.so
110 // remove {libbb} if any
111 if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) {
112 pkgname = pkgname.substr(5, pkgname.length());
115 /// \ \todo what would happen if (stupid) user names his package 'libbb' ?!?
116 /// \ --> Should be forbidden!
118 #elif defined(_WIN32)
121 // shared lib name = <name>.dll
123 // remove {bb} if any
124 if (memcmp (pkgname.c_str(), "bb", 2) == 0) {
125 pkgname = pkgname.substr(2, pkgname.length());
129 /// \ \todo what would happen if (stupid) user names his package 'bb' ?!?
130 /// \ --> Should be forbidden!
133 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
138 // ===================================================================================
140 static std::string ExtractScriptName(const std::string &name)
144 std::string::size_type slash_position = name.find_last_of("/\\");
145 if (slash_position != std::string::npos) {
146 pkgname =name.substr(slash_position+1,std::string::npos);
150 // remove {.bbs } if any
151 std::string::size_type dot_position = pkgname.find_last_of('.');
152 if (dot_position != std::string::npos){
153 pkgname = pkgname.substr(0,dot_position);
158 // ===================================================================================
160 static std::string ExpandLibName(const std::string &name, bool verbose)
162 // ----- Think of expanding path name ( ./ ../ ../../ )
164 char buf[2048]; // for getcwd
165 char * currentDir = getcwd(buf, 2048);
166 std::string cwd(currentDir);
167 std::string libname(name);
168 std::string fileSeparator;
169 fileSeparator = ConfigurationFile::GetInstance().Get_file_separator();
170 // tooHigh : true is user supplies a library pathname with too many "../"
171 bool tooHigh = false;
173 //std::cout << "------------------cwd [" << cwd << "]" << std::endl;
175 if ( name[0] == '/' || name[0] == '\\' )
179 else if ( name =="." )
181 libname = cwd + fileSeparator;
184 else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') )
186 libname = cwd + fileSeparator + name.substr(2, name.length());
189 else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ )
191 if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows)
193 // if we are already at / or c: --> hopeless
195 std::cout << " File path [" << name << "] doesn't exist" << std::endl;
200 // iterate on ../ and go up from the current working dir!
202 bool alreadyProcessRoot = false;
204 //if (a[a.size()-1] != fileSeparator[0])
205 // a.append(fileSeparator);
206 //std::cout << "------------------a [" << a << "]" << std::endl;
208 for(;;) // wild loop !
210 std::string::size_type slash_position = cwd.find_last_of(fileSeparator);
211 if (slash_position != std::string::npos) {
212 if (slash_position == 0)
214 cwd = cwd.substr(0,slash_position/*+1*/);
215 //std::cout << "------------------cwd [" << cwd << "]" << std::endl;
221 a = a.substr(3, /*name.length()*/ a.length()); // remove ../
222 //std::cout << "------------------a [" << a << "]" << std::endl;
223 if (a == "" || alreadyProcessRoot)
226 std::cout << " File path : [" << name << "] doesn't exist" << std::endl;
230 // std::string b = cwd + a;
232 char c = cwd[cwd.size()-1];
233 if (c != '/' && c != '\\' )
234 libname += fileSeparator;
237 if ( a[0] != '.' ) // if . (probabely ../), loop again
241 alreadyProcessRoot = true;
243 } // end iterating on ../
245 //std::cout << "------------------out of loop]" << std::endl;
250 } // ----- End of expanding path name ( ./ ../ ../../ )
252 std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl;
254 return(""); // Will never get here!
257 // ===================================================================================
259 static std::string MakeLibnameFromPath(std::string path, std::string pkgname)
261 std::string libname = path;
262 char c = path[path.size()-1];
263 #if defined(__GNUC__)
271 #elif defined(_WIN32)
273 libname = path+"\\bb";
280 // ===================================================================================
282 static inline std::string MakePkgnameFromPath(std::string path, std::string pkgname)
284 std::string libname = path;
285 char c = path[path.size()-1];
286 if (c != '/' && c != '\\')
287 libname += ConfigurationFile::GetInstance().Get_file_separator ();
289 int l = libname.size();
292 if (libname.substr(l-4, 4) != ".bbs")
294 libname = libname + ".bbs";
300 // ===================================================================================
302 static inline bool IsAtRoot(std::string cwd)
304 if ( cwd == "/" // hope it gets / (for Linux)
305 || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows)
312 // ===================================================================================
314 static inline void SplitAroundFirstDot( const std::string& in,
318 std::string delimiter = ".";
319 std::string::size_type pos = in.find_first_of(delimiter);
320 if (std::string::npos != pos)
322 left = in.substr(0,pos);
323 right = in.substr(pos+1,in.size());
328 // bbtkError(in<<" : expected 'a.b' format but no dot found");
334 // ===================================================================================
336 static inline std::string get_file_name(const std::string& s)
338 std::string::size_type slash_position = s.find_last_of("/\\");
339 if (slash_position != std::string::npos)
341 return s.substr(slash_position+1,std::string::npos);
350 //========================================================================
351 // Usefull functions for html generation
352 //========================================================================
354 static inline void replace( std::string& str,
355 const std::string& from,
356 const std::string& to )
359 string::size_type pos = str.find( from );
360 while ( pos != string::npos )
362 str.replace( pos, from.size(), to );
363 pos = str.find( from, pos+from.size()-1 );
366 //========================================================================
368 static inline void html_format(std::string& str)
370 replace( str, "&", "&" );
371 replace( str, "<", "<" );
372 replace( str, ">", ">" );
374 //========================================================================
380 #endif //#ifndef __bbtkUtilities_h_INCLUDED__