/***************************************************************************
** dlist.h:  Contains definitions for routines implementing a doubly-linked
**           list data structure.
**
**      Unpublished Confidential Information of Hewlett-Packard
**      Company. Do not disclose.  Copyright (c) Hewlett-Packard
**      Company 1995. All rights reserved
** 
** The contents of this software are proprietary and confidential to
** the Hewlett- Packard Company, and are limited in distribution to
** those with a direct need to know.  Individuals having access to
** this software are responsible for maintaining the confidentiality
** of the content and for keeping the software secure when not in use.
** Transfer to any party is strictly forbidden other than as expressly
** permitted in writing by Hewlett-Packard Company.  Unauthorized
** transfer to or possession by any unauthorized party may be a
** criminal offense.
**
**                  RESTRICTED RIGHTS LEGEND
**
**      Use,  duplication,  or disclosure by the U. S. Government is
**      subject to restrictions as set forth in subparagraph (c) (1)
**      (ii) in the Rights in Technical Data and Computer Software
**      clause in DFARS 252.227-7013 or any other successor clause.
**
**                      HEWLETT-PACKARD COMPANY
**                      3000 Hanover St.
**                      Palo Alto, CA  94304 U.S.A.
**
**      Rights for non-DOD U.S. Government Departments and Agencies
**      are as set forth in FAR 52.227-19 (c) (1,2) or any other
**      successor clause.
**
** Programmer:  Jon Hirota
** Language:    ANSI C but can be compiled under Kerningham & Ritchie C
***************************************************************************/

#ifndef _DLIST_H_
#define _DLIST_H_

#ifdef __cplusplus
extern "C" {
#endif

/*
** Constants for dlist error values
*/

#define DLERR_NOERR     0               /* No error */
#define DLERR_NOMEM     1               /* Malloc failure */
#define DLERR_NULL      2               /* Invalid NULL arg. to function */
#define DLERR_APPLF     3               /* Apply function failed */
#define DLERR_NOTFND    4               /* Item not found */

/*
** The dlist data types
*/

typedef struct dlist_node
{
   struct dlist_node    *next;
   struct dlist_node    *prev;
   void                 *data;
} dlist_node;

#ifdef __STDC__                 /* ANSI C */

typedef struct dlist_header
{
   dlist_node           *head;
   dlist_node           *tail;
   int                  (*cmp_func)(void *,void *);
   int                  count;
   int                  dlist_err;
} dlist;

#else                           /* Kerningham and Ritchie C */

typedef struct dlist_header
{
   dlist_node           *head;
   dlist_node           *tail;
   int                  (*cmp_func)();
   int                  count;
   int                  dlist_err;
} dlist;

#endif                          /* __STDC__ */

/*
**                      Brief function descriptions:
**
** dlistCreate:         creates a doubly-linked list structure
** dlistDestroy:        destroys and deallocates memory of a dlist.
** dlistPrepend:        adds a data item to the beginning of a dlist.
** dlistInsert:         adds a data item "in order" to a dlist.
** dlistAppend:         adds a data item to the end of a dlist.
** dlistDelete:         deletes a node from a dlist.
** dlistDatadelete:     finds and deletes a data item from a dlist.
** dlistFind:           finds a data item in a dlist.
** dlistApply:          applies a function to each data item in a dlist.
** dlistHead:           returns the head node of a dlist.
** dlistTail:           returns the tail node of a dlist.
** dlistCount:          returns the number of items in a dlist.
** dlistError:          returns the dlist's error value.
*/

/*
** The dlist function prototypes
*/

#ifdef __STDC__         /* ANSI C */

dlist *                 dlistCreate(int (*cmp_func)(void *,void *));
int                     dlistDestroy(dlist *list);
int                     dlistPrepend(dlist *list, void *datap);
int                     dlistInsert(dlist *list, void *datap);
int                     dlistAppend(dlist *list, void *datap);
void *                  dlistDelete(dlist *list, dlist_node *node);
void *                  dlistDatadelete(dlist *list, void *datap);
dlist_node *            dlistFind(dlist *list, void *datap);
int                     dlistApply(dlist *list, int (*apply_func)(void *),
                                   int failsafe);

#else                   /* Kerningham and Ritchie C */

dlist *                 dlistCreate();
int                     dlistDestroy();
int                     dlistPrepend();
int                     dlistInsert();
int                     dlistAppend();
void *                  dlistDelete();
void *                  dlistDatadelete();
dlist_node *            dlistFind();
int                     dlistApply();

#endif                  /* __STDC__ */

/*
** The following functions are implemented as macros
*/

#define dlistHead(list)         (((list) == (dlist *) NULL) ? \
                                   (dlist_node *) NULL : \
                                   ((list)->head))

#define dlistTail(list)         (((list) == (dlist *) NULL) ? \
                                   (dlist_node *) NULL : \
                                   ((list)->tail))

#define dlistCount(list)        (((list) == (dlist *) NULL) ? \
                                   0 : ((list)->count))

#define dlistError(list)        (((list) == (dlist *) NULL) ? \
                                   DLERR_NULL : ((list)->dlist_err))

#ifdef __cplusplus
}
#endif

#endif          /* _DLIST_H_ */
