/*
 * 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:  cache_list.c
 *
 * Purpose:  This source file contains the definitions to the interface
 * for managing a list of cache objects as defined in the header file.
 * 
 */


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

#include "cache_list.h"
#include "resolver.h"
#include "globals.h"


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


void append_cache_list (Cache_List* list, void* obj)
{
    Cache_Node * 		new_node;

    if (list == NULL) return;
    if (obj == NULL) return;
    if ((new_node = malloc(sizeof(Cache_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;
}


unsigned char* find_cache_obj (Cache_List* list, int address)
{
    unsigned int 	key;
    Cache_Node		*current;

    if (list == NULL) return NULL;

    current = list->head;
    while(current != NULL) {
        key = ((Name_Obj*)(current->obj))->address;
	if (key == address) {
	    return ((Name_Obj*)(current->obj))->name;
	} else {
	    current = current->next;
	}
    }
    return NULL;
}

void* delete_cache_list_head (Cache_List* list)
{
    void * 		obj;
    Cache_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_cache_list_node (Cache_List* list, Cache_Node* node)
{
    void * 		obj;
    Cache_Node * 		current;
    Cache_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_cache_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_cache_list (Cache_List* list)
{
    while (list->head != NULL) {
        free(list->head->obj);
        delete_cache_list_head(list);
    }
}


syntax highlighted by Code2HTML, v. 0.9.1