// C++ code Copyright (C) David R. Evans G4AMJ/NQ0I

#ifndef DEFS_SUNH
#define DEFS_SUNH

#include <time.h>       // to get usleep

// These are put here because the idiotic Mac has its own idea of
// the meaning of "true" and "false".
#define false	0
#define true	1
#define forever while(true)

typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
typedef unsigned int uint;

// we do this to save us from having to read stdio every time
#if (!defined(FILE))
#define FILE struct _iobuf
#define TEMPORARY_FILE_DEFINITION
#endif

template <class T> inline int SIGN(T x)
  { return ((x < 0) ? -1 : ((x > 0) ? 1 : 0)); }

template <class T> inline T MAX(T x, T y)
  { return ((x > y) ? x : y); }

template <class T> inline T MIN(T x, T y)
  { return ((x < y) ? x : y); }

template <class T> inline T SQR(T x)
  { return (x * x); }

// GNU is rather daft when it comes to trying to build a type safe
// binary read, so we use an unsafe macro instead.
#define read_binary(x,y) fread((char*)(&y), sizeof(y), 1, (FILE*)x) 

template <class T> inline int write_binary(FILE* fp, T& var)
  { return (fwrite(&var, sizeof(T), 1, fp)); }

template <class T> void destroy(T* pointer)
{ if (pointer) then
    delete pointer;
  pointer = 0;
}

template <class T> void destroy_array(T* pointer)
{ if (pointer) then
    delete [] pointer;
  pointer = 0;
}

template <class T> void START(T& obj)
{ fseek((FILE*)obj, 0, 0); }

template <class T> void END(T& obj)
{ fseek((FILE*)obj, 0, 2); }

// if two values are not identical, abort with a message. This is
// a "better" assert.
template <class T> void _TST(T& obj1, T& obj2, const char* msg,
                             const char* filename, const int line_nr)
{ if (!(obj1 == obj2)) then    // == more likely to be defined than !=
    _FE(msg, filename, line_nr);
}

inline void _CST(const unsigned long, const unsigned long, const char*, const int)
  { }

// The Sun does not provide itoa or ltoa
extern char* itoa(int, char*, int);
extern char* ltoa(long, char*, int);

// GNUC rather stupidly uses the presence of the FILE macro to indicate
// whether stdio has been read, so we put it back the way that we found
// it
#ifdef TEMPORARY_FILE_DEFINITION
#undef FILE
#endif

// provide a yield function for non pre-emptive multitasking systems
// (a la Windows)
inline void yield(void) { usleep(100000); }


#endif

