btmux/autom4te.cache/
btmux/doc/.svn/
btmux/event/.svn/
btmux/game/.svn/
btmux/game/bin/.svn/
btmux/game/data/.svn/
btmux/game/logs/.svn/
btmux/game/maps/
btmux/game/maps/.svn/
btmux/game/maps/.svn/prop-base/
btmux/game/maps/.svn/props/
btmux/game/maps/.svn/text-base/
btmux/game/maps/.svn/wcprops/
btmux/game/mechs/
btmux/game/mechs/.svn/
btmux/game/mechs/.svn/prop-base/
btmux/game/mechs/.svn/props/
btmux/game/mechs/.svn/text-base/
btmux/game/mechs/.svn/wcprops/
btmux/game/text/.svn/
btmux/include/.svn/
btmux/misc/
btmux/misc/.svn/
btmux/misc/.svn/prop-base/
btmux/misc/.svn/props/
btmux/misc/.svn/text-base/
btmux/misc/.svn/wcprops/
btmux/python/
btmux/python/.svn/
btmux/python/.svn/prop-base/
btmux/python/.svn/props/
btmux/python/.svn/text-base/
btmux/python/.svn/wcprops/
btmux/src/.svn/prop-base/
btmux/src/.svn/props/
btmux/src/.svn/text-base/
btmux/src/.svn/wcprops/
btmux/src/hcode/.svn/
btmux/src/hcode/btech/
btmux/src/hcode/btech/.svn/
btmux/src/hcode/btech/.svn/prop-base/
btmux/src/hcode/btech/.svn/props/
btmux/src/hcode/btech/.svn/text-base/
btmux/src/hcode/btech/.svn/wcprops/
btmux/src/hcode/include/.svn/
/* 
 * $Id: myfifo.c,v 1.3 2005/08/08 09:43:05 murrayma Exp $
 *
 * Author: Markus Stenberg <fingon@iki.fi>
 *
 *  Copyright (c) 1996 Markus Stenberg
 *       All rights reserved
 *
 * Created: Sun Dec  1 11:46:18 1996 fingon
 * Last modified: Sun Dec  1 12:43:01 1996 fingon
 *
 */


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

/* A little shortcut to save me some typing */
#define PFOO   (*foo)

void check_fifo(myfifo ** foo)
{
    if (PFOO == NULL) {
	PFOO = (myfifo *) malloc(sizeof(myfifo));
	PFOO->first = NULL;
	PFOO->last = NULL;
	PFOO->count = 0;
    }
}

int myfifo_length(myfifo ** foo)
{
    check_fifo(foo);
    return PFOO->count;
}

void *myfifo_pop(myfifo ** foo)
{
    void *tmpd;
    myfifo_e *tmp;

    check_fifo(foo);
    tmp = PFOO->last;
    /* Is the list empty? */
    if (tmp != NULL) {
	/* Are we removeing the only element? */
	if (PFOO->first == PFOO->last) {
	    PFOO->first = NULL;
	    PFOO->last = NULL;
	} else
	    tmp->prev->next = NULL;
	PFOO->last = tmp->prev;
	/* Are we going down to only one element? */
	if (PFOO->last->prev == NULL)
	    PFOO->first = PFOO->last;
	PFOO->count--;
	tmpd = tmp->data;
	free(tmp);
	return tmpd;
    } else
	return NULL;
}

void myfifo_push(myfifo ** foo, void *data)
{
    myfifo_e *tmp;

    check_fifo(foo);
    tmp = (myfifo_e *) malloc(sizeof(myfifo_e));
    tmp->data = data;
    tmp->next = PFOO->first;
    tmp->prev = NULL;
    PFOO->count++;
    if (PFOO->first == NULL) {
	PFOO->first = tmp;
	PFOO->last = tmp;
    } else
	PFOO->first->prev = tmp;
    PFOO->first = tmp;
}

void myfifo_trav(myfifo ** foo, void (*func) ())
{
    myfifo_e *tmp;

    check_fifo(foo);
    for (tmp = PFOO->first; tmp != NULL; tmp = tmp->next)
	func(tmp->data);
}

void myfifo_trav_r(myfifo ** foo, void (*func) ())
{
    myfifo_e *tmp;

    check_fifo(foo);
    for (tmp = PFOO->last; tmp != NULL; tmp = tmp->prev)
	func(tmp->data);
}