]> Creatis software - bbtk.git/commitdiff
Move some general usage methods to Utilities, to avoid dupplicate code :
authorjean-pierre roux <jean-pierre.roux@creatis.insa-lyon.fr>
Tue, 22 Jan 2008 16:55:04 +0000 (16:55 +0000)
committerjean-pierre roux <jean-pierre.roux@creatis.insa-lyon.fr>
Tue, 22 Jan 2008 16:55:04 +0000 (16:55 +0000)
static inline bool FileExists(std::string strFilename)
static std::string ExtractPackageName(const std::string  &name,
  std::string& path)
static std::string ExtractScriptName(const std::string  &name
static std::string ExpandLibName(const std::string &name, bool verbose)
static std::string MakeLibnameFromPath(std::string path, std::string pkgname)
static inline  bool IsAtRoot(std::string cwd)
static inline void SplitAroundFirstDot( const std::string& in,
                                            std::string& left,
                                            std::string& right)

  CVS: ----------------------------------------------------------------------

kernel/src/bbtkExecuter.h
kernel/src/bbtkFactory.cxx
kernel/src/bbtkFactory.h
kernel/src/bbtkInterpreter.cxx
kernel/src/bbtkInterpreter.h
kernel/src/bbtkUtilities.cxx
kernel/src/bbtkUtilities.h
kernel/src/bbtkVirtualExec.h

index be6099a40643627533bc3cc4b23449b9855d4efb..5477c917bd05754fc28a790d96623d0395b73de1 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkExecuter.h,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
   l'Image). All rights reserved. See Doc/License.txt or
@@ -43,16 +43,6 @@ namespace bbtk
 
   public:
 
-  /*
-   typedef enum 
-    {
-      NoDialog,
-      TextDialog,
-      GraphicalDialog
-    }
-    DialogModeType;
-*/    
-
     /// Constructor
     Executer();
 
@@ -174,7 +164,7 @@ namespace bbtk
     /// The root ComplexBlackBox, in which operations are done when outside a define/endefine block
     /// Its name in bbi is 'workspace'  
     ComplexBlackBoxDescriptor* mRoot;
-    
+
     /// Struct that stores info on user defined complex black boxes
     struct CBBDefinition
     {
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);
   
index 7a5476819d1452d9b2138abadca16b0d866abe40..7a72a469b2934d7318f9d519dad3819fc619a33f 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkFactory.h,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
   l'Image). All rights reserved. See doc/license.txt or
@@ -35,9 +35,6 @@
 
 namespace bbtk
 {
-
-
-
   class BBTK_EXPORT Factory
   {
 
@@ -71,26 +68,18 @@ namespace bbtk
                               const std::string& output,
                               BlackBox* to,
                               const std::string& input) const;
-    
+
     void WriteDotFilePackagesList(FILE *ff);
 
     void Reset();
-    
-    // usefull in many places.
-    // -> should be factorized : bbtk::Util class?    
-    bool FileExists(std::string strFilename);
+
   private:
-    /// the methods for LoadPackage
-    std::string ExtractPackageName(const std::string &name, 
-                                  std::string& path);
-    std::string ExpandLibName(const std::string &name, bool v);  
-    std::string MakeLibnameFromPath(std::string path, std::string pkgname);
+
     bool DoLoadPackage(std::string libname,
                       std::string pkgname,
                       std::string path,
                       bool v);
-    bool IsAtRoot(std::string cwd);     
-    private:
+
     /// The structure storing info on a package
     class PackageInfoType
     {
index f500e92fde9d125becaecee6a8ccbe9203c41779..ef03a40f986572635709764b305897bbfb162af2 100644 (file)
@@ -3,13 +3,13 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkInterpreter.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
   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.
@@ -37,8 +37,6 @@ namespace bbtk
 
 Interpreter* Interpreter::mGlobalInterpreter = NULL;
 
-
-
  //=======================================================================
  /**
    *  
@@ -51,7 +49,6 @@ Interpreter* Interpreter::mGlobalInterpreter = NULL;
     bbtk::MessageManager::RegisterMessageType("Interpreter","Messages of the interpreter",0);
     bbtkDebugMessageInc("Interpreter",9,"Interpreter::Interpreter()" <<std::endl);
  
-
     mGlobalInterpreter = this;
 
     //    mFactory = new bbtk::Factory();
@@ -417,52 +414,55 @@ void Interpreter::InterpretLine( const std::string& line, bool &insideComment )
             break;
         
       case cConnect :
-            SplitAroundFirstDot(words[1],left,right);
-            SplitAroundFirstDot(words[2],left2,right2);      
+            Utilities::SplitAroundFirstDot(words[1],left,right);
+            Utilities::SplitAroundFirstDot(words[2],left2,right2);      
             mExecuter->Connect(left,right,left2,right2);
             break;
         
       case cDefine :
-       if (mFileName.size()>0) 
-         {
-           filename = Utilities::get_file_name(mFileName.back());
-         }
-       if (words.size()==2) 
-         {
-           mExecuter->Define(words[1],"user",filename);
-         }
-       else 
-         {
-           mExecuter->Define(words[1],words[2],filename);
-         }
-       break;
+            if (mFileName.size()>0) 
+            {
+               filename = Utilities::get_file_name(mFileName.back());
+            }
+            if (words.size()==2) 
+            {
+               mExecuter->Define(words[1],"user",filename);
+            }
+            else 
+            {
+              mExecuter->Define(words[1],words[2],filename);
+            }
+            break;
         
       case cEndDefine :
             mExecuter->EndDefine();
             break;
         
       case cPrint :
-            Print(words[1]);
+            Print(words[1]); /// \todo use mExecuter 
             break;
         
       case cExec :
-       if (words[1]=="freeze") mExecuter->SetNoExecMode(true);
-       else if (words[1]=="unfreeze") mExecuter->SetNoExecMode(false);
-       else mExecuter->Update(words[1]);
-       break;
+            if (words[1]=="freeze") 
+           mExecuter->SetNoExecMode(true);
+            else if (words[1]=="unfreeze") 
+           mExecuter->SetNoExecMode(false);
+            else 
+           mExecuter->Update(words[1]);
+            break;
         
       case cInput :
-            SplitAroundFirstDot(words[2],left,right);
+            Utilities::SplitAroundFirstDot(words[2],left,right);
             mExecuter->DefineInput(words[1],left,right,words[3]);
             break;
         
       case cOutput :
-            SplitAroundFirstDot(words[2],left,right);
+            Utilities::SplitAroundFirstDot(words[2],left,right);
             mExecuter->DefineOutput(words[1],left,right,words[3]);
             break;
         
       case cSet :
-            SplitAroundFirstDot(words[1],left,right);
+            Utilities::SplitAroundFirstDot(words[1],left,right);
             mExecuter->Set(left,right,words[2]);
             break;
         
@@ -485,8 +485,8 @@ void Interpreter::InterpretLine( const std::string& line, bool &insideComment )
             }
             else 
             {
-            sscanf(words[2].c_str(),"%d",&level);
-            bbtk::MessageManager::SetMessageLevel(words[1],level);
+           sscanf(words[2].c_str(),"%d",&level);
+           bbtk::MessageManager::SetMessageLevel(words[1],level);
             }
             break;
         
@@ -532,7 +532,7 @@ void Interpreter::InterpretLine( const std::string& line, bool &insideComment )
       case cWorkspace :
             if (words.size() == 2) 
             {
-               if (words[1]=="freeze") mExecuter->SetNoExecMode(true);
+               if (words[1]=="freeze")        mExecuter->SetNoExecMode(true);
                else if (words[1]=="unfreeze") mExecuter->SetNoExecMode(false);
             }
             else 
@@ -642,7 +642,6 @@ void Interpreter::SplitLine ( const std::string& str, std::vector<std::string>&
 
     bbtkDebugMessageInc("Interpreter",9,"Interpreter::SplitLine(\""<<str<<"\")"<<std::endl);
 
-
     std::vector<std::string> chains;
     std::string delimiters("$");
 
@@ -667,7 +666,7 @@ void Interpreter::SplitLine ( const std::string& str, std::vector<std::string>&
        // is an output (between $$) : decode 
          std::string tok,box,output;
          tok = str.substr(lastPos, pos - lastPos);
-         SplitAroundFirstDot(tok,box,output);
+         Utilities::SplitAroundFirstDot(tok,box,output);
          chains.push_back( mExecuter->Get(box,output) );
     //    std::cout << "outp='"<<chains.back()<<"'"<<std::endl;
        }
@@ -695,167 +694,6 @@ void Interpreter::SplitLine ( const std::string& str, std::vector<std::string>&
    *  
    */
 
-
-// --> usefull in many places (at least : ConfigurationFile, Factory, Interpreter)
-// should be factorized ( "bbtk::Util class ?)
-/*
-bool Interpreter::FileExists(std::string strFilename)
-bool Interpreter::IsAtRoot(std::string cwd)
-std::string Interpreter::ExtractPackageName(const std::string  &name)
-std::string Interpreter::ExpandLibName(const std::string &name, bool verbose)
-std::string Interpreter::MakeLibnameFromPath(std::string path, std::string pkgname)
-*/    
-// ===================================================================================
-  
-// See : http://www.techbytes.ca/techbyte103.html for more O.S.
-bool Interpreter::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 Interpreter::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;
-  }
-  
-// ===================================================================================
-
-  std::string Interpreter::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;
-              }
-             // 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!
-  } 
-
-  
-// ===================================================================================
-
-  std::string Interpreter::MakeLibnameFromPath(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;    
-  }
-  
-// ===================================================================================
-
-  bool  Interpreter::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);
-}
-
-  
 // ===================================================================================
 
 
