X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=kernel%2Fsrc%2FbbtkUtilities.h;h=90bb34099c6ab2e10788795647f0626122cdb6ce;hb=87efce51877a540d943b1aa26307994b38bba55b;hp=d9c3193a0a07eac9f7c8cdcefb62fed751a76e84;hpb=6990a8aeeaf98fc901ed193b95365b2eee2fb950;p=bbtk.git diff --git a/kernel/src/bbtkUtilities.h b/kernel/src/bbtkUtilities.h index d9c3193..90bb340 100644 --- a/kernel/src/bbtkUtilities.h +++ b/kernel/src/bbtkUtilities.h @@ -1,22 +1,42 @@ +/* + # --------------------------------------------------------------------- + # + # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image + # pour la SantÈ) + # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton + # Previous Authors : Laurent Guigues, Jean-Pierre Roux + # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil + # + # This software is governed by the CeCILL-B license under French law and + # abiding by the rules of distribution of free software. You can use, + # modify and/ or redistribute the software under the terms of the CeCILL-B + # license as circulated by CEA, CNRS and INRIA at the following URL + # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html + # or in the file LICENSE.txt. + # + # As a counterpart to the access to the source code and rights to copy, + # modify and redistribute granted by the license, users are provided only + # with a limited warranty and the software's author, the holder of the + # economic rights, and the successive licensors have only limited + # liability. + # + # The fact that you are presently reading this means that you have had + # knowledge of the CeCILL-B license and that you accept its terms. + # ------------------------------------------------------------------------ */ + + /*========================================================================= - Program: bbtk Module: $RCSfile: bbtkUtilities.h,v $ Language: C++ - Date: $Date: 2008/01/22 16:55:04 $ - Version: $Revision: 1.2 $ - - Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de - l'Image). All rights reserved. See doc/license.txt or - http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notices for more information. - + Date: $Date: 2012/11/16 08:49:01 $ + Version: $Revision: 1.20 $ =========================================================================*/ + + + /** * \file * \brief struct bbtk::Utilities : various usefull methods @@ -26,7 +46,7 @@ * \class bbtk::Utilities * \brief various usefull methods */ - + #ifndef __bbtkUtilities_h_INCLUDED__ #define __bbtkUtilities_h_INCLUDED__ @@ -39,305 +59,164 @@ #define BBTK_USE_TERMIOS_BASED_PROMPT #endif +#ifdef _MSC_VER + #include + #include +#else + #include + #include +#endif + +#include // std::toupper +//#include "bbtkMessageManager.h" + +#if defined(__GNUC__) // gcc 4.3 +#include +#endif + 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; - bool blnReturn; - int intStat; + // ====================================================================== + static std::string GetExecutablePath(); - // 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; - } - - 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 - - // remove {libbb} if any - 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' ?!? - /// \ --> 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()); - } - - /* - /// \ \todo what would happen if (stupid) user names his package 'bb' ?!? - /// \ --> Should be forbidden! - */ -#else - bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?"); -#endif - return pkgname; - } - -// =================================================================================== - - static std::string ExtractScriptName(const std::string &name) - { - 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); - } 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; - } - -// =================================================================================== - - 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); - - // tooHigh : true is user supplies a library pathname with too many "../" - bool tooHigh = false; - - if ( name[0] == '/' || name[0] == '\\' ) - { - return(libname); - } - else if (name[0] == '.' && (name[1] == '/' || name[1] == '\\') ) - { - libname = cwd + ConfigurationFile::GetInstance().Get_file_separator () + name.substr(2, name.length()); - return(libname); - } - else if ( name[0] == '.' && name[1] == '.' && (name[2] == '/' || name[2] == '\\') ) + // ===================================================================== + static std::string ExtractPackageName(const std::string &name, + std::string& path); + + //===================================================================== + static std::string ExtractScriptName(const std::string &name, + std::string& path); + + // ======================================================================== + static std::string ExpandLibName(const std::string &name, bool verbose); + + // ======================================================================= + static std::string MakeLibnameFromPath(std::string path, std::string pkgname); + + // ===================================================================== + static std::string MakePkgnameFromPath(std::string path, + std::string pkgname, bool addExt); + //======================================================================== + + + // ======================================================================= + /// Makes a valid filename with string (replaces invalid file seps + /// by valid ones) + static inline void MakeValidFileName(std::string& name) { - if ( IsAtRoot(cwd) ) // hope it gets / (for Linux), C: D: (for Windows) - { - // if we are already at / or c: --> hopeless - if (verbose) - std::cout << " File path [" << name << "] doesn't exist" << std::endl; - tooHigh = true; - } - else - { - // iterate on ../ and go up from the current working dir! - std::string a(name); - bool alreadyProcessRoot = false; - for(;;) - { - std::string::size_type slash_position = cwd.find_last_of(ConfigurationFile::GetInstance().Get_file_separator ()); - 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 ../ - if (a == "" || alreadyProcessRoot) - { - if (verbose) - std::cout << " File path [" << name << "] doesn't exist" << std::endl; - tooHigh = true; - break; - } - // std::string b = cwd + a; - libname = cwd; - char c = cwd[cwd.size()-1]; - if (c != '/' && c != '\\' ) - libname += ConfigurationFile::GetInstance().Get_file_separator (); - libname += a; - - if ( a[0] != '.' ) // if . (probabely ../), loop again - break; - - if (IsAtRoot(cwd)) - alreadyProcessRoot = true; - } - } // end iterating on ../ - } - if (tooHigh) - libname=""; - return (libname); - - } // ----- End of expanding path name ( ./ ../ ../../ ) - - // To avoid warning - return(""); // Will never get here! - } - -// =================================================================================== - - static std::string MakeLibnameFromPath(std::string path, std::string pkgname) - { - std::string libname = path; - char c = path[path.size()-1]; -#if defined(__GNUC__) - if (c != '/') - libname += "/libbb"; - libname += pkgname; - libname += ".so"; - -#elif defined(_WIN32) - if (c != '\\') - libname = path+"\\bb"; - libname += pkgname; - libname += ".dll"; -#endif - return libname; - } - -// =================================================================================== - - static inline std::string MakePkgnameFromPath(std::string path, std::string pkgname) - { - std::string libname = path; - char c = path[path.size()-1]; - if (c != '/' && c != '\\') - libname += ConfigurationFile::GetInstance().Get_file_separator (); - libname += pkgname; - 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); -} - -// =================================================================================== + replace( name, + INVALID_FILE_SEPARATOR , + VALID_FILE_SEPARATOR); + } - 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()); + // ======================================================================= + /// Returns the user settings dir, e.g. /home/username/.bbtk + static std::string GetUserSettingsDir(); - } - /* - else - { - bbtkError(in<<" : expected 'a.b' format but no dot found"); - } - */ - left =""; - right = ""; - } + // ======================================================================= + /// Builds the complete path to the file 'name' located + /// in user settings dir, e.g. /home/username/.bbtk/ + static std::string MakeUserSettingsFullFileName(const std::string& name); + + + static bool IsAtRoot(std::string cwd); + // ====================================================================== + + static bool IsDirectory(std::string const &dirName); - 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); - } - else - { - return s; - } - } + static void CreateDirectoryIfNeeded( std::string const &dirName); + // ======================================================================= + + static void SplitAroundFirstDot( const std::string& in, + std::string& left, + std::string& right); + //====================================================================== + static void SplitString ( const std::string& str, + const std::string& delimiters, + std::vector& tokens); + //==================================================================== + + + // ==================================================================== + + static std::string get_file_name(const std::string& 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); + + + //======================================================================= + // Replaces substrings "\\n" by a real carriage return "\n" + static void SubsBackslashN ( std::string& s ); + //======================================================================= //======================================================================== - // 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 ) + const std::string& what, + const std::string& with ) { - using std::string; - string::size_type pos = str.find( from ); - while ( pos != string::npos ) + std::string::size_type pos = str.find( what ); + while ( pos != std::string::npos ) { - str.replace( pos, from.size(), to ); - pos = str.find( from, pos+from.size()-1 ); + str.replace( pos, what.size(), with ); + pos = str.find( what, pos+what.size()-1 ); } } //======================================================================== - //======================================================================== 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); + + static std::string GetEnvHome(); + + + }; +class BBTK_EXPORT NodeTreeC +{ +public: + std::string data; + std::vector childs; + + NodeTreeC(); + NodeTreeC(std::string _data); + ~NodeTreeC(); + void insertChild(std::string _data); + void treeTour(int lvl); + void setData(std::string _data); + void deleteTree(); +}; + } // namespace bbtk #endif //#ifndef __bbtkUtilities_h_INCLUDED__