#include <stdio.h>
#include <string.h>
#include "DmErrno.h"

static char DmErrno_id[] = "$Header: DmErrno.c,v 1.3 97/03/20 12:25:35 oreilly Exp $";

/*
$Log:	DmErrno.c,v $
 * Revision 1.3  97/03/20  12:25:35  12:25:35  oreilly (Thomas C. O'Reilly)
 * *** empty log message ***
 * 
 * Revision 1.2  96/07/22  08:49:29  08:49:29  oreilly (Thomas C. O'Reilly)
 * Added RCS stuff
 * 
*/

static DmErrorMsg _dmErrorMsg[] = 
{
  
  EDM_GENERAL,
  "General DM error code",

  EDM_BADITEM,
  "Item doesn't exist",

  EDM_NOPROVIDER,
  "No provider for item",

  EDM_NOTCONSUMER,
  "Caller is not a consumer of item",

  EDM_TOOFAST,
  "Requested consumer period faster than provider period",

  EDM_PERIOD,
  "Requested consumer period not divisible by provider period",

  EDM_NOSPACE,
  "Data Manager couldn't alloc space",

  EDM_NOSEM,
  "Couldn't allocate semaphore",

  EDM_TOOBIG,
  "Size exceeds MAX_ITM_SIZE",

  EDM_PROVIDER_CONFLICT,
  "Item already has provider",

  EDM_NOTPROVIDER,
  "Caller is not the provider of this item",

  EDM_NOTCONNECTED,
  "Item is not dm_connect'ed",

  EDM_URGENT,
  "Item has been urgent_open()'d by another task",

  EDM_ISCONSUMER,
  "Calling task is already a consumer of this item",

  EDM_NAME_EXISTS,
  "Item with this name already exists",

  EDM_WRONG_SIZE,
  "Source, destination items of different size",

  EDM_NO_SIGNAL,
  "No signal handler specified",

  EDM_NO_SOCKET,
  "Cannot create socket",

  EDM_NETFAIL,
  "Network I/O failure",
				
  EDM_BAD_PKT,
  "Ill-formed packet on network",

  EDM_NO_GROUP,
  "Bad DM_Group ID, or item not a member of this group",

  EDM_IS_IN_GROUP,
  "Item is already a member of specified group",

  EDM_GROUP_SIZE,
  "Exceeded maximum items in group",

  EDM_ISCONNECTED,
  "Attempted to dm_connect() to an item that is already connected",

  EDM_CIRCULAR_CONNECT,
  "dm_connect() would be circular",

  EDM_BAD_TYPE,
  "Erroneous DM_Type",

  EDM_WRONG_TYPE,
  "Net packet has wrong size for item; possible name conflict",

  EDM_NO_DAEMON,
  "Data Manager daemon not running",

  EDM_SOCKET_BIND,
  "Cannot bind socket to address",

  EDM_NO_CFG_FILE,
  "No configuration file found",

  -1,
  NULL
};


void sprintDmError(Errno err, char *prefix, char *buf)
{
  char *ptr;
  DmErrorMsg *dmErrorMsg;
  char errorBuf[512];
  
  sprintf(buf, "%s: ", prefix);
  ptr = buf + strlen(buf);
  
  for (dmErrorMsg = _dmErrorMsg; dmErrorMsg->err != -1; dmErrorMsg++)
  {
    if (dmErrorMsg->err == err)
    {
      strcpy(ptr, dmErrorMsg->msg);
      return;
    }
  }
  
  /* If we get here, we couldn't find specified err */
  sprintf(errorBuf, "0x%x - %s", err, UNKNOWN_DM_ERRNO_MSG);
  strcpy(ptr, errorBuf);
  return;
}