@@ -869,7 +707,7 @@ bool Interpreter::FileExists(std::string strFilename) {
     std::string libname;  // full path library name
     std::string pkgname;  // e.g. <scriptname>.bbs
      
-    pkgname = ExtractScriptName(name);
+    pkgname = Utilities::ExtractScriptName(name);
 
     if (use_configuration_file)
     {
@@ -885,9 +723,9 @@ bool Interpreter::FileExists(std::string strFilename) {
     if (slash_position != std::string::npos)
     {
       fullnameGiven = true;     
-      libname = ExpandLibName(name, verbose);
+      libname =  Utilities::ExpandLibName(name, verbose);
       if (libname != "") {
-        if (FileExists(libname))
+        if ( Utilities::FileExists(libname))
         {
           foundFile = true;
         }
@@ -909,10 +747,10 @@ bool Interpreter::FileExists(std::string strFilename) {
           path = currentDir;
         }
 
-        libname = MakeLibnameFromPath(path, pkgname);
+        libname = Utilities::MakePkgnameFromPath(path, pkgname);
 
       // Check if library exists           
-        if ( !FileExists(libname) )
+        if ( ! Utilities::FileExists(libname) )
         {
           if (verbose)
             std::cout <<"   [" <<libname <<"] : doesn't exist" <<std::endl;
@@ -1502,37 +1340,6 @@ void Interpreter::Help(const std::vector<std::string>& words)
     
     bbtkDebugDecTab("Interpreter",9);
   }
-  //=======================================================================
-  
-  
-  //=======================================================================
-  void Interpreter::SplitAroundFirstDot( const std::string& in,
-                                         std::string& left,
-                                         std::string& right)
-  {
-    bbtkDebugMessageInc("Interpreter",9,
-                        "Interpreter::SplitAroundFirstDot(\""
-                        <<in<<"\")"<<std::endl);
-    
-    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());
-       bbtkDebugMessage("Interpreter",9,
-       "["<<left<<"] ["<<right<<"]"<<std::endl);
-    }
-    else 
-    {
-       bbtkError(in<<" : expected 'a.b' format but no dot found");
-    }
-    
-    bbtkDebugDecTab("Interpreter",9);
-  }
-  //=======================================================================
-  
-
 
   //=======================================================================
   void Interpreter::Graph(const std::vector<std::string>& words)
index b078a5884d5c5f8a579a89e6a39bae0427332be5..4d81c655d54406aa42dc5ee63510e0abe9b38bfa 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkInterpreter.h,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
   l'Image). All rights reserved. See Doc/License.txt or
 #ifndef __bbtkInterpreter_h__
 #define __bbtkInterpreter_h__
 
-
+#include "bbtkVirtualExec.h"
 #include "bbtkExecuter.h"
+#include "bbtkTranscriptor.h"
+
 #include <fstream>
 #include <deque>
 
 namespace bbtk
 {
  
   class BBTK_EXPORT Interpreter
   {
 
@@ -67,7 +68,7 @@ namespace bbtk
       cPrint,
       cWorkspace // LG 
     } CommandCodeType;
-    
+
     /// The structure storing the informations on a command 
     typedef struct 
     {
@@ -78,14 +79,13 @@ namespace bbtk
       std::string help;
     } CommandInfoType;
 
-
-
     /// The type of dictionnary of commands 
     typedef std::map<std::string,CommandInfoType> CommandDictType;
 
   public:
     /// Constructor
-    Interpreter();  
+    Interpreter();
     /// Destructor
     ~Interpreter();
       
@@ -104,7 +104,8 @@ namespace bbtk
     void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
 
     /// 
-    typedef Executer::DialogModeType DialogModeType;
+    //typedef Executer::DialogModeType DialogModeType;
+    typedef VirtualExec::DialogModeType DialogModeType;
 
     void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
 
@@ -150,11 +151,7 @@ namespace bbtk
     void Config(bool verbose) const; // JPR
     /// 
     void Print(const std::string&);
-    
-    /// Splits the string in around the first dot encountered
-    void SplitAroundFirstDot( const std::string& in,
-                              std::string& left,
-                              std::string& right);
+
     ///
     void FindCommandsWithPrefix( char* buf,
                                 int n,
@@ -163,32 +160,35 @@ namespace bbtk
     /// Sets the bool that indicates wether we are in command line context
     void SetCommandLine(bool v = true) { mCommandLine = v; }
 
-  private:
-  /// the methods for include bbs
-  bool FileExists(std::string strFilename);
-  std::string ExtractScriptName(const std::string &name);
-  std::string ExpandLibName(const std::string &name, bool v);  
-  std::string MakeLibnameFromPath(std::string path, std::string pkgname);
-  bool IsAtRoot(std::string cwd);       
+  private: 
     
   private:
   
     //==================================================================
     // ATTRIBUTES
+
     /// The factory
     //    bbtk::Factory* mFactory;
+
     /// The command executer  
-    bbtk::Executer* mExecuter;
+   // bbtk::Executer* mExecuter;
+    bbtk::VirtualExec* mExecuter;
+
     /// Vector of opened files  
-    std::vector<std::ifstream*> mFile; 
+    std::vector<std::ifstream*> mFile;
+
     /// Vector of names of open files  
-    std::vector<std::string> mFileName; 
+    std::vector<std::string> mFileName;
+
     /// Stores the current line number in each open file
-    std::vector<int> mLine; 
+    std::vector<int> mLine;
+
     /// The dictionnary of commands
     CommandDictType mCommandDict;
+
     /// Are we in a command line context ?
     bool mCommandLine;
+    
     /// The history of commands 
     std::deque< char* > mHistory; 
     
index 6ebbe6152ed020ceeb2303db6b741c9fe08c2c44..f15e7080650ce658a9bc018695552c9722905d57 100644 (file)
@@ -3,28 +3,5 @@
 
 namespace bbtk
 {
-  /*
-  //========================================================================
-    /// Usefull functions for html generation
-    static void replace( std::string& str,
-                        const std::string& from, const std::string& to )
-    {
-      using std::string;
-      string::size_type pos = str.find( from );
-      while ( pos != string::npos )
-       {
-         //    std::cout << "'" << str << "' -> '";
-         str.replace( pos, from.size(), to );
-         pos = str.find( from, pos+from.size()-1 );
-         //    std::cout << str << "'"<< std::endl;
-       } 
-    }
-  //==========================================================================
-  inline void html_format(std::string& str)
-  {
-    replace( str, "&", "&amp;" );
-    replace( str, "<", "&lt;"# );
-    replace( str, ">", "&gt;" );
-  }
-  */
+
 }
index b83193fd91e4aee1d9ec002fea8d31317cb5f13e..d9c3193a0a07eac9f7c8cdcefb62fed751a76e84 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkUtilities.h,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
   l'Image). All rights reserved. See doc/license.txt or
 #ifndef __bbtkUtilities_h_INCLUDED__
 #define __bbtkUtilities_h_INCLUDED__
 
+#include "bbtkConfigurationFile.h"
 #include "bbtkSystem.h"
 #include <string>
+#include <sys/stat.h>
+#ifdef CMAKE_HAVE_TERMIOS_H
+#include <termios.h>
+#define BBTK_USE_TERMIOS_BASED_PROMPT
+#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;
+
+     // 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='"<<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;
+  }
+
+// ===================================================================================
+
+  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] == '\\') ) 
+    {
+      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);
+}
+
+// ===================================================================================
+
+    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());
+
+      }
+      /*
+      else
+      {
+        bbtkError(in<<" : expected 'a.b' format but no dot found");
+      }
+      */
+      left ="";
+      right = "";
+    }
+
     static inline std::string get_file_name(const std::string& s) 
     { 
       std::string::size_type slash_position = s.find_last_of("/\\");
@@ -48,7 +305,7 @@ namespace bbtk
       else 
       {
         return s;
-      }      
+      }
     }
 
 
index 9a15909b31992747064a408f34abcd5fcfcd9635..2edbd0e55b4d5d6020ad22c6a9d1d2aa547d7692 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkVirtualExec.h,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
   l'Image). All rights reserved. See Doc/License.txt or