/*=========================================================================
* Safe.h : Blindage
*
* Author               Fred "Bart" Compagnon
*
* Important : define SAF_SAVE ! ! !
*=======================================================================*/
 
#ifndef SAF_H
#define SAF_H
 
#if !defined(U64)	/* Arg... this has been ecrased !!! a long time ago !!! - Oliv' - 10/06/1999 */

#define SAF_CallMax 16
 
#if defined(ACTIVE_EDITOR)
  #define SAF_SAFE
#endif /*defined(ACTIVE_EDITOR)*/
 
#if defined SAF_SAFE
 
#include "setjmp.h"
 
#if defined(__cplusplus)
extern "C"
{
#endif /* __cplusplus */
 
  extern jmp_buf* SAF_fn_pbufGetJmpBufInc();
  extern jmp_buf* SAF_fn_pbufGetJmpBufDec();
  extern char SAF_fn_cGetCompt();
  extern void SAF_fn_vDecCompt();
  extern char* SAF_fn_szGetMsgPtr();
 
#if defined(__cplusplus)
}
#endif /* __cplusplus */
 
#if defined(__DeclareGlobalVariableSafe_h__)
 
  jmp_buf SAF_bufGlob[SAF_CallMax];
  unsigned char SAF_cCompt;
  char SAF_szErrorMsg[1024]= "";
 
  jmp_buf* SAF_fn_pbufGetJmpBufInc()
  {
    return &SAF_bufGlob[SAF_cCompt++];
  }
 
  jmp_buf* SAF_fn_pbufGetJmpBufDec()
  {
    return &SAF_bufGlob[--SAF_cCompt];
  }
 
  char SAF_fn_cGetCompt()
  {
    return SAF_cCompt;
  }
 
  void SAF_fn_vDecCompt()
  {
    SAF_cCompt--;
  }
 
  char* SAF_fn_szGetMsgPtr()
  {
    return SAF_szErrorMsg;
  }
 
#endif /*defined(__DeclareGlobalVariableSafe_h__)*/
 
 
 
#define SAF_M_Try \
if ( setjmp(*SAF_fn_pbufGetJmpBufInc()) == 0 ) \
{
 
 
#define SAF_M_Catch \
SAF_fn_vDecCompt(); \
} else {
 
 
#define SAF_M_CatchWithMsg(X) \
SAF_fn_vDecCompt(); \
} else { \
 (X)=SAF_fn_szGetMsgPtr();
 
#define SAF_M_EndCatch \
if (SAF_fn_cGetCompt()) longjmp(*SAF_fn_pbufGetJmpBufDec(),-1); }
 
 
#define SAF_M_Assert(X) \
{ if (!(X)) if (SAF_fn_cGetCompt()) longjmp(*SAF_fn_pbufGetJmpBufDec(),-1); \
}
 
#define SAF_M_AssertWithMsg(X,Y) \
{ if (!(X)) { \
  sprintf(SAF_fn_szGetMsgPtr(), Y); \
  if (SAF_fn_cGetCompt()) longjmp(*SAF_fn_pbufGetJmpBufDec(),-1); \
} \
}
 
 
 
 
#else /* ! defined SAF_SAFE */
 
  #define SAF_M_Try \
  {
 
  #define SAF_M_Catch \
  } \
  if (0) {
 
  #define SAF_M_CatchWithMsg(X) \
  } \
  if (0) {
 
  #define SAF_M_EndCatch \
  }
 
  #define SAF_M_Assert(X)
 
  #define SAF_M_AssertWithMsg(X,Y)
 
 
#endif /* defined SAF_SAFE */

#else /* U64 */

  #define SAF_M_Try \
  {

  #define SAF_M_Catch \
  } \
  if (0) {

  #define SAF_M_CatchWithMsg(X) \
  } \
  if (0) {

  #define SAF_M_EndCatch \
  }


#if !defined(RETAIL) || defined(USE_MONITOR)

  #define SAF_M_Assert(X)							if(!(X))	{ ASM_BREAK; }
  #define SAF_M_AssertWithMsg(X,Y)		if(!(X))	{ M_PrintfStopErrorN64((Y)); }

#else

  #define SAF_M_Assert(X)
  #define SAF_M_AssertWithMsg(X,Y)

#endif

#endif /* U64 */
 
#endif /* SAF_H */
