#ifndef XPKMASTER_FAULT_C
#define XPKMASTER_FAULT_C

/* Routinesheader

	Name:		fault.c
	Main:		xpkmaster
	Versionstring:	$VER: fault.c 1.6 (26.03.1998)
	Author:		SDI
	Distribution:	Freeware
	Description:	Error message generators

 1.0   05.10.96 : first real version
 1.1   27.12.96 : added parts of Fault functions
 1.2   28.12.96 : finished the two Fault functions
 1.3   31.03.97 : added new error (XPKERR_UNKNOWN);
 1.4   02.04.97 : renamed to fault.c, removed geterror
 1.5   21.02.98 : uses new style register definition
 1.6   26.03.98 : some optimizations and a bug fix
*/

#include <proto/exec.h>
#include <proto/dos.h>
#include "xpkmaster.h"
#include "texts.h"

ASM(BOOL) LIBXpkPrintFault(REG(d0, LONG code), REG(a0, STRPTR header))
{
  STRPTR a[2], fmt = "%s: %s\n";

  if(code > 0 || code < MINERROR)
    code = XPKERR_UNKNOWN;

  a[1] = XpkErrs[-code];

  if((a[code = 0] = header))
  {
    ++code; fmt += 4;
  }

  if(VPrintf(fmt, &a[code]) == -1)
    return 0;  /* error */
  else
    return -1; /* ok */
}

ASM(ULONG) LIBXpkFault(REG(d0, LONG code), REG(a0, STRPTR header),
	REG(a1, STRPTR buffer), REG(d1, ULONG size))
{
  ULONG ssize = 0;

  if(size > 1 && buffer)
  {
    STRPTR string;

    if(code > 0 || code < MINERROR)
      code = XPKERR_UNKNOWN;

    string = XpkErrs[-code];

    if((ssize = strlen(string)) > --size) /* remove 1 for 0-byte from size */
      ssize = size;
    size -= ssize;

    if(header && (code = strlen(header)) + 2 <= size)
    {
      CopyMem(header, buffer, code);
      buffer[code++] = ':';
      buffer[code++] = ' ';
      buffer += code;
    }
    else
      code = 0;

    CopyMem(string, buffer, ssize);
    buffer[ssize] = 0;
    ssize += code;
  }

  return ssize;
}

#endif /* XPKMASTER_FAULT_C */
