// C++ code Copyright (C) David R. Evans G4AMJ/NQ0I

#include <defines.h>
#include <DREstring.h>

#include <fstream.h>

// define a handler for fatal error

void _FE(const char* cp, const char* file, const int line)
{ char temp1[100], temp2[10];
  strcpy(temp1, cp);
  if (*cp) then
    strcat(temp1, " -- ");
  strcat(temp1, file);
  strcat(temp1, " : ");
  strcat(temp1, itoa(line, temp2, 10));
  cerr << "Fatal error: " << temp1 << endl;
  exit(-2);
}

// define a handler for nonfatal error

void _NFE(const char* cp, const char* file, const int line)
{ trace(cp);
  char temp1[100], temp2[10];
  strcpy(temp1, cp);
  if (*cp) then
    strcat(temp1, " -- ");
  strcat(temp1, file);
  strcat(temp1, " : ");
  strcat(temp1, itoa(line, temp2, 10));
  cerr << "Nonfatal error: " << temp1 << endl;
}

// Merely setting the new_handler does not tell us where the error
// occurred. This is poor man's exception handling. I long for the
// real thing.

// define a handler to check an upcoming request for space; not all compilers
// for Intel chips like allocating > 64K at a time

void _CST(const unsigned long size, const unsigned long number,
         const char* file, const int line)
{ if (size * number > 65000L) then
    _FE("Attempt to allocate > 64K", file, line);
}

// a generic logging facility
void _TRACELOG(const char* msg, const char* file, const int line)
{ char temp[100];
  itoa(line, temp, 10);
  trace_log << msg << " (" << file << ":" << temp << ")" << endl << flush;
}

// byte inversion routines
/*void invert(uint16& ui16)
{ byte* b = (byte*)(void*)&ui16, t;
  t = b[0];
  b[0] = b[1];
  b[1] = t;
}*/

/*void invert(uint32& ui32)
{ uint16* ui16 = (uint16*)(void*)&ui32;
  invert(ui16[0]);
  invert(ui16[1]);
}*/
