]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkInterpreter.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkInterpreter.cxx
index 2147d0ca35c1b84760ca57380c523ad77b85b89a..674513f847edc62677042542069d0285351b42f0 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkInterpreter.cxx,v $ $
   Language:  C++
-  Date:      $Date: 2008/03/18 12:51:26 $
-  Version:   $Revision: 1.46 $
+  Date:      $Date: 2008/03/19 14:58:13 $
+  Version:   $Revision: 1.47 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -302,57 +302,41 @@ bufferNb =0;
   /**
    *  
    */
-  void Interpreter::InterpretFile( const std::string& filename )
+  Interpreter::ExitStatus Interpreter::InterpretFile( const std::string& filename )
   {
     bbtkDebugMessageInc("Interpreter",9,"Interpreter::InterpretFile(\""<<filename<<"\")"<<std::endl);
-  //  std::cout << "==========================================Entree InterpretFile ["<<filename<<"] try SwitchToFile "<<std::endl;
+
     bool exm = mCommandLine;
     mCommandLine = false;
 
+    ExitStatus status = OK;
+
     try 
     {
       SwitchToFile(filename);
-
-/*
-      for( std::vector<std::string>::iterator it =mFileName.begin(); it!=mFileName.end(); ++it)
-      {
-         std::cout << "A=== [" << (*it) << "]" << std::endl;
-      }
-*/      
       bool insideComment = false; // for multiline comment
       while (mFile.size()>0) 
       {
-/*
-         for( std::vector<std::string>::iterator it =mFileName.begin(); it!=mFileName.end(); ++it)
-         {
-            std::cout << "B=== [" << (*it) << "]" << std::endl;
-         }
-*/      
-
-         //while ((mFile.size()>0) && !mFile.back()->eof() ) 
-         //{
-//      std::cout << "mFile.size() "<< mFile.size() << " mFileName.back() [" << mFileName.back() << "]" <<  std::endl;
-        while (!mFile.back()->eof()) {
-                    mLine.back()++;
-            char buf[500];
-            mFile.back()->getline(buf,500);
-            std::string str(buf);
-// std::cout << " in InterpretFile mFile.back()->getline [" << str << "]" << std::endl;    
-            int size=str.length();
-            if ( str[ size-1 ]==13  )
+       while (!mFile.back()->eof()) {
+         mLine.back()++;
+         char buf[500];
+         mFile.back()->getline(buf,500);
+         std::string str(buf);
+         int size=str.length();
+         if ( str[ size-1 ]==13  )
             {
-               str.erase(size-1,1);
+             str.erase(size-1,1);
             }
-      
-               InterpretLine(str, insideComment);
-
+         
+         InterpretLine(str, insideComment);
+         
        }//while
-       //if (mFile.size()>0) 
        CloseCurrentFile();
       }
     }
     catch (QuitException e) 
     {
+      status = QUIT;
     }
     catch (bbtk::Exception e) 
     {
@@ -361,6 +345,7 @@ bufferNb =0;
          std::cerr << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
          std::cerr << "* LINE  : "<<mLine.back()<<std::endl;
       }    
+      status = ERROR;
     }
     catch (std::exception& e) 
     {
@@ -368,15 +353,17 @@ bufferNb =0;
        if (mFileName.size()) {
           std::cerr << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
           std::cerr << "* LINE  : "<<mLine.back()<<std::endl;
-       }    
+       }  
+      status = ERROR;
     }  
     catch (...)
     {
-       std::cout << "* UNDEFINED ERROR (not a bbtk nor a std exception)"<<std::endl;
+       std::cerr << "* UNDEFINED ERROR (not a bbtk nor a std exception)"<<std::endl;
        if (mFileName.size()) {
-          std::cout << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
-          std::cout << "* LINE  : "<<mLine.back()<<std::endl;
+          std::cerr << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
+          std::cerr << "* LINE  : "<<mLine.back()<<std::endl;
       }    
+       status = ERROR;
     }
 
     CloseAllFiles();
@@ -384,6 +371,8 @@ bufferNb =0;
     bbtkDecTab("Interpreter",9);
 
     mCommandLine = exm;
+    
+    return status;
   }
   //=======================================================================
 
@@ -392,93 +381,128 @@ bufferNb =0;
   /**
    *  
    */
