
#include <stdarg.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include "error.h"
#include "_error.h"
//#include "file_lib.h"
//#include "util_lib.h"
#include "develop.h"
#include "types.h"

static boolean InError=false;
#if 0
static boolean SoftwareErrorStarted=false;
static int32   SoftwareErrorHandle=0;
#endif

//===========================================================================
//
//  Error Functions
//
//===========================================================================

//=================
//
//  Error
//
//  For abnormal program terminations (ERRORS!)
//
//=================

void Error (int8 *error, ...)
   {
	va_list	argptr;

   if (InError==true)
      {
      return;
      }
   else
      {
      InError=true;
      }

   ShutDown ();
   TextMode ();

   va_start ( argptr,error );
   vprintf ( error,argptr );
   va_end ( argptr );

	printf ("\n");

	exit (1);
   }



#if (SOFTERROR == 1)

/*
=================
=
= OpenSoftwareError
=
=================
*/
void OpenSoftwareError ( void )
   {
   if ( SofwareErrorStarted == false )
      {
      // The first time lets destroy the old file
      SoftwareErrorHandle = SafeOpenWriteText ( SOFTWAREERRORFILE );
      SoftwareErrorStarted = true;
      }
   else
      {
      // all subsequent times lets append to the file
      SoftwareErrorHandle = SafeOpenAppendText ( SOFTWAREERRORFILE );
      }
   }

/*
=================
=
= CloseSoftwareError
=
=================
*/

void CloseSoftwareError ( void )
   {
   SafeClose( SoftwareErrorHandle );
   }

/*
=================
=
= SoftwareError
=
=================
*/

void SoftwareError (int8 *error, ...)
   {
	va_list	argptr;
   int8 str[400];

   va_start ( argptr, error );
   vsprintf ( &str[0], error, argptr );
   va_end ( argptr );
   OpenSoftwareError();
   SafeWrite( SoftwareErrorHandle, &str[0], strlen(&str[0]) );
   CloseSoftwareError();
   }

#endif
