]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkFactory.cxx
Move some general usage methods to Utilities, to avoid dupplicate code :
[bbtk.git] / kernel / src / bbtkFactory.cxx
index eb0b829339d9e4e6330f7071e70bf995c9efa629..2accdca393e67717daa9060a1185a12e6e67bc6e 100644 (file)
@@ -4,8 +4,8 @@ Program:   bbtk
 Module:    $RCSfile: bbtkFactory.cxx,v $
 Language:  C++
 
-Date:      $Date: 2008/01/22 15:02:00 $
-Version:   $Revision: 1.1 $
+Date:      $Date: 2008/01/22 16:55:04 $
+Version:   $Revision: 1.2 $
                                                                                 
 
 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
@@ -27,6 +27,7 @@ PURPOSE.  See the above copyright notices for more information.
 #include "bbtkMessageManager.h"
 #include "bbtkConnection.h"
 #include "bbtkConfigurationFile.h"
+#include "bbtkUtilities.h"
 
 #include <sys/stat.h> // for struct stat stFileInfo
 
@@ -97,210 +98,7 @@ namespace bbtk
   }
   //===================================================================
 
-// --> usefull in many places (at least : ConfigurationFile, Factory, Interpreter)
-// should be factorized ( "bbtk::Util class ?)
-/*
-bool Factory::FileExists(std::string strFilename)
-bool Factory::IsAtRoot(std::string cwd)
-std::string Factory::ExtractPackageName(const std::string  &name)
-std::string Factory::ExpandLibName(const std::string &name, bool verbose)
-std::string Factory::MakeLibnameFromPath(std::string path, std::string pkgname)
-bool Factory::CheckIfLibraryContainsPackage(std::string libname,std::string pkgname, bool verbose)
-*/
-
-// See : http://www.techbytes.ca/techbyte103.html for more O.S.
-bool Factory::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;
-  }
-
-  return(blnReturn);
-}
-  
-// ===================================================================================
-
-  std::string Factory::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='"<<path<<"'"<<std::endl;//+1,std::string::npos); 
-      } 
-    else 
-      {
-       pkgname = name;  
-      }      
-          
-    // remove {.so | dll} if any
-    std::string::size_type dot_position = pkgname.find_last_of('.');      
-    if (dot_position != std::string::npos){
-      pkgname = pkgname.substr(0,dot_position);
-    }      
-#if defined(__GNUC__)
-    
-    // GCC mechanism
-    // shared lib name = libbb<name>.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 = <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;
-  }
-  
-// ===================================================================================
-
-  std::string Factory::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] == '\\') ) 
-    {       
-      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;
-              }
-              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="";
-//  std::cout << "=======================================3 libname [" << libname << "]" << std::endl;
-         
-      return (libname);
-      
-    }  // -----   End of expanding path name   ( ./ ../ ../../ )
-    // avoid warnings
-    return(""); // will never get here!
-  } 
-   
-// ===================================================================================
-
-  std::string Factory::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;    
-  }
-   
-// ===================================================================================
-
 
-  bool  Factory::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);
-}
 // ===================================================================================
 
   bool Factory::DoLoadPackage(std::string libname,
@@ -421,8 +219,9 @@ bool Factory::FileExists(std::string strFilename) {
          + ".." + docreldoc;
        std::string doc = path + separator + ".." + separator
          + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) 
-         + docreldoc; 
-       std::cout << "doc='"<<doc<<"'"<<std::endl;
+         + docreldoc;
+     
+       //std::cout << "doc='"<<doc<<"'"<<std::endl;
        
        pack.mPackage->SetDocURL(doc);
        pack.mPackage->SetDocRelativeURL(reldoc);
@@ -476,7 +275,7 @@ bool Factory::FileExists(std::string strFilename) {
     std::string pkgname;  // e.g. libbb<pkgname>.so
     
     std::string upath;
-    pkgname = ExtractPackageName(name,upath);
+    pkgname = Utilities::ExtractPackageName(name,upath);
 
     bbtkMessage("Debug",1,"Package name ["<<pkgname<<"]"<<std::endl);
     bbtkMessage("Debug",1,"Package path ["<<upath<<"]"<<std::endl);
@@ -522,12 +321,12 @@ bool Factory::FileExists(std::string strFilename) {
           path = currentDir;
         }
        
-        libname = MakeLibnameFromPath(path, pkgname);
+        libname = Utilities::MakeLibnameFromPath(path, pkgname);
        
             bbtkMessage("Debug",2,"-> Trying to load ["<<libname<<"]"<<std::endl);
 
       // Check if library exists           
-        if ( !FileExists(libname) )
+        if ( !Utilities::FileExists(libname) )
         {
         // The following is *NOT* a debug time message :
         // It's a user intended message.
@@ -583,7 +382,7 @@ bool Factory::FileExists(std::string strFilename) {
  void Factory::UnLoadPackage( const std::string& userSuppliedName )
   { 
     std::string path;
-    std::string name = ExtractPackageName(userSuppliedName,path);
+    std::string name = Utilities::ExtractPackageName(userSuppliedName,path);
     bbtkDebugMessageInc("Core",7,"Factory::UnLoadPackage(\""
                        <<name<<"\")"<<std::endl);