#ifdef DOC_BUILD_INTERNAL
#endif
#include "misc.h"
#ifdef SPASS_TEST
#include "munit.h"
#endif
const PRIOR_T PRIOR_MAX = UINT_MAX;
void misc_Error(void)
{
  fflush(misc_USERERROROUT);
  fflush(stdout);
  fflush(stderr);
#ifdef SPASS_TEST
  munit_error("");
#else
#ifdef SPASS_CHECK
  abort();
#else
  exit(EXIT_FAILURE);
#endif
#endif
}
BOOL misc_SmallerThan(int i, int j)
{
  return (BOOL)(i < j);
}
BOOL misc_TestForNull(intptr_t ptr ) { return ((void*)ptr == NULL); }
BOOL misc_NotNull(intptr_t ptr) { return ((void*)ptr != NULL); }
BOOL misc_True(intptr_t ptr) { return TRUE; }
void misc_Assert(BOOL test, const char* file, int line)
{
  if(!test)
  {
    misc_StartErrorReport();
   misc_ErrorReport("\n\tAssertion failed in file %s at line %d\n",file, line);
   misc_FinishErrorReport();
  }
}
void misc_StartErrorReporting(const char* file, int line)
{
#ifdef SPASS_QUIET_ERRORS
  fflush(stdout);
  fprintf(misc_ERROROUT,"\nError\n");
#else
  fflush(stdout);
  fprintf(misc_ERROROUT,"\n\tError in file %s at line %d\n",file, line);
#endif
}
void misc_FinishErrorReport(void)
{
#ifndef SPASS_QUIET_ERRORS
  fputs("\n Please report this error.\n",misc_ERROROUT);
#endif
  #ifdef SPASS_TEST
  munit_errorf_ex(NULL,0,"Try to continue with next testcase");
  #else
  misc_DumpCore();
  #endif
}
void misc_ErrorReport(const char * Format, ...)
{
#ifndef SPASS_QUIET_ERRORS
  va_list args;
  va_start(args,Format);
  vfprintf(misc_ERROROUT,Format,args);
  va_end(args);
#endif
}
void misc_UserWarning(const char * Format, ...)
{
#ifndef SPASS_QUIET_ERRORS
  va_list args;
  va_start(args,Format);
  vfprintf(misc_ERROROUT,Format,args);
  va_end(args);
#endif
}
void misc_UserErrorReport(const char * Format, ...)
{
#ifndef SPASS_QUIET_ERRORS
  va_list args;
  va_start(args,Format);
  vfprintf(misc_USERERROROUT,Format,args);
  va_end(args);
#endif
}
void misc_PrintChar(NAT Number, char Character)
{
  NAT Counter;
  for (Counter = 1; Counter <= Number; Counter++)
    putchar(Character);
}
void misc_DumpCore(void)
{
  fputs("\n\n", misc_ERROROUT);
  fflush(misc_ERROROUT);
  fflush(stdout);
  fflush(stderr);
  abort();
}
void misc_DumpCoreOut(const char* String)
{
  fprintf(stderr, "\n %s \n", String);
  misc_DumpCore();
}
int misc_ReturnValue(void)
{
  return 0;
}
int misc_Max(int a, int b)
{
  if (a > b)
    return a;
  else
    return b;
}
int misc_Min(int a, int b)
{
  if (a < b)
    return a;
  else
    return b;
}
int misc_IntCmp(const void *v1, const void *v2)
{
  int a,b;
  a = *(int *)v1;
  b = *(int *)v2;
  return (a<b)?-1:(a>b);
}
int misc_IntptrCmp(const void *A, const void *B)
{
  intptr_t a, b;
  a = *(intptr_t *)A;
  b = *(intptr_t *)B;
  return (int)(a<b ? -1 : (a>b));
}
int misc_UIntptrCmp(const void *A, const void *B)
{
  uintptr_t a, b;
  a = *(uintptr_t *)A;
  b = *(uintptr_t *)B;
  return (int)(a<b ? -1 : (a>b));
}
BOOL misc_IntEqual(int a, int b)
{
    return (a == b) ? TRUE:FALSE;
}
BOOL misc_PtrEqual(intptr_t A, intptr_t B)
{
    return (A == B) ? TRUE : FALSE;
}
int misc_Ident(int a)
{
    return a;
}
int misc_Sgn(intptr_t a)
{
  return ((int)(a > (intptr_t)0))-((int)(a < (intptr_t)0));
}
void misc_PrintBitmap(unsigned long Nat)
{
  int i;
  for (i = sizeof(unsigned long)*8-1; i>=0; i--) {
    if ((((long unsigned)1<<i) & Nat) != 0)
      putchar('1');
    else
      putchar('0');
  }
}
