/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of SanKit -- A multiplatform set of C code
 * for reading and writing PGN formatted chess games. In addition
 * it can be used as an engine to determine in real time if proposed
 * moves are legal or not. It appears to generate near perfect PGN
 * from incorrectly formatted files.
 *
 * Copyright 1994 Steven J. Edwards All Rights Reserved.
 * Copyright 2009-2011 David Hoerl 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 David Hoerl ''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 David Hoerl 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.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*>>> sanslm.c: SAN project string list management */

/* Revised: 1993.05.16 */

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

#include "sandef.h"
#include "sanvar.h"

#include "sanslm.h"
#include "sanutl.h"

/*--> SANSLMMember: return string's member position, if any */
nonstatic
sleptrT SANSLMMember(slaptrT slaptr, charptrT s)
{
	sleptrT sleptr0, sleptr1;

	sleptr0 = slaptr->sla_head;
	sleptr1 = NULL;

	while((sleptr0 != NULL) && (sleptr1 == NULL)) {
		if(strcmp(sleptr0->sle_s, s) == 0) {
			sleptr1 = sleptr0;
		} else {
			sleptr0 = sleptr0->sle_next;
		}
	}

	return sleptr1;
}

/*--> SANSLMAppend: append a string to the end of a list */
nonstatic
void SANSLMAppend(slaptrT slaptr, charptrT s)
{
	sleptrT sleptr;

	sleptr = (sleptrT) SANMemGrab(sizeof(sleT));
	sleptr->sle_s = SANStrGrab(s);
	sleptr->sle_prev = slaptr->sla_tail;
	sleptr->sle_next = NULL;

	if(slaptr->sla_tail != NULL) {
		slaptr->sla_tail->sle_next = sleptr;
	} else {
		slaptr->sla_head = sleptr;
	}
	slaptr->sla_tail = sleptr;

	return;
}

/*--> SANSLMReleaseElement: release storage for a string list element */
nonstatic
void SANSLMReleaseElement(slaptrT slaptr, sleptrT sleptr)
{
	if(sleptr->sle_prev == NULL) {
		slaptr->sla_head = sleptr->sle_next;
	} else {
		sleptr->sle_prev->sle_next = sleptr->sle_next;
	}

	if(sleptr->sle_next == NULL) {
		slaptr->sla_tail = sleptr->sle_prev;
	} else {
		sleptr->sle_next->sle_prev = sleptr->sle_prev;
	}

	SANMemFree(sleptr->sle_s);
	SANMemFree(sleptr);

	return;
}

/*--> SANSLMInitAnchor: initialize a string list anchor */
nonstatic
void SANSLMInitAnchor(slaptrT slaptr)
{
	slaptr->sla_head = slaptr->sla_tail = NULL;

	return;
}

/*--> SANSLMReleaseAnchor: release storage for a string list anchor */
nonstatic
void SANSLMReleaseAnchor(slaptrT slaptr)
{
	sleptrT sleptr;

	while((sleptr = slaptr->sla_head) != NULL) {
		SANSLMReleaseElement(slaptr, sleptr);
	}


	return;
}

/*<<< sanslm.c: EOF */
