1 /*=========================================================================
4 Module: $RCSfile: bbtkUtilities.h,v $
6 Date: $Date: 2008/03/10 06:24:13 $
7 Version: $Revision: 1.13 $
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
47 #include <sys/types.h>
50 #include <cctype> // std::toupper
51 //#include "bbtkMessageManager.h"
55 /// Holds various usefull methods
56 struct BBTK_EXPORT Utilities
59 // ===================================================================================
61 // See : http://www.techbytes.ca/techbyte103.html for more O.S.
62 static inline bool FileExists(std::string strFilename)
64 struct stat stFileInfo;
68 // Attempt to get the file attributes
69 intStat = stat(strFilename.c_str(),&stFileInfo);
71 // We were able to get the file attributes
72 // so the file obviously exists.
75 // We were not able to get the file attributes.
76 // This may mean that we don't have permission to
77 // access the folder which contains this file. If you
78 // need to do that level of checking, lookup the
79 // return values of stat which will give you
80 // more details on why stat failed.
88 // ===================================================================================
90 static std::string ExtractPackageName(const std::string &name,
96 std::string::size_type slash_position = name.find_last_of("/\\");
97 if (slash_position != std::string::npos)
99 pkgname = name.substr(slash_position+1,std::string::npos);
100 path = name.substr(0,slash_position);
101 // std::cout << "F:P='"<<path<<"'"<<std::endl;//+1,std::string::npos);
108 // remove {.so | dll} if any
109 std::string::size_type dot_position = pkgname.find_last_of('.');
110 if (dot_position != std::string::npos){
111 pkgname = pkgname.substr(0,dot_position);
113 #if defined(__GNUC__)
116 // shared lib name = libbb<name>.so
118 // remove {libbb} if any
119 if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) {
120 pkgname = pkgname.substr(5, pkgname.length());
123 /// \ \todo what would happen if (stupid) user names his package 'libbb' ?!?
124 /// \ --> Should be forbidden!
126 #elif defined(_WIN32)
129 // shared lib name = <name>.dll
131 // EED Problem loading package call bbtkTools
132 // // remove {bb} if any
133 if (memcmp (pkgname.c_str(), "bb", 2) == 0) {
134 pkgname = pkgname.substr(2, pkgname.length());
138 /// \ \todo what would happen if (stupid) user names his package 'bb' ?!?
139 /// \ --> Should be forbidden!
142 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
147 // ===================================================================================
149 static std::string ExtractScriptName(const std::string &name,
154 std::string::size_type slash_position = name.find_last_of("/\\");
155 if (slash_position != std::string::npos) {
156 pkgname =name.substr(slash_position+1,std::string::npos);
157 path = name.substr(0,slash_position);
161 // remove {.bbs } if any
162 std::string::size_type dot_position = pkgname.find_last_of('.');
163 if (dot_position != std::string::npos){
164 pkgname = pkgname.substr(0,dot_position);
169 // ===================================================================================
171 static std::string ExpandLibName(const std::string &name, bool verbose)
173 // ----- Think of expanding path name ( ./ ../ ../../ )
175 char buf[2048]; // for getcwd
176 char * currentDir = getcwd(buf, 2048);
177 std::string cwd(currentDir);
178 std::string libname(name);
179 std::string fileSeparator;
180 fileSeparator = ConfigurationFile::GetInstance().Get_file_separator();
181 // tooHigh : true is user supplies a library pathname with too many "../"
182 bool tooHigh = false;
184 //std::cout << "------------------cwd [" << cwd << "] name [" << name << "]" << std::endl;
186 if ( name[0] == '/' || name[1] == ':' ) // Linux or Windows absolute name
190 else if ( name =="." )
192 libname = cwd + fileSeparator;
195 else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') )
197 libname = cwd + fileSeparator + name.substr(2, name.length());
200 else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ )
202 if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows)
204 // if we are already at / or c: --> hopeless
206 std::cout << " File path [" << name << "] doesn't exist" << std::endl;
211 // iterate on ../ and go up from the current working dir!
213 bool alreadyProcessRoot = false;
215 //if (a[a.size()-1] != fileSeparator[0])
216 // a.append(fileSeparator);
217 //std::cout << "------------------a [" << a << "]" << std::endl;
219 for(;;) // wild loop !
221 std::string::size_type slash_position = cwd.find_last_of(fileSeparator);
222 if (slash_position != std::string::npos) {
223 if (slash_position == 0)
225 cwd = cwd.substr(0,slash_position/*+1*/);
226 //std::cout << "------------------cwd [" << cwd << "]" << std::endl;
232 a = a.substr(3, /*name.length()*/ a.length()); // remove ../
233 //std::cout << "------------------a [" << a << "]" << std::endl;
234 if (a == "" || alreadyProcessRoot)
237 std::cout << " File path : [" << name << "] doesn't exist" << std::endl;
241 // std::string b = cwd + a;
243 char c = cwd[cwd.size()-1];
244 if (c != '/' && c != '\\' )
245 libname += fileSeparator;
248 if ( a[0] != '.' ) // if . (probabely ../), loop again
252 alreadyProcessRoot = true;
254 } // end iterating on ../
256 //std::cout << "------------------out of loop]" << std::endl;
261 } // ----- End of expanding path name ( ./ ../ ../../ )
263 std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl;
265 return(""); // Will never get here!
268 // ===================================================================================
270 static std::string MakeLibnameFromPath(std::string path, std::string pkgname)
272 std::string libname = path;
273 char c = path[path.size()-1];
274 #if defined(__GNUC__)
282 #elif defined(_WIN32)
284 libname = path+"\\bb";
291 // ===================================================================================
293 static inline std::string MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt)
295 std::string libname = path;
296 char c = path[path.size()-1];
297 if (c != '/' && c != '\\')
299 libname += ConfigurationFile::GetInstance().Get_file_separator ();
304 int l = libname.size();
307 if (libname.substr(l-4, 4) != ".bbs")
309 libname = libname + ".bbs";
316 // ===================================================================================
318 static inline bool IsAtRoot(std::string cwd)
320 if ( cwd == "/" // hope it gets / (for Linux)
321 || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows)
327 // ===================================================================================
329 static bool IsDirectory(std::string const &dirName)
333 if ( stat(dirName.c_str(), &fs) == 0 )
336 return ((fs.st_mode & _S_IFDIR) != 0);
338 return S_ISDIR(fs.st_mode);
347 // ===================================================================================
349 static inline void SplitAroundFirstDot( const std::string& in,
353 std::string delimiter = ".";
354 std::string::size_type pos = in.find_first_of(delimiter);
355 if (std::string::npos != pos)
357 left = in.substr(0,pos);
358 right = in.substr(pos+1,in.size());
363 // bbtkError(in<<" : expected 'a.b' format but no dot found");
368 //=======================================================================
369 static inline void SplitString ( const std::string& str,
370 const std::string& delimiters,
371 std::vector<std::string>& tokens)
373 // Skip delimiters at beginning.
374 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
375 // Find first delimiter.
376 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
378 while (std::string::npos != pos || std::string::npos != lastPos)
380 // Found a token, add it to the vector.
381 tokens.push_back(str.substr(lastPos, pos - lastPos));
382 // Skip delimiters. Note the "not_of"
383 lastPos = str.find_first_not_of(delimiters, pos);
384 // Find next delimiter
385 pos = str.find_first_of(delimiters, lastPos);
389 //=======================================================================
392 // ===================================================================================
394 static inline std::string get_file_name(const std::string& s)
396 std::string::size_type slash_position = s.find_last_of("/\\");
397 if (slash_position != std::string::npos)
399 return s.substr(slash_position+1,std::string::npos);
407 // ===================================================================================
409 * \brief Explore a directory with possibility of recursion
410 * return number of files read
411 * @param dirpath directory to explore
412 * @param recursive whether we want recursion or not
414 static int Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames)
416 int numberOfFiles = 0;
417 std::string fileName;
419 std::string dirName = dirpath;
422 WIN32_FIND_DATA fileData;
423 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
425 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
426 b = FindNextFile(hFile, &fileData))
428 fileName = fileData.cFileName;
429 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
431 // Need to check for . and .. to avoid infinite loop
432 if ( fileName != "." && fileName != ".." && recursive )
434 numberOfFiles += Explore(dirName+ "\\"+fileName,recursive,Filenames);
439 Filenames.push_back(dirName+fileName);
443 DWORD dwError = GetLastError();
444 if (hFile != INVALID_HANDLE_VALUE)
446 if (dwError != ERROR_NO_MORE_FILES)
449 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
450 FORMAT_MESSAGE_FROM_SYSTEM|
451 FORMAT_MESSAGE_IGNORE_INSERTS,
453 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
454 (LPTSTR) &lpMsgBuf,0,NULL);
456 // ErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
457 // <<" for the directory : "<<dirName);
463 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
464 // work on debian for example
465 //std::cout <<"in Explor dirname[" << dirName << "]" << std::endl;
466 DIR* dir = opendir(dirName.c_str());
471 //std::cout <<"Open OK" << std::endl;
472 // According to POSIX, the dirent structure contains a field char d_name[]
473 // of unspecified size, with at most NAME_MAX characters preceeding the
474 // terminating null character. Use of other fields will harm the porta-
475 // bility of your programs.
479 for (d = readdir(dir); d; d = readdir(dir))
481 fileName = dirName + "/" + d->d_name;
482 //std::cout <<"in Explor filename[" << fileName << "]" << std::endl;
483 if( stat(fileName.c_str(), &buf) != 0 )
485 //ErrorMacro( strerror(errno) );
487 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
489 Filenames.push_back( fileName );
492 else if ( S_ISDIR(buf.st_mode) ) //directory?
494 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
496 numberOfFiles += Explore( fileName, recursive, Filenames);
501 //ErrorMacro( "Unexpected error" );
505 if( closedir(dir) != 0 )
507 // ErrorMacro( strerror(errno) );
511 return numberOfFiles;
515 //========================================================================
516 // Usefull functions for html generation
517 //========================================================================
519 static inline void replace( std::string& str,
520 const std::string& what,
521 const std::string& with )
523 std::string::size_type pos = str.find( what );
524 while ( pos != std::string::npos )
526 str.replace( pos, what.size(), with );
527 pos = str.find( what, pos+what.size()-1 );
530 //========================================================================
532 static inline void html_format(std::string& str)
534 replace( str, "&", "&" );
535 replace( str, "<", "<" );
536 replace( str, ">", ">" );
540 //========================================================================
541 // Usefull functions OPTIONS string
542 //========================================================================
545 static bool loosematch(std::string stdLine,std::string stdOptions);
555 #endif //#ifndef __bbtkUtilities_h_INCLUDED__