/*****************************************************************************
 * EXTERROR.C - Extended error message handling.
 ****************************************************************************/

#include "gemfintl.h"
#include "exterror.h"
#include <string.h>

#define MAX_EXT_TABLES	8

static _Err_tab GFAR internal_errors[] = {
	gfErr_no_memory,		"Out of memory",
	gfErr_vdi_handle,		"Out of VDI handles",
	gfErr_parameter_range,	"Parameter value out of range",
	gfErr_parameter_null,	"Invalid NULL pointer parameter",
	gfErr_resource_in_use,	"Required internal resource in use",
	gfErr_wrong_type,		"Specified object is the wrong type",
	gfErr_option_invalid,	"Invalid option requested",
	gfErr_action_invalid,	"Invalid action requested",
	gfErr_object_too_small, "Specified object is too small",
	gfErr_object_too_big,	"Specified object is too big",
	0,						NULL
};

static _Err_tab *extmsg_tables[MAX_EXT_TABLES] = {internal_errors};
static char 	nullstr[] = "";

/*----------------------------------------------------------------------------
 * exterrset - Install or remove an application-specific error msg table.
 *--------------------------------------------------------------------------*/

int exterrset(ptab, install)
	_Err_tab	*ptab;
	int 	  install;
{
	register short		tabidx;
	register _Err_tab **pptab = extmsg_tables;

	for (tabidx = 0; tabidx < MAX_EXT_TABLES; ++tabidx, ++pptab) {
		if (install) {
			if (*pptab == NULL) {
				*pptab = ptab;
				return E_OK;
			}
		} else {
			if (*pptab == ptab) {
				*pptab = NULL;
				return E_OK;
			}
		}
	}

	return ERROR;
}

/*----------------------------------------------------------------------------
 * exterror - like strerror(), but looks for application-specific msg first.
 *--------------------------------------------------------------------------*/

char *exterror(err)
	register int err;
{
	register short		tabidx;
	register _Err_tab *ptab;
	register char	  *themsg;

	for (tabidx = 0; tabidx < MAX_EXT_TABLES; ++tabidx) {
		for (ptab = extmsg_tables[tabidx]; ptab && ptab->code; ++ptab) {
			if (ptab->code == err) {
				themsg = ptab->msg;
				goto RETURN_IT;
			}
		}
	}

	if (err == 0) {
		themsg = nullstr;
	} else {
		themsg = strerror(err);
	}

RETURN_IT:

	if (themsg == NULL) {
		themsg = nullstr;
	}

	return themsg;
}


