/*
 * 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:  network_list.c
 *
 * Purpose:  This source file defines the interface for a custom list
 * used to handle network objects and declared in network.h
 *
 */


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

#include "network.h"
#include "network_list.h"
#include "globals.h"


void init_net_list (Net_List* list)
{
    list->head = NULL;
    list->tail = NULL;
    list->count = 0;
    list->size = 0;
}

void append_net_list (Net_List* list, void* obj)
{
    Net_Node * 			new_node;

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

    if ((new_node = malloc(sizeof(Net_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);

    list->size += ((Net_Obj*) obj)->len;
    return;
}

void* delete_net_list_head (Net_List* list)
{
    void * 			obj;
    Net_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);
    list->size -= ((Net_Obj*) obj)->len;
    return obj;
}

void* delete_net_list_node (Net_List* list, Net_Node* node)
{
    void * 			obj;
    Net_Node * 			current;
    Net_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_net_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);
            list->size -= ((Net_Obj*) obj)->len;
            return obj;
        }
        last = current;
        current = current->next;
    }
    return NULL;
}

void empty_net_list (Net_List* list)
{
    while (list->head != NULL) {
        free(list->head->obj);
        delete_net_list_head(list);
    }
    return;
}


syntax highlighted by Code2HTML, v. 0.9.1