-  void Interpreter::InterpretBuffer( std::stringstream* buffer )
+  Interpreter::ExitStatus 
+  Interpreter::InterpretBuffer( std::stringstream* buffer )
   {
     bbtkDebugMessageInc("Interpreter",9,"Interpreter::InterpretBuffer()"<<std::endl);
-  //  std::cout << "==========================================Entree InterpretFile ["<<filename<<"] try SwitchToFile "<<std::endl;
+
     bool exm = mCommandLine;
     mCommandLine = false;
 
+    ExitStatus status = OK;
+
     try 
     {
       SwitchToStream(buffer);
-
-/*
-      for( std::vector<std::string>::iterator it =mFileName.begin(); it!=mFileName.end(); ++it)
-      {
-         std::cout << "A=== [" << (*it) << "]" << std::endl;
-      }
-*/      
       bool insideComment = false; // for multiline comment
       while (mFile.size()>0) 
       {
-/*
-         for( std::vector<std::string>::iterator it =mFileName.begin(); it!=mFileName.end(); ++it)
-         {
-            std::cout << "B=== [" << (*it) << "]" << std::endl;
-         }
-*/      
-
-         //while ((mFile.size()>0) && !mFile.back()->eof() ) 
-         //{
-//      std::cout << "mFile.size() "<< mFile.size() << " mFileName.back() [" << mFileName.back() << "]" <<  std::endl;
-        while (!mFile.back()->eof()) {
-                    mLine.back()++;
-            char buf[500];
-            mFile.back()->getline(buf,500);
-            std::string str(buf);
-// std::cout << " in InterpretFile mFile.back()->getline [" << str << "]" << std::endl;    
-            int size=str.length();
-            if ( str[ size-1 ]==13  )
+       while (!mFile.back()->eof()) {
+         mLine.back()++;
+         char buf[500];
+         mFile.back()->getline(buf,500);
+         std::string str(buf);
+         
+         int size=str.length();
+         if ( str[ size-1 ]==13  )
             {
-               str.erase(size-1,1);
+             str.erase(size-1,1);
             }
-      
-               InterpretLine(str, insideComment);
-
-       }//while
-       //if (mFile.size()>0) 
-       CloseCurrentFile();
+         
+         InterpretLine(str, insideComment);
+         
+       }//while
+       
+       CloseCurrentFile();
       }
     }
     catch (QuitException e) 
-    { 
-      //      std::cout  << "**QuitException caught**"<<std::endl;
-    }
+      
+       status = QUIT;
+      }
     catch (bbtk::Exception e) 
-    {
-      std::cerr << "* ERROR : "<<e.GetMessage()<<std::endl;
-      if (mFileName.size()) {
-         std::cerr << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
-         std::cerr << "* LINE  : "<<mLine.back()<<std::endl;
-      }    
-    }
+      {
+       std::cerr << "* ERROR : "<<e.GetMessage()<<std::endl;
+       if (mFileName.size()) 
+         {
+           std::cerr << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
+           std::cerr << "* LINE  : "<<mLine.back()<<std::endl;
+         }    
+       status = ERROR;
+      }
     catch (std::exception& e) 
-    {
-       std::cerr << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
-       if (mFileName.size()) {
-          std::cerr << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
-          std::cerr << "* LINE  : "<<mLine.back()<<std::endl;
-       }    
+      {
+       std::cerr << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
+       if (mFileName.size()) 
+         {
+           std::cerr << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
+           std::cerr << "* LINE  : "<<mLine.back()<<std::endl;
+         }    
+       status = ERROR;
     }  
     catch (...)
-    {
-       std::cout << "* UNDEFINED ERROR (not a bbtk nor a std exception)"<<std::endl;
-       if (mFileName.size()) {
-          std::cout << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
-          std::cout << "* LINE  : "<<mLine.back()<<std::endl;
-      }    
-    }
-
+      {
+       std::cerr 
+         << "* UNDEFINED ERROR (not a bbtk nor a std exception)"<<std::endl;
+       if (mFileName.size()) 
+         {
+           std::cerr << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
+           std::cerr << "* LINE  : "<<mLine.back()<<std::endl;
+         }    
+       status = ERROR;
+      }
+    
     CloseAllFiles();
     bbtkDebugMessage("Interpreter",9,"EO Interpreter::InterpretBuffer()"<<std::endl);
     bbtkDecTab("Interpreter",9);
-
+    
     mCommandLine = exm;
+    return status;
   }
   //=======================================================================
 
+  //=======================================================================
+  /// Runs the interpretation of a command
+  Interpreter::ExitStatus Interpreter::InterpretLine( const std::string& line )
+  {
+    bbtkDebugMessageInc("Interpreter",9,"Interpreter::InterpretLine('"<<line<<"')"<<std::endl);
+
+    ExitStatus status = OK;
 
+    try 
+    {
+      bool insideComment = false;
+      InterpretLine(line, insideComment);
+    }
+    catch (QuitException e) 
+      { 
+      status = QUIT;
+      }
+    catch (bbtk::Exception e) 
+      {
+       std::cerr << "* ERROR : "<<e.GetMessage()<<std::endl;
+       status = ERROR;
+      }
+    catch (std::exception& e) 
+      {
+       std::cerr << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
+       status = ERROR;
+    }  
+    catch (...)
+      {
+       std::cerr 
+         << "* UNDEFINED ERROR (not a bbtk nor a std exception)"<<std::endl;
+       status = ERROR;
+      }
+    
+
+    bbtkDebugMessage("Interpreter",9,"EO Interpreter::InterpretLine()"
+                    <<std::endl);
+    bbtkDecTab("Interpreter",9);
+    
+    return status;
+  }
+  
 
   //=======================================================================  
   /**
@@ -1696,6 +1720,7 @@ void Interpreter::Help(const std::vector<std::string>& words)
       }
       catch (QuitException e)
       {
+       bbtkMessage("Interpreter",1,"Interpreter : Quit"<<std::endl);
         again = false;
       }
       catch (bbtk::Exception e)