/**\file n_exceptions.c
 * ExceptionContextList management for C
 *\author Castagnier Mickael
 *\version 1.0
 *\date 20/11/09
 *
 */

#include "n_exceptions.h"


ExceptionContextList* __Exceptions = NULL;


/*!\fn void push_exception()
 *\brief Function to push an exception in the list
 */
void push_exception()
{
   ExceptionContextList *_new = ( ExceptionContextList * )malloc( sizeof( ExceptionContextList ) );

   if( _new )
   {
      _new->head = __Exceptions;
      __Exceptions = _new;
   }
   else
   {
      n_log( LOG_ERR , "Cannot even add a new exception context !!" );
      exit( FALSE );
   }
} /* push_exception() */



/*!\fn void pop_exception( int ex )
 *\brief Function to pop an exception in the list
 *\param ex Type of exception to pop
 */
void pop_exception( int ex )
{
   int no_known_exception = 1 ;
   ExceptionContextList *head = NULL ;
   head = __Exceptions -> head;
   Free( __Exceptions );
   __Exceptions = head;

   if( ex != NO_EXCEPTION ) 
   {
      if( __Exceptions )
      {
         longjmp( __Exceptions -> context , ex );
      }
      else
      {
         char str[256];
         if( ex & ARRAY_EXCEPTION )
         {
            no_known_exception = 0 ;
            strcpy( str, "Array exception " );    
         }
         if( ex & DIVZERO_EXCEPTION ) 
         {
            no_known_exception = 0 ;
            strcpy( str, "Div zero exception " ); 
         }
         if( ex & OVERFLOW_EXCEPTION ) 
         {
            no_known_exception = 0 ;
            strcpy( str, "Overflow exception " ); 
         }
         if( ex & PARSING_EXCEPTION )
         {
            no_known_exception = 0 ;
            strcpy( str , "Parsing exception " );
         }
         if( no_known_exception )
         {
            strcpy( str , "Unknow exception" );
            n_log( LOG_ERR , "Exception number: %d, message: %s" , ex , str );
            exit(-1);
         }
         n_log( LOG_ERR , "Exception number: %d, message: %s" , ex , str );
      }
   }
} /* pop_exception */

