/****************************************************************************
 * This file was derived from the CNCL library by the Pablo research group.
 *
 * The complete CNCL library is available at:
 *       http://www.comnets.rwth-aachen.de/doc/cncl.html
 *
 * The CNCL copyright notice is included here:
 *
 * |_|_|_  |_|_    |_    |_|_|_  |_               C O M M U N I C A T I O N
 *|_       |_  |_  |_  |_        |_                         N E T W O R K S
 *|_       |_  |_  |_  |_        |_                               C L A S S
 * |_|_|_  |_    |_|_    |_|_|_  |_|_|_|_                     L I B R A R Y
 *
 *
 *
 * Copyright (C) 1992-1998   Communication Networks
 *                           Aachen University of Technology
 *                           D-52056 Aachen
 *                           Germany
 *                           Email: cncl-adm@comnets.rwth-aachen.de
 *
 *
 * This library is free  software; you can  redistribute it and/or modify
 * it  under   the terms  of the GNU   Library  General Public License as
 * published by the  Free Software Foundation;  either  version 2 of  the
 * License, or  (at  your option)  any  later  version.  This  library is
 * distributed  in the  hope  that  it  will  be useful,  but WITHOUT ANY
 * WARRANTY;  without even the   implied  warranty of MERCHANTABILITY  or
 * FITNESS FOR A PARTICULAR PURPOSE.   See the GNU Library General Public
 * Library General Public License along with  this library; if not, write
 * to the Free Software Foundation,  675  Mass Ave, Cambridge, MA  02139,
 * USA.
 ****************************************************************************/

/*
 * Fuzzy Rules Compiler
 *
 * $Id: fuzzysubs.c,v 1.2 1999/07/01 13:32:53 aydt Exp $
 *
 * fuzzysubs.c - list management routines (code)
 */

#include "fuzzysubs.h"
#include <string.h>
#include <malloc.h>

/* Add a node to the tail of the list */
void add(struct List *l, struct Node *n)
{
    if (l->head == NULL)
	l->head = n;
    else 
	l->tail->next = n;

    n->next = NULL;
    l->tail = n;
}

/* Find a named node in the list */
struct Node *findname(struct List *l, char *name)
{
    struct Node *n = l->head;
    
    while (n && strcmp((char *)n->data, name)) n = n->next;
    
    return(n);
}

/* Create a new node */
struct Node *newnode(long size, void *data)
{
    struct Node *n = (struct Node *)calloc(1, size);
    n->data = data;
    return(n);
}

/* Add a named node to the list, but only if it does not yet exist */
struct Node *addnewname(struct List *l, long size, char *name)
{
    struct Node *n = NULL;
    
    if (!findname(l, name))
    {
	n = newnode(size, strdup(name));
	add(l, n);
    }
    
    return(n);
}

/* Free all nodes in the list */
void freelist(struct List *l)
{
    struct Node *n = l->head;
    
    while (n)
    {
	struct Node *next = n->next;
	free(n);
	n = next;
    }
    l->head = NULL;
}

/* Free all named nodes in the list */
void freenamelist(struct List *l)
{
    struct Node *n = l->head;
    
    while (n)
    {
	struct Node *next = n->next;
	free(n->data);
	free(n);
	n = next;
    }
    l->head = NULL;
}
