/*
 * Copyright (c) 2001 Fenris, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * File:  display_list.c
 *
 * Purpose:  This source file contains the definitions to the interface
 * for managing a list of display objects as defined in the header file.
 * 
 */


#include <stdio.h>
#include <stdlib.h>

#include "display.h"
#include "display_list.h"
#include "globals.h"


void init_dsp_list (Dsp_List* list)
{
    list->head = NULL;
    list->tail = NULL;
    list->count = 0;
    return;
}


void append_dsp_list (Dsp_List* list, void* obj)
{
    Dsp_Node * 		new_node;

    if (list == NULL) return;
    if (obj == NULL) return;
    if ((new_node = malloc(sizeof(Dsp_Node))) == NULL)
        die("malloc");

    new_node->obj = obj;
    new_node->next = NULL;

    if (list->head == NULL) {
        list->head = new_node;
        list->tail = list->head;
    } else {
        list->tail->next = new_node;
        list->tail = new_node; 
    }
    ++(list->count);
    return;
}


void insert_src_dsp_obj (Dsp_List* list, void* obj)
{
    Dsp_Node * 		new_node;
    register unsigned int key;
    Dsp_Node * 		current;
    Dsp_Node * 		last;

    if (list == NULL) return;
    if (obj == NULL) return;

    if ((new_node = malloc(sizeof(Dsp_Node))) == NULL)
        die("malloc");

    new_node->obj = obj;
    new_node->next = NULL;

    if (list->head == NULL) {
        list->head = new_node;
        list->tail = list->head;
    } else {
        key = ((Dsp_Obj*) obj)->src;

        if (key < ((Dsp_Obj*)list->head->obj)->src) {
            new_node->next = list->head;
            list->head = new_node;
        } else {
            last = list->head;
            current = last->next;
            for(;;) {
                if (current == NULL) {
                    last->next = new_node;
                    list->tail = new_node;
                    break;
                } else {
                    if (key < ((Dsp_Obj*)current->obj)->src) {
                        last->next = new_node;
                        new_node->next = current;
                        break; 
                    } else {
                        last = last->next;
                        current = last->next;
                    }
                }
            }
        }
    }
    ++(list->count);
    return;
}


void insert_dst_dsp_obj (Dsp_List* list, void* obj)
{
    Dsp_Node * 		new_node;
    register unsigned int key;
    Dsp_Node * 		current;
    Dsp_Node * 		last;

    if (list == NULL) return;
    if (obj == NULL) return;

    if ((new_node = malloc(sizeof(Dsp_Node))) == NULL)
        die("malloc");

    new_node->obj = obj;
    new_node->next = NULL;

    if (list->head == NULL) {
        list->head = new_node;
        list->tail = list->head;
    } else {
        key = ((Dsp_Obj*) obj)->dst;

        if (key < ((Dsp_Obj*)list->head->obj)->dst) {
            new_node->next = list->head;
            list->head = new_node;
        } else {
            last = list->head;
            current = last->next;
            for(;;) {
                if (current == NULL) {
                    last->next = new_node;
                    list->tail = new_node;
                    break;
                } else {
                    if (key < ((Dsp_Obj*)current->obj)->dst) {
                        last->next = new_node;
                        new_node->next = current;
                        break;
                    } else {
                        last = last->next;
                        current = last->next;
                    }
                }
            }
        }
    }
    ++(list->count);
    return;
}


void* delete_dsp_list_head (Dsp_List* list)
{
    void * 		obj;
    Dsp_Node * 		temp;

    if (list == NULL) return NULL;
    if (list->head == NULL) return NULL;
    if (list->head == list->tail) {
        obj = list->head->obj;
        list->tail = NULL;
        free(list->head);
        list->head = NULL;
    } else {
        obj = list->head->obj;
        temp = list->head;
        list->head = list->head->next;
        free(temp);
    }
    --(list->count);
    return obj;
}


void* delete_dsp_list_node (Dsp_List* list, Dsp_Node* node)
{
    void * 		obj;
    Dsp_Node * 		current;
    Dsp_Node * 		last;

    if (list == NULL) return NULL;
    if (node == NULL) return NULL;
    if (list->head == NULL) return NULL;

    current = list->head;
    if (current == node) {
        obj = delete_dsp_list_head(list);
        return obj;
    }
    last = current;
    current = current->next;

    while(current != NULL) {
        if (current == node) {
            if (current == list->tail) {
                last->next = NULL;
                list->tail = last;
                obj = current->obj;
                free(current);
            } else {

                last->next = current->next;
                obj = current->obj;
                free(current);
            } 
            --(list->count);
            return obj;
        }
        last = current;
        current = current->next;
    }
    return NULL;
}


void empty_dsp_list (Dsp_List* list)
{
    while (list->head != NULL) {
        free(list->head->obj);
        delete_dsp_list_head(list);
    }
}


syntax highlighted by Code2HTML, v. 0.9.1