/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: html_stack.cxx,v 1.3 1998/08/07 19:11:28 schmitzj Exp $)
 * 
 * Copyright (C) 1998 Jürgen Schmitz
 *
 * HTML Parser system
 * (c) 1998 by Jürgen Schmitz - all rights reserved
 * THIS IS NOT GPL OR SOMETHING OTHER FREE LICENSE!!
 * it may only be used together with Xclasses!
 *-----------------------------------------------------------------------
 */

#include <stdlib.h>
#include <string.h>
#include "include/html_stack.h"

html_stack::html_stack(void)
{
	stack=NULL;
	keybuffer=NULL;
}
html_stack::~html_stack()
{
	if (keybuffer) free(keybuffer);

	struct items *i1=stack,*i2;
	while (i1)
	{
		i2=i1->next;
		free(i1->key);
		delete i1;
		i1=i2;
	}
}
void html_stack::Push(char *key)
{
	struct items *i=new items;
	if (i)
	{
		i->next=stack;
		i->key=strdup(key);
		stack=i;
	}
}
char *html_stack::Pull(void)
{
	char *r=NULL;
	if (stack)
	{
		if (keybuffer) free(keybuffer);
		struct items *i=stack;
		keybuffer=i->key;
		stack=i->next;
		delete i;
		r=keybuffer;
	}
	return r;
}
char *html_stack::Read(void)
{
	char *r=NULL;
	if (stack)
	{
		if (keybuffer) free(keybuffer);
		keybuffer=strdup(stack->key);
		r=keybuffer;
	}
	return r;
}
int html_stack::Exists(char *key,char *stop)
{
	struct items *i1=stack;
	int r=0;
	while (i1)
	{
		if (stop && !strcmp(stop,i1->key))
		{
			r=0;
			break;
		}
		if (!strcmp(key,i1->key))
		{
			r=1;
			break;
		}
		i1=i1->next;
	}
	return r;
}
int html_stack::Count(char *key,char *stop)
{
	struct items *i1=stack;
	int r=0;
	while (i1)
	{
		if (stop && !strcmp(stop,i1->key))
		{
			break;
		}
		if (!strcmp(key,i1->key))
		{
			r++;
		}
		i1=i1->next;
	}
	return r;
}
void html_stack::PullUntil(char *key)
{
	while (stack)
	{
		if (!strcmp(key,Pull()))
		{
			break;
		}
	}
}
void html_stack::PullBefore(char *key)
{
	while (stack)
	{
		if (!strcmp(key,Read()))
		{
			break;
		}
		Pull();
	}
}

// -------------------------------------------------------------------

html_valuestack::html_valuestack(void)
{
	stack=NULL;
}
html_valuestack::~html_valuestack()
{
	struct items *i1=stack,*i2;
	while (i1)
	{
		i2=i1->next;
		delete i1;
		i1=i2;
	}
}
void html_valuestack::Push(unsigned int key)
{
	struct items *i=new items;
	if (i)
	{
		i->next=stack;
		i->key=key;
		stack=i;
	}
}
unsigned int html_valuestack::Pull(void)
{
	unsigned int r=0;
	if (stack)
	{
		struct items *i=stack;
		r=i->key;
		stack=i->next;
		delete i;
	}
	return r;
}
unsigned int html_valuestack::Read(void)
{
	unsigned int r=0;
	if (stack)
	{
		r=stack->key;
	}
	return r;
}
