/*****************************************************************************/
/* html.cpp - 28/07/99 - By Christophe LOREK - CRYO-NETWORKS                 */
/*                                                                           */
/* HTML class                                                                */
/*                                                                           */
/* last modified on 03/08/99 By Christophe LOREK                             */
/*****************************************************************************/

#ifdef HTML_INCLUDE

#include "html.h"

extern int HtmlHandler;

HTML::HTML(mmachine m, TagStruct *roottag)
{
	int				structbuf,nodetab,objhtmlbuf;

// initialisation of HTML class contents
	x = y = w =	h = 0;
	linkcolorred = 0;
	linkcolorgreen = 0;
	linkcolorblue = 0xFF;

	title = NULL;
	nbURL = 1;
	URLlist = (URL**)malloc(nbURL*sizeof(URL*));
	URLlist[0] = NULL;

// HEADER treatment
	HeaderContent(m,this,roottag);

// HTML treatment
	HtmlContent(m,this,roottag);

// creating struct buffer
	structbuf = MMmalloc(m,NODESTRUCT_BUFSIZE,TYPEBUF);
	MMstore(m,structbuf,NODESTRUCT_STRUCT,(int)this);
	MMpush(m,1+(structbuf<<1));

// creating root node
	nodetab = MMmalloc(m,NODE_SIZE,TYPETAB);
	MMstore(m,nodetab,NODE_TYPE,NODE_TYPHTML<<1);
	MMstore(m,nodetab,NODE_FATHER,NIL);
	MMstore(m,nodetab,NODE_OBJBITMAP,NIL);
	MMstore(m,nodetab,NODE_STRUCT,MMpull(m));
	MMstore(m,nodetab,NODE_BROTHER,MMpull(m));
	MMstore(m,nodetab,NODE_SON,MMpull(m));
	MMpush(m,1+(nodetab<<1));

// creating ObjHtml
	objhtmlbuf = MMmalloc(m,OBJHTML_SIZE,TYPETAB);
	MMstore(m,objhtmlbuf,OBJHTML_HANDLER,HtmlHandler<<1);
	MMstore(m,objhtmlbuf,OBJHTML_NODE,MMpull(m));
	MMpush(m,1+(objhtmlbuf<<1));

}

HTML::~HTML()
{
	URL	**p;

	p = URLlist;
	if (title != NULL) 
		free(title);

	while (*p != NULL)		// free the URL elements of the list
	{
		if ((*p)->string != NULL) 
			free((*p)->string);	// free the string allocated for the URL
		free(*p);						// free the URL structure allocated
		p++;
	}

	free(URLlist);				// free the URL list
}

void	HTML::SetTitle(char *string)
{
	title = string;
}

void	HTML::CopyTitle(char *string)
{
	int length;

	if (string == NULL)
		return;

	length = strlen(string);
	title = (char*) malloc((length+1)*sizeof(char));
	memcpy(title,string,length);
	title[length]='\0';
	return;
}

void	HTML::GetTitle(char **string)
{
	*string = title;
}

char	*HTML::GetTitle()
{
	int		length;
	char	*currentstring;

	if (title == NULL)
		return NULL;

	length = strlen(title);
	currentstring = (char*) malloc((length+1)*sizeof(char));
	memcpy(currentstring,title,length);
	currentstring[length]='\0';

	return currentstring;
}

int		HTML::AddURL(char *givenstring,int URLtype)
{
	int		returnedstatus;
	URL		**newURLlist;
	URL		*newURL;
	char	*currentstring;
	int		length;

 if (givenstring == NULL)
		return HTML_URLSTATUS_ERROR;

	returnedstatus = GetURLStatus(givenstring);

	if (returnedstatus != HTML_URLSTATUS_NOTFOUND)
		return returnedstatus;
	else
	{
		newURLlist = (URL**) malloc ((nbURL + 1)*sizeof(URL*));
		newURL = (URL*) malloc (sizeof(URL));
		memcpy(newURLlist+1,URLlist,(nbURL)*sizeof(URL*));
		length = strlen(givenstring);
		currentstring = (char*) malloc((length+1)*sizeof(char));
		memcpy(currentstring,givenstring,length);
		currentstring[length]='\0';
		newURLlist[0] = newURL;
		newURLlist[0]->string = currentstring;
		newURLlist[0]->type = URLtype;
		newURLlist[0]->status = HTML_URLSTATUS_MISSING;
		free (URLlist);
		URLlist = newURLlist;
		nbURL++;
		return HTML_URLSTATUS_MISSING;
	}
}

int		HTML::GetURLStatus(char *string)
{
	int	i=0;

	while (URLlist[i] != NULL)
	{
		if (!strcmp(string,URLlist[i]->string)) return URLlist[i]->status;
		i++;
	}
	return HTML_URLSTATUS_NOTFOUND;
}

int	HTML::SetUrlStatus(char *string,int givenstatus)
{	
	int	i=0;

	while (URLlist[i] != NULL)
	{
		if (!strcmp(string,URLlist[i]->string)) return URLlist[i]->status;
		i++;
	}
	return HTML_URLSTATUS_NOTFOUND;
}

int		HTML::DisplayURLs()
{
	int	i=0;

	while (URLlist[i] != NULL)
	{
		MMechostr(MSKFOO,"DisplayURLs : URL '%s'   TYPE '%i'   STATUS '%i'\n",URLlist[i]->string,URLlist[i]->type,URLlist[i]->status);
		i++;
	}
	return 0;
}

int		HTML::URLListToScolStack(mmachine m)
{
	int	i=0,nburl=1;

	if (URLlist[0] == NULL)
		return MMpush(m,NIL);

	while (URLlist[nburl] != NULL)
		nburl++;
	
	for (i=0;i<nburl;i++)
	{
		Mpushstrbloc(m,URLlist[i]->string);
		MMpush(m,URLlist[i]->type << 1);
		MMpush(m,2 << 1);
		MBdeftab(m);
	}

	MMpush(m,NIL);								// list terminator

	for (i=0;i<nburl;i++)
	{
		MMpush(m,2 << 1);
		MBdeftab(m);
	}

	return 0;
}


#endif
