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



#include "n_exceptions.h"


ExceptionContextList* __Exceptions = NULL;



void push_exception()
{
	ExceptionContextList *_new = ( ExceptionContextList * )malloc( sizeof( ExceptionContextList ) );

	if( _new )
	{
		_new -> custom_msg = _new -> std_msg = NULL ;
		_new->head = __Exceptions;
		__Exceptions = _new;
	}
	else
	{
		fprintf( stderr , "Cannot even add a new exception context !!" );
		exit( 0 );
	}
} /* push_exception() */



void pop_exception( int ex )
{
	int no_known_exception = 1 ;
	ExceptionContextList *head = NULL ;
	head = __Exceptions -> head;

	if( __Exceptions -> custom_msg )
		free( __Exceptions -> custom_msg );
	if( __Exceptions -> std_msg )
		free( __Exceptions -> std_msg );

	free( __Exceptions );

	__Exceptions = head;

	if( ex != NO_EXCEPTION ) 
	{
		if( __Exceptions )
		{
			longjmp( __Exceptions -> context , ex );
		}
		else
		{
			if( ex & ARRAY_EXCEPTION )
			{
				no_known_exception = 0 ;
			}
			if( ex & DIVZERO_EXCEPTION ) 
			{
				no_known_exception = 0 ;
			}
			if( ex & OVERFLOW_EXCEPTION ) 
			{
				no_known_exception = 0 ;
			}
			if( ex & PARSING_EXCEPTION )
			{
				no_known_exception = 0 ;
			}
			if( ex & IO_EXCEPTION )
			{
				no_known_exception = 0 ;
			}
			if( no_known_exception )
			{
				fprintf( stderr , "Unknow exception\n" );
				exit(-1);
			}
		}
	}
} /* pop_exception */



