
/* OpenWebSpider
 *
 *  Authors:     Stefano Alimonti AND Stefano Fantin
 *  Version:     0.7
 *  E-Mails:     shen139 [at] openwebspider (dot) org AND stefanofantinguz@yahoo.it
 *
 *
 * This file is part of OpenWebSpider
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef __HSTLIST
#define __HSTLIST


NODE* lstInit(struct sHost host)
{
NODE* first=NULL;
struct sHost* Host;
lstInited = 1;

	first=(NODE*) malloc(sizeof(NODE));
	
	Host = (struct sHost*)malloc(sizeof(struct sHost));

	if(Host==NULL || first==NULL)
		MemoryCorruptedHandler("lstInit");

	thrdBlock(BLOCKTHRDHST);
	first->field = (void*)Host;
	memcpy(Host ,&host,sizeof(struct sHost));
	first->next = NULL;
	
	thrdUnBlock(BLOCKTHRDHST);

return first;
}

int lstAddHost(NODE** first,struct sHost host)
{
NODE* cur;
NODE* newHost = (NODE*)malloc(sizeof(NODE));
struct sHost* fieldHost = (struct sHost*)malloc(sizeof(struct sHost));

	if(fieldHost==NULL || newHost==NULL)
		MemoryCorruptedHandler("lstAddHost");

	if((*first)->field == NULL || lstInited == 0)		//reinit list;
	{
		FREE(newHost);
		FREE(fieldHost);

		*first= lstInit(host);
		return 1;
	}

	thrdBlock(BLOCKTHRDHST);
	cur = lstGetLastNode(*first,NULL);

	if(cur==NULL)
	{
		FREE(newHost);
		FREE(fieldHost);
		return 1;
	}

	cur->next = newHost;
	newHost->field = (void*)fieldHost;
	memcpy(fieldHost ,&host,sizeof(struct sHost));
	
	newHost->next = NULL;

	thrdUnBlock(BLOCKTHRDHST);

return 1;
}

NODE* lstGetNodeByHost(NODE* first, struct sHost host)
{
NODE* cur;

	cur = first;

	if(lstInited==0 || cur == NULL)	
		return NULL;

	thrdBlock(BLOCKTHRDHST);
	while(cur != NULL && cur->field != NULL)
	{
		if(cur != NULL && cur->field!=NULL && ((struct sHost*)cur->field)->Page != NULL && strcmp(((struct sHost*)cur->field)->Page,host.Page )==0)
		{
			thrdUnBlock(BLOCKTHRDHST);
			return cur;
		}

		cur=cur->next;
	}
	thrdUnBlock(BLOCKTHRDHST);

return NULL;
}

NODE* lstGetNodeByVal(NODE* first,int val)
{
NODE* cur;

	cur = first;

	if(lstInited==0 || first==NULL)	
		return NULL;


	thrdBlock(BLOCKTHRDHST);
	while(cur != NULL && cur->field != NULL)
	{
		if(cur->field!=NULL && ((struct sHost*)cur->field)->viewed == val)
		{
			thrdUnBlock(BLOCKTHRDHST);
			return cur;
		}
		cur=cur->next;
	}
	thrdUnBlock(BLOCKTHRDHST);

return NULL;
}

int lstSetNodeStatus(NODE* first,int sub, int bus)
{
NODE* cur;

	cur = first;

	if(lstInited==0 || first==NULL)	
		return 0;


	thrdBlock(BLOCKTHRDHST);
	while(cur != NULL && cur->field != NULL)
	{
		if(cur->field!=NULL && ((struct sHost*)cur->field)->viewed == sub)
		{
			((struct sHost*)cur->field)->viewed=bus;
		}
		cur=cur->next;
	}
	thrdUnBlock(BLOCKTHRDHST);

return 1;
}

int lstDebugNodes(NODE* first,int viewed)
{
NODE* cur;

	cur = first;

	printf("\n\n----------------- debug nodes(%d) ------------------------\n\n", viewed);

	if(lstInited==0 || first==NULL)	
		return 0;

	
	thrdBlock(BLOCKTHRDHST);
	while(cur != NULL && cur->field != NULL)
	{
		if(cur->field!=NULL && ((struct sHost*)cur->field)->viewed == viewed)
		{
			printf("h: %s\np: %s\n\n",((struct sHost*)cur->field)->Host,((struct sHost*)cur->field)->Page);
		}
		cur=cur->next;
	}
	thrdUnBlock(BLOCKTHRDHST);

	printf("\n----------------- ============== ------------------------\n\n");
return 1;
}

#endif

/*EOF*/
