X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=kernel%2Fsrc%2FbbtkUtilities.h;h=087f2208b09995fce326e5ec706413d72c5c5e49;hb=2b8d8f686974ab38c9ffcf0f88dd533b01b68e84;hp=47779dea87130710e957480a760dfcb75ffecafd;hpb=8c0188cb0a3b4c491a4c5498ee04c754ec0fac2b;p=bbtk.git diff --git a/kernel/src/bbtkUtilities.h b/kernel/src/bbtkUtilities.h index 47779de..087f220 100644 --- a/kernel/src/bbtkUtilities.h +++ b/kernel/src/bbtkUtilities.h @@ -3,8 +3,8 @@ Program: bbtk Module: $RCSfile: bbtkUtilities.h,v $ Language: C++ - Date: $Date: 2008/02/04 13:02:58 $ - Version: $Revision: 1.10 $ + Date: $Date: 2008/03/26 08:27:19 $ + Version: $Revision: 1.14 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See doc/license.txt or @@ -47,6 +47,7 @@ #include #endif +#include // std::toupper //#include "bbtkMessageManager.h" namespace bbtk @@ -54,150 +55,152 @@ namespace bbtk /// Holds various usefull methods struct BBTK_EXPORT Utilities { - -// =================================================================================== - - // See : http://www.techbytes.ca/techbyte103.html for more O.S. - static inline bool FileExists(std::string strFilename) - { - struct stat stFileInfo; + + // ====================================================================== + // See : http://www.techbytes.ca/techbyte103.html for more O.S. + static inline bool FileExists(std::string strFilename) + { + struct stat stFileInfo; bool blnReturn; int intStat; - + // Attempt to get the file attributes intStat = stat(strFilename.c_str(),&stFileInfo); - if(intStat == 0) { - // We were able to get the file attributes - // so the file obviously exists. - blnReturn = true; - } else { - // We were not able to get the file attributes. - // This may mean that we don't have permission to - // access the folder which contains this file. If you - // need to do that level of checking, lookup the - // return values of stat which will give you - // more details on why stat failed. - blnReturn = false; - } - + if(intStat == 0) + { + // We were able to get the file attributes + // so the file obviously exists. + blnReturn = true; + } + else + { + // We were not able to get the file attributes. + // This may mean that we don't have permission to + // access the folder which contains this file. If you + // need to do that level of checking, lookup the + // return values of stat which will give you + // more details on why stat failed. + blnReturn = false; + } + return(blnReturn); - } - - -// =================================================================================== - - static std::string ExtractPackageName(const std::string &name, - std::string& path) - { - std::string pkgname; - path = ""; - - std::string::size_type slash_position = name.find_last_of("/\\"); - if (slash_position != std::string::npos) - { - pkgname = name.substr(slash_position+1,std::string::npos); - path = name.substr(0,slash_position); - // std::cout << "F:P='"<.so - + + // GCC mechanism + // shared lib name = libbb.so + // remove {libbb} if any - if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) { - pkgname = pkgname.substr(5, pkgname.length()); - } + if (memcmp ( pkgname.c_str(), "libbb", 5) == 0) { + pkgname = pkgname.substr(5, pkgname.length()); + } /* - /// \ \todo what would happen if (stupid) user names his package 'libbb' ?!? + /// \ \todo what would happen if (stupid) user names his package 'libbb' ?!? /// \ --> Should be forbidden! */ #elif defined(_WIN32) - - // WIN 32 mechanism - // shared lib name = .dll - - // remove {bb} if any - if (memcmp (pkgname.c_str(), "bb", 2) == 0) { - pkgname = pkgname.substr(2, pkgname.length()); - } - - /* + + // WIN 32 mechanism + // shared lib name = .dll + + // EED Problem loading package call bbtkTools + // // remove {bb} if any + if (memcmp (pkgname.c_str(), "bb", 2) == 0) { + pkgname = pkgname.substr(2, pkgname.length()); + } + + /* /// \ \todo what would happen if (stupid) user names his package 'bb' ?!? /// \ --> Should be forbidden! */ #else - bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?"); + bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?"); #endif - return pkgname; - } - -// =================================================================================== - - static std::string ExtractScriptName(const std::string &name, - std::string& path) - { - std::string pkgname; - - std::string::size_type slash_position = name.find_last_of("/\\"); - if (slash_position != std::string::npos) { - pkgname =name.substr(slash_position+1,std::string::npos); - path = name.substr(0,slash_position); - } else { - pkgname = name; + return pkgname; } - // remove {.bbs } if any - std::string::size_type dot_position = pkgname.find_last_of('.'); - if (dot_position != std::string::npos){ - pkgname = pkgname.substr(0,dot_position); - } - return pkgname; - } - -// =================================================================================== - - static std::string ExpandLibName(const std::string &name, bool verbose) - { - // ----- Think of expanding path name ( ./ ../ ../../ ) - - char buf[2048]; // for getcwd - char * currentDir = getcwd(buf, 2048); - std::string cwd(currentDir); - std::string libname(name); - std::string fileSeparator; - fileSeparator = ConfigurationFile::GetInstance().Get_file_separator(); - // tooHigh : true is user supplies a library pathname with too many "../" - bool tooHigh = false; -//std::cout << "------------------cwd [" << cwd << "] name [" << name << "]" << std::endl; - - if ( name[0] == '/' || name[1] == ':' ) // Linux or Windows absolute name - { - return(libname); - } - else if ( name =="." ) + //===================================================================== + static std::string ExtractScriptName(const std::string &name, + std::string& path) { - libname = cwd + fileSeparator; - return(libname); - } - else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') ) - { - libname = cwd + fileSeparator + name.substr(2, name.length()); - return(libname); + std::string pkgname; + + std::string::size_type slash_position = name.find_last_of("/\\"); + if (slash_position != std::string::npos) { + pkgname =name.substr(slash_position+1,std::string::npos); + path = name.substr(0,slash_position); + } else { + pkgname = name; + } + // remove {.bbs } if any + std::string::size_type dot_position = pkgname.find_last_of('.'); + if (dot_position != std::string::npos){ + pkgname = pkgname.substr(0,dot_position); + } + return pkgname; } - else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ ) + + // ======================================================================== + + static std::string ExpandLibName(const std::string &name, bool verbose) { - if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows) + // ----- Think of expanding path name ( ./ ../ ../../ ) + + char buf[2048]; // for getcwd + char * currentDir = getcwd(buf, 2048); + std::string cwd(currentDir); + std::string libname(name); + std::string fileSeparator; + fileSeparator = ConfigurationFile::GetInstance().Get_file_separator(); + // tooHigh : true is user supplies a library pathname with too many "../" + bool tooHigh = false; + + //std::cout << "------------------cwd [" << cwd << "] name [" << name << "]" << std::endl; + + if ( name[0] == '/' || name[1] == ':' ) // Linux or Windows absolute name + { + return(libname); + } + else if ( name =="." ) + { + libname = cwd + fileSeparator; + return(libname); + } + else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') ) + { + libname = cwd + fileSeparator + name.substr(2, name.length()); + return(libname); + } + else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ ) + { + if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows) { // if we are already at / or c: --> hopeless if (verbose) @@ -311,113 +314,113 @@ namespace bbtk return libname; } -// =================================================================================== - - static inline bool IsAtRoot(std::string cwd) - { - if ( cwd == "/" // hope it gets / (for Linux) - || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows) - return (true); - else - return(false); - } - -// =================================================================================== - -static bool IsDirectory(std::string const &dirName) -{ - struct stat fs; - - if ( stat(dirName.c_str(), &fs) == 0 ) - { + //======================================================================== + + static inline bool IsAtRoot(std::string cwd) + { + if ( cwd == "/" // hope it gets / (for Linux) + || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows) + return (true); + else + return(false); + } + + // ====================================================================== + + static bool IsDirectory(std::string const &dirName) + { + struct stat fs; + + if ( stat(dirName.c_str(), &fs) == 0 ) + { #if _WIN32 - return ((fs.st_mode & _S_IFDIR) != 0); + return ((fs.st_mode & _S_IFDIR) != 0); #else - return S_ISDIR(fs.st_mode); + return S_ISDIR(fs.st_mode); #endif - } - else - { - return false; - } -} - -// =================================================================================== - + } + else + { + return false; + } + } + + // =================================================================================== + static inline void SplitAroundFirstDot( const std::string& in, std::string& left, std::string& right) - { + { std::string delimiter = "."; std::string::size_type pos = in.find_first_of(delimiter); if (std::string::npos != pos) - { - left = in.substr(0,pos); - right = in.substr(pos+1,in.size()); - - } + { + left = in.substr(0,pos); + right = in.substr(pos+1,in.size()); + + } else - { - // bbtkError(in<<" : expected 'a.b' format but no dot found"); - left =""; - right = ""; - } + { + // bbtkError(in<<" : expected 'a.b' format but no dot found"); + left =""; + right = ""; + } } - //======================================================================= - static inline void SplitString ( const std::string& str, - const std::string& delimiters, - std::vector& tokens) - { - // Skip delimiters at beginning. - std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); - // Find first delimiter. - std::string::size_type pos = str.find_first_of(delimiters, lastPos); + //======================================================================= + static inline void SplitString ( const std::string& str, + const std::string& delimiters, + std::vector& tokens) + { + // Skip delimiters at beginning. + std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); + // Find first delimiter. + std::string::size_type pos = str.find_first_of(delimiters, lastPos); + + while (std::string::npos != pos || std::string::npos != lastPos) + { + // Found a token, add it to the vector. + tokens.push_back(str.substr(lastPos, pos - lastPos)); + // Skip delimiters. Note the "not_of" + lastPos = str.find_first_not_of(delimiters, pos); + // Find next delimiter + pos = str.find_first_of(delimiters, lastPos); + } + + } + //======================================================================= - while (std::string::npos != pos || std::string::npos != lastPos) - { - // Found a token, add it to the vector. - tokens.push_back(str.substr(lastPos, pos - lastPos)); - // Skip delimiters. Note the "not_of" - lastPos = str.find_first_not_of(delimiters, pos); - // Find next delimiter - pos = str.find_first_of(delimiters, lastPos); - } - } - //======================================================================= - - -// =================================================================================== - + // =================================================================================== + static inline std::string get_file_name(const std::string& s) { std::string::size_type slash_position = s.find_last_of("/\\"); if (slash_position != std::string::npos) - { - return s.substr(slash_position+1,std::string::npos); - } + { + return s.substr(slash_position+1,std::string::npos); + } else - { - return s; - } + { + return s; + } } -// =================================================================================== -/** - * \brief Explore a directory with possibility of recursion - * return number of files read - * @param dirpath directory to explore - * @param recursive whether we want recursion or not - */ -static int Explore(std::string const &dirpath, bool recursive, std::vector &Filenames) -{ - int numberOfFiles = 0; - std::string fileName; - - std::string dirName = dirpath; - + // =================================================================================== + /** + * \brief Explore a directory with possibility of recursion + * return number of files read + * @param dirpath directory to explore + * @param recursive whether we want recursion or not + */ + static int Explore(std::string const &dirpath, bool recursive, std::vector &Filenames) + { + int numberOfFiles = 0; + std::string fileName; + + std::string dirName = dirpath; + #ifdef _MSC_VER - WIN32_FIND_DATA fileData; + WIN32_FIND_DATA fileData; HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData); for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b; @@ -510,20 +513,37 @@ static int Explore(std::string const &dirpath, bool recursive, std::vector", ">" ); } + + + //======================================================================== + // Usefull functions OPTIONS string //======================================================================== + + static bool loosematch(std::string stdLine,std::string stdOptions); + + + + }; + } // namespace bbtk #endif //#ifndef __bbtkUtilities_h_INCLUDED__