X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=kernel%2Fsrc%2FbbtkUtilities.h;h=8711393d233f267a2645ae01051ec1bddf8f92bb;hb=acec0f7b93efd8318c826ff6b006fde051993c5a;hp=51f85193ff0b9ec5554611e86cb3ff44d0ae9662;hpb=f90eb2c535faec3e455d3b5da9a5e1adf653de2b;p=bbtk.git diff --git a/kernel/src/bbtkUtilities.h b/kernel/src/bbtkUtilities.h index 51f8519..8711393 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/01/23 07:51:51 $ - Version: $Revision: 1.3 $ + Date: $Date: 2008/02/08 14:58:31 $ + Version: $Revision: 1.11 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See doc/license.txt or @@ -26,7 +26,7 @@ * \class bbtk::Utilities * \brief various usefull methods */ - + #ifndef __bbtkUtilities_h_INCLUDED__ #define __bbtkUtilities_h_INCLUDED__ @@ -39,11 +39,25 @@ #define BBTK_USE_TERMIOS_BASED_PROMPT #endif +#ifdef _MSC_VER + #include + #include +#else + #include + #include +#endif + +#include // std::toupper +//#include "bbtkMessageManager.h" + 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) { @@ -81,23 +95,23 @@ namespace bbtk 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 @@ -117,8 +131,8 @@ namespace bbtk // 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! @@ -131,13 +145,15 @@ namespace bbtk // =================================================================================== - static std::string ExtractScriptName(const std::string &name) + 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; } @@ -159,20 +175,28 @@ namespace bbtk 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; - - if ( name[0] == '/' || name[0] == '\\' ) + +//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] == '\\') ) + } + else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') ) { - libname = cwd + ConfigurationFile::GetInstance().Get_file_separator () + name.substr(2, name.length()); + libname = cwd + fileSeparator + name.substr(2, name.length()); return(libname); - } - else if ( name[0] == '.' && name[1] == '.' && (name[2] == '/' || name[2] == '\\') ) + } + else if ( name[0] == '.' && name[1] == '.' /* && (name[2] == '/' || name[2] == '\\') */ ) { if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows) { @@ -186,18 +210,30 @@ namespace bbtk // iterate on ../ and go up from the current working dir! std::string a(name); bool alreadyProcessRoot = false; - for(;;) + + //if (a[a.size()-1] != fileSeparator[0]) + // a.append(fileSeparator); +//std::cout << "------------------a [" << a << "]" << std::endl; + + for(;;) // wild loop ! { - std::string::size_type slash_position = cwd.find_last_of(ConfigurationFile::GetInstance().Get_file_separator ()); + std::string::size_type slash_position = cwd.find_last_of(fileSeparator); if (slash_position != std::string::npos) { if (slash_position == 0) slash_position = 1; cwd = cwd.substr(0,slash_position/*+1*/); - a = a.substr(3, name.length()); // remove ../ +//std::cout << "------------------cwd [" << cwd << "]" << std::endl; + // if (a == "..") { + // a = ""; + // break; + // } + // else + a = a.substr(3, /*name.length()*/ a.length()); // remove ../ +//std::cout << "------------------a [" << a << "]" << std::endl; if (a == "" || alreadyProcessRoot) { if (verbose) - std::cout << " File path [" << name << "] doesn't exist" << std::endl; + std::cout << " File path : [" << name << "] doesn't exist" << std::endl; tooHigh = true; break; } @@ -205,7 +241,7 @@ namespace bbtk libname = cwd; char c = cwd[cwd.size()-1]; if (c != '/' && c != '\\' ) - libname += ConfigurationFile::GetInstance().Get_file_separator (); + libname += fileSeparator; libname += a; if ( a[0] != '.' ) // if . (probabely ../), loop again @@ -216,12 +252,14 @@ namespace bbtk } } // end iterating on ../ } +//std::cout << "------------------out of loop]" << std::endl; if (tooHigh) libname=""; return (libname); } // ----- End of expanding path name ( ./ ../ ../../ ) + std::cout <<"* ERROR in ExpandLibName : should never get here!" << std::endl; // To avoid warning return(""); // Will never get here! } @@ -235,6 +273,8 @@ namespace bbtk #if defined(__GNUC__) if (c != '/') libname += "/libbb"; + else + libname += "libbb"; libname += pkgname; libname += ".so"; @@ -249,26 +289,58 @@ namespace bbtk // =================================================================================== - static inline std::string MakePkgnameFromPath(std::string path, std::string pkgname) + static inline std::string MakePkgnameFromPath(std::string path, std::string pkgname, bool addExt) { std::string libname = path; - char c = path[path.size()-1]; - if (c != '/' && c != '\\') - libname += ConfigurationFile::GetInstance().Get_file_separator (); - libname += pkgname; - libname += ".bbs"; + char c = path[path.size()-1]; + if (c != '/' && c != '\\') + { + libname += ConfigurationFile::GetInstance().Get_file_separator (); + } + libname += pkgname; + if (addExt) + { + int l = libname.size(); + if (l>4) + { + if (libname.substr(l-4, 4) != ".bbs") + { + libname = libname + ".bbs"; + } + } + } 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 ) + { +#if _WIN32 + return ((fs.st_mode & _S_IFDIR) != 0); +#else + return S_ISDIR(fs.st_mode); +#endif + } + else + { + return false; + } } // =================================================================================== @@ -287,11 +359,36 @@ namespace bbtk } 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); + + 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) { @@ -305,12 +402,119 @@ namespace bbtk 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; + +#ifdef _MSC_VER + WIN32_FIND_DATA fileData; + HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData); + for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b; + b = FindNextFile(hFile, &fileData)) + { + fileName = fileData.cFileName; + if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) + { + // Need to check for . and .. to avoid infinite loop + if ( fileName != "." && fileName != ".." && recursive ) + { + numberOfFiles += Explore(dirName+ "\\"+fileName,recursive,Filenames); + } + } + else + { + Filenames.push_back(dirName+fileName); + numberOfFiles++; + } + } + DWORD dwError = GetLastError(); + if (hFile != INVALID_HANDLE_VALUE) + FindClose(hFile); + if (dwError != ERROR_NO_MORE_FILES) + { + LPVOID lpMsgBuf; + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| + FORMAT_MESSAGE_FROM_SYSTEM| + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL,GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &lpMsgBuf,0,NULL); + + // ErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf + // <<" for the directory : "<d_name; +//std::cout <<"in Explor filename[" << fileName << "]" << std::endl; + if( stat(fileName.c_str(), &buf) != 0 ) + { + //ErrorMacro( strerror(errno) ); + } + if ( S_ISREG(buf.st_mode) ) //is it a regular file? + { + Filenames.push_back( fileName ); + numberOfFiles++; + } + else if ( S_ISDIR(buf.st_mode) ) //directory? + { + if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files + { + numberOfFiles += Explore( fileName, recursive, Filenames); + } + } + else + { + //ErrorMacro( "Unexpected error" ); + return -1; + } + } + if( closedir(dir) != 0 ) + { + // ErrorMacro( strerror(errno) ); + } +#endif + + return numberOfFiles; + +} //======================================================================== - // Usefull functions for html generation - - //======================================================================== + // Usefull functions for html generation + //======================================================================== + static inline void replace( std::string& str, const std::string& from, const std::string& to ) @@ -325,17 +529,27 @@ namespace bbtk } //======================================================================== - //======================================================================== static inline void html_format(std::string& str) { replace( str, "&", "&" ); replace( str, "<", "<" ); replace( str, ">", ">" ); } + + + //======================================================================== + // Usefull functions OPTIONS string //======================================================================== + + static bool loosematch(std::string stdLine,std::string stdOptions); + + + + }; + } // namespace bbtk #endif //#ifndef __bbtkUtilities_h_INCLUDED__