/*-- Rev Header - do NOT edit!
 *
 *  Filename : OwnError.h
 *  Purpose  : Eine Klasse, die das Bearbeiten von schwerwiegenden Fehlern erleichtert,
 *             die zum Abbruch des Programmes führen müssen
 *
 *  Program  : -
 *  Author   : Gerhard Müller
 *  Copyright: (c) by Gerhard Müller
 *  Creation : Sun Sep 12 20:44:12 1993
 *
 *  compile  :
 *
 *  Compile version  : 0.1
 *  Ext. Version     : 0.1
 *
 *  REVISION HISTORY
 *
 *  Date                     Comment
 *  ------------------------ -------------------------------------------------
 *  Fri Sep 17 00:56:20 1993 Created because I don't know how to use execptions
 *
 *-- REV_END --
 */

#ifndef OWNERROR_H
#define OWNERROR_H

	/*
	 * C-Includes, C-Definitionen
	 *
	 */

#define class _class
#define template _template

extern "C" {
#include <exec/types.h>
#include <exec/memory.h>
#include <clib/alib_protos.h>
#include <clib/alib_stdio_protos.h>
#include <inline/stubs.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#ifdef __OPTIMIZE__
#include <inline/exec.h>
#include <inline/dos.h>
#else
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#endif
}

#undef template
#undef class


	/*
	 * C++-Includes, C++-Definitionen
	 *
	 */

typedef void (*OwnError_err_func)(char *, LONG);

class OwnError
{
public:
	OwnError()
	{
		// nothing to do to construct, only clear our private things
		error_func=0;
		error_string=0;
		error_number=RETURN_OK;
	}

	void NotifyError(char *s,LONG nr=RETURN_FAIL, OwnError_err_func ef=0)
	{
		// we dont't have the ability to store more errors (yet), only
		// the last one is memorized

		if(error_string)
		{
			FreeVec(error_string);
		}

		error_func=ef;
		error_string=(char *)AllocVec(strlen(s)+1,MEMF_CLEAR);
		strcpy(error_string,s);
		error_number=nr;
	}

	LONG SetErrorNumber(LONG error)
	{
		LONG old_error=error_number;

		error_number=error;

		return old_error;
	}

	OwnError_err_func SetErrorFunc(OwnError_err_func ef)
	{
		OwnError_err_func old_error = error_func;

		error_func=ef;

		return old_error;
	}


	void SetErrorString(char *new_error_string)
	{
		if(error_string) FreeVec(error_string);

		error_string = new_error_string;
	}

	void Clear()
	{
		if(error_string) FreeVec(error_string);

		error_func=0;
		error_string=0;
		error_number=RETURN_OK;
	}

	void DosError(ULONG Err,char *def="")  // we can use AmigaDos-Error-Strings
	{
		if(error_string) FreeVec(error_string);

		// get String with dos.Fault()

		error_string=(char *)AllocVec(200,MEMF_CLEAR);

		Fault(Err,(unsigned char *)def,(unsigned char *)error_string,200);
	}

	~OwnError()
	{
		if(error_func) OwnError_err_func(error_string,error_number);
		if(error_string) FreeVec(error_string);
	}

	char *have_error_string(void)		// did we have an Error ? Yes ? Give the Err-Str.
	{
		return(error_string);
	}

	BOOL have_error(void)				// did we have an Error ? Yes ?
	{
		if(error_string || error_number) return(TRUE);
		else return(FALSE);
	}

	operator void*()					// allows if(err) { }
	{
		return (void *)have_error();
	}

private:

	OwnError_err_func error_func;
	char	*error_string;
	LONG	error_number;
};


extern OwnError err;

#endif
