/*- FUNCS.C ----------------------------------------------------------------*/
/*
 * Modified 2010/08/04 by Christopher Bazley
 */

/*- terminate the program reporting an error -------------------------------*/

#include "structs.h"
#include "bsp.h"

#ifdef STDC_HEADERS
# include <stdio.h>
#endif /* STDC_HEADERS */

#ifdef RISCOS
#include "swis.h"
#endif /* RISCOS */

enum
{
  BUF_SIZE = 1024
};

void ProgError(const char *errstr, ...)
{
   va_list args;

   va_start( args, errstr);
   fprintf(stderr, "\nProgram Error: *** ");
   vfprintf( stderr, errstr, args);
   fprintf(stderr, " ***\n");
   va_end( args);
#ifdef STDC_HEADERS
   if (unlinkwad) remove(unlinkwad);
#endif /* STDC_HEADERS */
   exit(EXIT_FAILURE);
}

/* Print stuff if verbose output */

int verbosity;

void Verbose(const char *errstr, ...)
{
   va_list args;

   if (!verbosity) return;

   va_start( args, errstr);
   vprintf(errstr, args);
   va_end( args);
}

#ifndef WITH_DMALLOC
/*- allocate memory with error checking ------------------------------------*/
#ifdef FORTIFY
void* GetMemoryF(size_t size, const char *file, unsigned long line)
{
   void *ret = Fortify_malloc(size, file, line);
#else /* FORTIFY */
void* GetMemory(size_t size)
{
   void *ret = malloc( size);
#endif /* FORTIFY */
   if (!ret)
      ProgError( "out of memory (cannot allocate %zu bytes)", size);
   return ret;
}

/*- reallocate memory with error checking ----------------------------------*/
#ifdef FORTIFY /* FORTIFY */
void* ResizeMemoryF(void *old, size_t size, const char *file, unsigned long line)
{
   void *ret = Fortify_realloc(old, size, file, line);
#else /* FORTIFY */
void* ResizeMemory(void *old, size_t size)
{
   void *ret = realloc( old, size);
#endif /* FORTIFY */
   if (!ret)
      ProgError( "out of memory (cannot reallocate %zu bytes)", size);
   return ret;
}

#endif
/*--------------------------------------------------------------------------*/

