]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkInterpreter.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkInterpreter.cxx
index 82edfcd775c46d173dc99d500d29f9d0492dfcf0..544729156a10cb88e6d430f6a174648a5194c666 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkInterpreter.cxx,v $ $
   Language:  C++
-  Date:      $Date: 2008/03/21 11:44:37 $
-  Version:   $Revision: 1.51 $
+  Date:      $Date: 2008/03/21 14:59:39 $
+  Version:   $Revision: 1.52 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -296,6 +296,143 @@ bufferNb =0;
   //=======================================================================
 
 
+  InterpreterError::InterpreterError( const std::string& message,
+                                     bool in_script_file,
+                                     const std::string& script_file,
+                                     int script_line 
+                                     )
+    : Exception("Interpreter",0,message),
+      mInScriptFile(in_script_file),
+      mScriptFile(script_file),
+      mScriptLine(script_line)
+  {
+  }
+  InterpreterError::InterpreterError( const Exception& excep,
+                     bool in_script_file,
+                     const std::string& script_file,
+                     int script_line 
+                     )
+    : Exception(excep),
+      mInScriptFile(in_script_file),
+      mScriptFile(script_file),
+      mScriptLine(script_line)
+  {
+  }
+  //=======================================================================
+  void Interpreter::CatchBbtkException( const bbtk::Exception& e )
+  {
+    if (mThrow) 
+      {
+       bool in_script = false;
+       std::string file("");
+       int line = 0;
+       if (mFileName.size()) {
+         std::ifstream* fs = dynamic_cast<std::ifstream*>(mFile.back());
+         if (fs!=0) in_script = true;    
+         file = mFileName.back();
+         line = mLine.back();
+       }    
+       throw InterpreterError(e,in_script,file,line);
+      }
+    else
+      {
+       std::stringstream mess;
+       mess << "* ERROR : "<<e.GetMessage()<<std::endl;
+       if (mFileName.size()) {
+         mess << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
+         mess << "* LINE  : "<<mLine.back()<<std::endl;
+       }    
+       std::cerr << mess.str();
+      }
+  }
+  //=======================================================================
+  
+  //=======================================================================
+  void Interpreter::CatchStdException( const std::exception& e )
+  {  
+    if (mThrow) 
+      {
+       bool in_script = false;
+       std::string file("");
+       int line = 0;
+       if (mFileName.size()) {
+         std::ifstream* fs = dynamic_cast<std::ifstream*>(mFile.back());
+         if (fs!=0) in_script = true;    
+         file = mFileName.back();
+         line = mLine.back();
+       }    
+       throw InterpreterError(e.what(),in_script,file,line);
+      }
+    else
+      {
+       std::stringstream mess;
+       mess << "* ERROR : "<<e.what()<<std::endl;
+       if (mFileName.size()) {
+         mess << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
+         mess << "* LINE  : "<<mLine.back()<<std::endl;
+       }    
+       std::cerr << mess.str();
+      }
+  }
+  //=======================================================================
+
+  //=======================================================================
+  void Interpreter::CatchUnknownException()
+  {
+    if (mThrow) 
+      {
+       bool in_script = false;
+       std::string file("");
+       int line = 0;
+       if (mFileName.size()) {
+         std::ifstream* fs = dynamic_cast<std::ifstream*>(mFile.back());
+         if (fs!=0) in_script = true;    
+         file = mFileName.back();
+         line = mLine.back();
+       }    
+       throw InterpreterError("Unknown exception caught",
+                                  in_script,file,line);
+      }
+    else
+      {
+       std::stringstream mess;
+       mess << "* UNDEFINED ERROR (not a bbtk nor a std exception)" 
+            << std::endl;
+       if (mFileName.size()) {
+         mess << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
+         mess << "* LINE  : "<<mLine.back()<<std::endl;
+       }    
+       std::cerr << mess.str();
+      }
+  }
+  //=======================================================================
+
+  //=======================================================================
+
+#define CATCH_MACRO                            \
+  catch (QuitException e)                      \
+    {                                          \
+      status = Interpreter_QUIT;               \
+      if (mThrow) throw QuitException();       \
+    }                                          \
+  catch (bbtk::Exception e)                    \
+    {                                          \
+      status = Interpreter_ERROR;              \
+      CatchBbtkException(e);                   \
+    }                                          \
+  catch (std::exception& e)                    \
+    {                                          \
+      status = Interpreter_ERROR;              \
+      CatchStdException(e);                    \
+    }                                          \
+  catch (...)                                  \
+    {                                          \
+      status = Interpreter_ERROR;              \
+      CatchUnknownException();                 \
+    }                                          
+  //=======================================================================
+   
+
   //=======================================================================
   /**
    *  
@@ -309,7 +446,6 @@ bufferNb =0;
 
     ExitStatus status = Interpreter_OK;
 
-  
     try 
     {
       SwitchToFile(filename);
@@ -334,55 +470,7 @@ bufferNb =0;
        CloseCurrentFile();
       }
     }
-    catch (QuitException e) 
-    {
-      status = Interpreter_QUIT;
-      if (mThrow) throw QuitException();
-    }
-    catch (bbtk::Exception e) 
-    {
-      std::stringstream mess;
-      mess << "* ERROR : "<<e.GetMessage()<<std::endl;
-      if (mFileName.size()) {
-         mess << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
-         mess << "* LINE  : "<<mLine.back()<<std::endl;
-      }    
-      status = Interpreter_ERROR;
-      if (mThrow) 
-       throw bbtk::Exception("Interpreter","",mess.str());
-      else
-       std::cerr << mess.str();
-
-    }
-    catch (std::exception& e) 
-    {
-       std::stringstream mess;
-       mess << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
-       if (mFileName.size()) {
-          mess << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
-          mess << "* LINE  : "<<mLine.back()<<std::endl;
-       }  
-      status = Interpreter_ERROR;
-      if (mThrow) 
-       throw bbtk::Exception("Interpreter","",mess.str());
-      else
-       std::cerr << mess.str();  
-    }  
-    catch (...)
-      {
-        std::stringstream mess;
-       mess << "* UNDEFINED ERROR (not a bbtk nor a std exception)"
-            <<std::endl;
-       if (mFileName.size()) {
-          mess << "* FILE  : \""<<mFileName.back()<<"\""<<std::endl;
-          mess << "* LINE  : "<<mLine.back()<<std::endl;
-       }    
-       status = Interpreter_ERROR;
-       if (mThrow) 
-         throw bbtk::Exception("Interpreter","",mess.str());
-       else
-         std::cerr << mess.str();   
-      }
+    CATCH_MACRO;
     
     CloseAllFiles();
     bbtkDebugMessage("Interpreter",9,"EO Interpreter::InterpretFile(\""<<filename<<"\")"<<std::endl);
@@ -434,41 +522,7 @@ bufferNb =0;
        CloseCurrentFile();
       }
     }
-    catch (QuitException e) 
-      { 
-       status = Interpreter_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;
-         }    
-       status = Interpreter_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;
-         }    
-       status = Interpreter_ERROR;
-    }  
-    catch (...)
-      {
-       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 = Interpreter_ERROR;
-      }
+    CATCH_MACRO;
     
     CloseAllFiles();
     bbtkDebugMessage("Interpreter",9,"EO Interpreter::InterpretBuffer()"<<std::endl);
@@ -492,7 +546,9 @@ bufferNb =0;
       bool insideComment = false;
       InterpretLine(line, insideComment);
     }
-    catch (QuitException e) 
+    CATCH_MACRO;
+    /*
+   catch (QuitException e) 
       { 
       status = Interpreter_QUIT;
       }
@@ -512,7 +568,7 @@ bufferNb =0;
          << "* UNDEFINED ERROR (not a bbtk nor a std exception)"<<std::endl;
        status = Interpreter_ERROR;
       }
-    
+    */
 
     bbtkDebugMessage("Interpreter",9,"EO Interpreter::InterpretLine()"
                     <<std::endl);
@@ -1176,6 +1232,7 @@ void Interpreter::SwitchToStream( std::stringstream* stream )
     mFileName.push_back(fullPathScriptName);
     mIncludeFileName.push_back(includeScriptName);
     mLine.push_back(0);
+
     return;
   }