/*
 * $Log: object.c,v $
 * Revision 1.11  2001/01/30 06:16:18  root
 * #
 *
 * Revision 1.10  2000/12/01 04:42:10  root
 * #
 *
 * Revision 1.9  2000/11/23 23:45:19  root
 * #
 *
 * Revision 1.8  2000/10/07 18:30:03  root
 * #
 *
 * Revision 1.7  2000/09/24 01:58:26  root
 * #
 *
 * Revision 1.6  2000/09/24 01:58:21  root
 * #
 *
 * Revision 1.5  2000/09/24 01:46:04  root
 * #
 *
 * Revision 1.4  2000/09/24 01:44:47  root
 * #
 *
 * Revision 1.3  2000/09/19 03:53:27  root
 * #
 *
 * Revision 1.2  2000/09/13 15:13:40  root
 * #
 *
 * Revision 1.1  2000/09/13 14:36:48  root
 * #
 *
 * Revision 1.3  2000/09/03 03:21:53  root
 * #
 *
 * Revision 1.1  2000/09/03 02:03:28  root
 * #
 *
 * Revision 1.2  2000/09/02 17:15:27  root
 * #
 *
 * Revision 1.1  2000/09/02 17:14:58  root
 * #
 *
 * Revision 1.1  1997/11/20 00:13:19  root
 * Initial revision
 *
 * Revision 1.1  1997/10/26 23:59:20  root
 * Initial revision
 *
 * Revision 6.0  1997/07/04  00:32:42  root
 * \#makecheckin
 *
 * Revision 5.0  1997/07/03 15:05:48  root
 * \#makecheckin
 *
 * Revision 4.0  1997/07/01 11:58:59  root
 * \#makecheckin
 *
 * Revision 3.0  1997/06/23 12:51:50  root
 * \#makecheckin
 *
 * Revision 2.0  1997/06/23 09:04:44  root
 * \#makecheckin
 *
 * Revision 1.8  1997/06/18 15:15:32  root
 * \#makecheckin
 *
 * Revision 1.7  1997/06/15 23:47:49  root
 * \#makecheckin
 *
 */

static char rcsid[] =
    "$Id: object.c,v 1.11 2001/01/30 06:16:18 root Exp $";


#include "project.h"

BOTH void Obj_DefaultDestructor(Object * object)
{
  if (object == NULL) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Invalid object 		Op: destruct\n");
    abort();
  }
#ifdef MEM_DEBUG
  my_free(object);
#else
  free(object);
#endif
  return;

}

BOTH void Obj_DefaultCreator(Object * object)
{
}

BOTH void Do_Obj_CreateList(List * list)
{

  list->head = NULL;
  list->tail = NULL;

}


BOTH void Do_Obj_Init(Object * object)
{
  object->next = NULL;
  object->prev = NULL;
  object->destructor = (Destructor(Object)) Obj_DefaultDestructor;

}



BOTH void Do_Obj_Delete(Object * object)
{

  if (object == NULL) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Invalid object 		Op: delete\n");
    abort();
  }
  if (((object->next) != NULL) || ((object->prev) != NULL)) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Object is still in list	Op: delete\n");
    abort();
  }
  (*object->destructor) (object);


}



BOTH void Do_Obj_Remove(List * list, Object * object)
{

  if (object == NULL) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Invalid object 		Op: remove\n");
    abort();
  }
  if (object->prev == NULL) {
    list->head = (Object *) object->next;
  } else {
    ((Object *) object->prev)->next = object->next;
  }

  if (object->next == NULL) {
    list->tail = (Object *) object->prev;
  } else {
    ((Object *) object->next)->prev = object->prev;
  }

  object->prev = NULL;
  object->next = NULL;

}


BOTH void Do_Obj_Insert(List * list, Object * object, int action)
{

  if (object == NULL) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Invalid object 		Op: insert\n");
    abort();
  }
  if (((object->next) != NULL) || ((object->prev) != NULL)) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Object allready in list	Op: insert\n");
    abort();
  }
  switch (action) {
  case ObjInsertHead:
    if (list->head == NULL) {
      object->prev = NULL;
      object->next = list->head;
      list->head = object;
    } else {
      object->prev = NULL;
      object->next = list->head;
      list->head->prev = object;
      list->head = object;
    }
    if (list->tail == NULL) {
      list->tail = object;
    }
    break;
  case ObjInsertTail:
    if (list->tail == NULL) {
      object->next = NULL;
      object->prev = list->tail;
      list->tail = object;
    } else {
      object->next = NULL;
      object->prev = list->tail;
      list->tail->next = object;
      list->tail = object;
    }
    if (list->head == NULL) {
      list->head = object;
    }
    break;
  default:
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Invalid action 		Op: insert\n");
    abort();
  }
}


BOTH void Do_Obj_InsertAfter(List * list, Object * p_object,
                             Object * object)
{

  if (object == NULL) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Invalid object 		Op: insertafter\n");
    abort();
  }
  if (p_object == NULL) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Invalid p_object 		Op: insertafter\n");
    abort();
  }
  if (((object->next) != NULL) || ((object->prev) != NULL)) {
    fprintf(stderr, "Object package\n");
    fprintf(stderr, "Error: Object allready in list	Op: insertafter\n");
    abort();
  }
  object->next = p_object->next;
  p_object->next = object;
  object->prev = p_object;


  if (object->next == NULL) {
    if (list->tail != p_object) {
      fprintf(stderr, "Object package\n");
      fprintf(stderr,
              "Error: Object p_object not in list	Op: insertafter\n");
      abort();

    }
    list->tail = object;
  }
}





BOTH void Do_Obj_DeleteList(List * list)
{
  Object *object;

  while ((object = list->head) != NULL) {
    Obj_Remove(list, object);
    Obj_Delete(object);
  }


}
BOTH void Obj_ListDestructor(List * list)
{
  Obj_DeleteList(list);
#ifdef MEM_DEBUG
  my_free(list);
#else
  free(list);
#endif
}
