/*****************************************************************************/
/* htmltree.cpp - 05/08/99 - By Christophe LOREK - CRYO-NETWORKS             */
/*                                                                           */
/* HTML tree management                                                      */
/*                                                                           */
/* last modified on 17/08/99 By Christophe LOREK                             */
/*****************************************************************************/

#ifdef HTML_INCLUDE

#include "htmltree.h"

#define HTMLBITMAP_DEFAULT_WIDTH	32
#define HTMLBITMAP_DEFAULT_HEIGHT	32

int HeaderContent(mmachine m,HTML *currentHTML, TagStruct *roottag)
{
	TagStruct	*headtag;

  headtag = FindTag(roottag,"head");
	if (headtag == NULL)
		return NULL;

// title retrieval
  currentHTML->SetTitle(FindStringInTag(FindTag(headtag,"title")));

// URL retrieval, not specially part of the header and shall be removed, node will do the job...
//	SearchURLs(roottag,currentHTML);

	return 0;
}

int HtmlContent(mmachine m,HTML *currentHTML, TagStruct *roottag)
{
	char			*string;
	HTMLTEXT	*currentHTMLTEXT;
	TagStruct	*bodytag;
	Param			*param;
	int				linkcolor;

  bodytag = FindTag(roottag,"body");
	if (bodytag == NULL)
	{
		string = GetText(roottag);
		BodyContent(m,currentHTML,roottag->sontag,3,0,0,0,NULL,NULL,HTMLTEXT_ALIGN_LEFT,HTMLTEXT_ATT_NORMAL);
		MMpush(m,NIL); // no brother for body tag
		currentHTMLTEXT = new HTMLTEXT(m,3,0,0,0,NULL,NULL,HTMLTEXT_ALIGN_LEFT,HTMLTEXT_ATT_NORMAL,string,HTMLTEXT_FLAG_NOCR);
		MMpush(m,NIL); // no brother for html tag
	}
  else
	{
		param = TagGetParam(bodytag,"link");
		if (param != NULL)
		{
			if (sscanf(param->value,"#%x",&linkcolor) == 1)
			{
				currentHTML->linkcolorred	  = (0x00ff0000 & linkcolor)>>16;
				currentHTML->linkcolorgreen	= (0x0000ff00 & linkcolor)>>8;
				currentHTML->linkcolorblue  = (0x000000ff & linkcolor);
			}
		}
		string = GetText(bodytag);
		BodyContent(m,currentHTML,bodytag->sontag,3,0,0,0,NULL,NULL,HTMLTEXT_ALIGN_LEFT,HTMLTEXT_ATT_NORMAL);
		BodyContent(m,currentHTML,bodytag->brothertag,3,0,0,0,NULL,NULL,HTMLTEXT_ALIGN_LEFT,HTMLTEXT_ATT_NORMAL);
		currentHTMLTEXT = new HTMLTEXT(m,3,0,0,0,NULL,NULL,HTMLTEXT_ALIGN_LEFT,HTMLTEXT_ATT_NORMAL,string,HTMLTEXT_FLAG_NOCR);
		MMpush(m,NIL); // no brother for html tag

		param = TagGetParam(bodytag,"background");
		if (param != NULL)
		{
			// creer une bitmap mere de tout les autres contenus du body
			// rajouter l'URL de la bitmap a la liste de toutes les URL de l'objet HTML
		}
	}
	return 0;
}
int BodyContent(
	mmachine m,
	HTML *currentHTML,
	TagStruct *currenttag,
	int givensize,
	int givenred,
	int givengreen,
	int givenblue,
	char *givenface,
	char *givenurl,
	int givenalign,
	int givenattributes)
{
	char			*string;
	HTMLTEXT	*currentHTMLTEXT;
	Param			*param;
	int				currentalign = givenalign;

	if (currenttag != NULL)
	{
//		MMechostr(MSKFOO,"DEBUG BODY TAGNAME %s\n",currenttag->name);
		if (!stricmp(currenttag->name,"div"))
		{
			param = TagGetParam(currenttag,"align");
			if (param != NULL)
			{
				if (!stricmp(param->value,"left"))
					currentalign = HTMLTEXT_ALIGN_LEFT;
				else if (!stricmp(param->value,"center"))
					currentalign = HTMLTEXT_ALIGN_CENTER;
				else if (!stricmp(param->value,"right"))
					currentalign = HTMLTEXT_ALIGN_RIGHT;
				else
					currentalign = HTMLTEXT_ALIGN_LEFT;
			}	

			string = GetText(currenttag);
			BodyContent(m,currentHTML,currenttag->sontag,givensize,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes);
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes,string,HTMLTEXT_FLAG_NOCR);
		}
		else if (!stricmp(currenttag->name,"h1"))
			HeadingContent(m,currentHTML,currenttag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,1);
		else if (!stricmp(currenttag->name,"h2"))
			HeadingContent(m,currentHTML,currenttag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,2);
		else if (!stricmp(currenttag->name,"h3"))
			HeadingContent(m,currentHTML,currenttag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,3);
		else if (!stricmp(currenttag->name,"h4"))
			HeadingContent(m,currentHTML,currenttag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,4);
		else if (!stricmp(currenttag->name,"h5"))
			HeadingContent(m,currentHTML,currenttag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,5);
		else if (!stricmp(currenttag->name,"h6"))
			HeadingContent(m,currentHTML,currenttag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,6);
		else if ((!stricmp(currenttag->name,"p"))	||
						 (!stricmp(currenttag->name,"table")) ||
						 (!stricmp(currenttag->name,"center")) )
			BlockContent(m,currentHTML,currenttag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
		else
		{
			TextContent(m,currentHTML,currenttag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
		}
	}
	else
	{
		MMpush(m,NIL);
	}
	return 0;
}

int HeadingContent(
	mmachine m,
	HTML *currentHTML,
	TagStruct *currenttag,
	int givensize,
	int givenred,
	int givengreen,
	int givenblue,
	char *givenface,
	char *givenurl,
	int givenalign,
	int givenattributes,
	int level)
{
	HTMLTEXT		*currentHTMLTEXT;
	int					currentalign=HTMLTEXT_ALIGN_LEFT;
	char				*string;
	Param				*param;

	if (currenttag != NULL)
	{
//		MMechostr(MSKFOO,"DEBUG HEADING TAGNAME %s\n",currenttag->name);
		param = TagGetParam(currenttag,"align");
		if (param != NULL)
		{
			if (!stricmp(param->value,"left"))
				currentalign = HTMLTEXT_ALIGN_LEFT;
			else if (!stricmp(param->value,"center"))
				currentalign = HTMLTEXT_ALIGN_CENTER;
			else if (!stricmp(param->value,"right"))
				currentalign = HTMLTEXT_ALIGN_RIGHT;
			else
				currentalign = HTMLTEXT_ALIGN_LEFT;
		}	

    switch(level)
		{
			case 1:
				string = GetText(currenttag);
				TextContent(m,currentHTML,currenttag->sontag,6,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD);
				BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
				currentHTMLTEXT = new HTMLTEXT(m,6,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_CR);
//				currentHTMLTEXT = new HTMLTEXT(m,6,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_NOCR);
				break;

			case 2:
				string = GetText(currenttag);
				TextContent(m,currentHTML,currenttag->sontag,5,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD);
				BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
				currentHTMLTEXT = new HTMLTEXT(m,5,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_CR);
//				currentHTMLTEXT = new HTMLTEXT(m,5,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_NOCR);
				break;

			case 3:
				string = GetText(currenttag);
				TextContent(m,currentHTML,currenttag->sontag,4,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD);
				BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
				currentHTMLTEXT = new HTMLTEXT(m,4,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_CR);
//				currentHTMLTEXT = new HTMLTEXT(m,4,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_NOCR);
				break;

			case 4:
				string = GetText(currenttag);
				TextContent(m,currentHTML,currenttag->sontag,3,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD);
				BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
				currentHTMLTEXT = new HTMLTEXT(m,3,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_CR);
//				currentHTMLTEXT = new HTMLTEXT(m,3,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_NOCR);
				break;

			case 5:
				string = GetText(currenttag);
				TextContent(m,currentHTML,currenttag->sontag,2,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD);
				BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
				currentHTMLTEXT = new HTMLTEXT(m,2,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_CR);
//				currentHTMLTEXT = new HTMLTEXT(m,2,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_NOCR);
				break;

			case 6:
				string = GetText(currenttag);
				TextContent(m,currentHTML,currenttag->sontag,1,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD);
				BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
				currentHTMLTEXT = new HTMLTEXT(m,1,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_CR);
//				currentHTMLTEXT = new HTMLTEXT(m,1,givenred,givengreen,givenblue,givenface,givenurl,currentalign,givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_NOCR);
				break;

			default:
				string = GetText(currenttag);
				TextContent(m,currentHTML,currenttag->sontag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
				BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
				currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string,HTMLTEXT_FLAG_CR);
//				currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string,HTMLTEXT_FLAG_NOCR);
				break;
		}
	}
	else
	{
		MMpush(m,NIL); // son
	}
	return 0;
}

int BlockContent(
	mmachine m,
	HTML *currentHTML,
	TagStruct *currenttag,
	int givensize,
	int givenred,
	int givengreen,
	int givenblue,
	char *givenface,
	char *givenurl,
	int givenalign,
	int givenattributes)
{
	HTMLTABLE	*currentHTMLTABLE;
	HTMLTEXT	*currentHTMLTEXT;
	char			*string;
	char			*string2;
	int				currentalign=HTMLTEXT_ALIGN_LEFT;
	Param			*param;
	int				givenwidth=0;
	int				givencols=0;
	int				givenborder=0;
	int				cellspacing=0;
	int				cellpadding=0;
	int				width=0;
	int				cols=0;
	int				bgcolor=0;
	int				bgred=-1;
	int				bggreen=-1;
	int				bgblue=-1;
	
	if (currenttag != NULL)
	{
//		MMechostr(MSKFOO,"DEBUG BLOCK : tagname %s  s:'%08x'  b:'%08x'\n",currenttag->name,currenttag->sontag,currenttag->brothertag);
		if (!stricmp(currenttag->name,"table"))
		{
			currentalign=HTMLTEXT_ALIGN_CENTER;  // default align value for table is centered
			param = TagGetParam(currenttag,"width");
			if (param != NULL)
				if (strchr(param->value,'%')!=NULL)
				{
					if (sscanf(param->value,"%i%%",&width) == 1)
						givenwidth = -width; // negative value because width express in %
					else
						givenwidth = -100; // indicates full width
				}
				else
				{
					if (sscanf(param->value,"%i",&width) == 1)
						givenwidth = width;
					else
						givenwidth = -100; // indicates full width
				}
			param = TagGetParam(currenttag,"cols");
			if (param != NULL)
				if (sscanf(param->value,"%i",&cols) == 1)
					givencols = cols;
				else
					givencols = -1;
			param = TagGetParam(currenttag,"align");
			if (param != NULL)
			{
				if (!stricmp(param->value,"left"))
					currentalign = HTMLTEXT_ALIGN_LEFT;
				else if (!stricmp(param->value,"center"))
					currentalign = HTMLTEXT_ALIGN_CENTER;
				else if (!stricmp(param->value,"right"))
					currentalign = HTMLTEXT_ALIGN_RIGHT;
				else
					currentalign = HTMLTEXT_ALIGN_CENTER;
			}
			param = TagGetParam(currenttag,"border");
			if (param != NULL)
			{
				if (sscanf(param->value,"%i",&givenborder) != 1)
					givenborder = 1;
			}
			param = TagGetParam(currenttag,"cellspacing");
			if (param != NULL)
			{
				if (sscanf(param->value,"%i",&cellspacing) != 1)
					cellspacing = 0;
			}
			param = TagGetParam(currenttag,"cellpadding");
			if (param != NULL)
			{
				if (sscanf(param->value,"%i",&cellpadding) != 1)
					cellpadding = 0;
			}
			param = TagGetParam(currenttag,"bgcolor");
			if (param != NULL)
			{
				if (sscanf(param->value,"#%x",&bgcolor) == 1)
				{
					bgred		= (0x00ff0000 & bgcolor)>>16;
					bggreen	= (0x0000ff00 & bgcolor)>>8;
					bgblue	= (0x000000ff & bgcolor);
				}
			}
				
	//	MMechostr(MSKFOO,"DEBUG TABLE : border %i\n",givenborder);
			TableContent(m,currentHTML,currenttag->sontag,currentalign,bgred,bggreen,bgblue,givenwidth,givencols,givenborder,HTMLTABLE_FRAME_BORDER,HTMLTABLE_RULES_NONE,cellspacing,cellpadding);

			string2 = GetText2(currenttag->endtag);
      MMpush(m,NIL);		
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTABLE = new HTMLTABLE(m,currentalign,bgred,bggreen,bgblue,givenwidth,givencols,givenborder,HTMLTABLE_FRAME_BORDER,HTMLTABLE_RULES_NONE,cellspacing,cellpadding);			
		}
		else if (!stricmp(currenttag->name,"p"))
		{
			param = TagGetParam(currenttag,"align");
			if (param != NULL)
			{
				if (!stricmp(param->value,"left"))
					currentalign = HTMLTEXT_ALIGN_LEFT;
				else if (!stricmp(param->value,"center"))
					currentalign = HTMLTEXT_ALIGN_CENTER;
				else if (!stricmp(param->value,"right"))
					currentalign = HTMLTEXT_ALIGN_RIGHT;
				else
					currentalign = HTMLTEXT_ALIGN_LEFT;
			}
//		MMechostr(MSKFOO,"DEBUG P : tagname %s\n",currenttag->name);
			string = GetText(currenttag);

			TextContent(m,currentHTML,currenttag->sontag,3,0,0,0,NULL,NULL,currentalign,HTMLTEXT_ATT_NORMAL);

			string2 = GetText2(currenttag->endtag);
      MMpush(m,NIL);		
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,3,0,0,0,NULL,NULL,currentalign,HTMLTEXT_ATT_NORMAL,string,HTMLTEXT_FLAG_CR);
		}
		else if (!stricmp(currenttag->name,"center"))
		{
			currentalign = HTMLTEXT_ALIGN_CENTER;

//		MMechostr(MSKFOO,"DEBUG P : tagname %s\n",currenttag->name);
			string = GetText(currenttag);

			BodyContent(m,currentHTML,currenttag->sontag,3,0,0,0,NULL,NULL,currentalign,HTMLTEXT_ATT_NORMAL);

			string2 = GetText2(currenttag->endtag);
      MMpush(m,NIL);		
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,3,0,0,0,NULL,NULL,currentalign,HTMLTEXT_ATT_NORMAL,string,HTMLTEXT_FLAG_NOCR);
		}
		else
		{
			param = TagGetParam(currenttag,"align");
			if (param != NULL)
			{
				if (!stricmp(param->value,"left"))
					currentalign = HTMLTEXT_ALIGN_LEFT;
				else if (!stricmp(param->value,"center"))
					currentalign = HTMLTEXT_ALIGN_CENTER;
				else if (!stricmp(param->value,"right"))
					currentalign = HTMLTEXT_ALIGN_RIGHT;
				else
					currentalign = HTMLTEXT_ALIGN_LEFT;
			}
//		MMechostr(MSKFOO,"DEBUG OTHER : tagname %s\n",currenttag->name);
			string = GetText(currenttag);
			TextContent(m,currentHTML,currenttag->sontag,3,0,0,0,NULL,NULL,currentalign,HTMLTEXT_ATT_NORMAL);
			TextContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,3,0,0,0,NULL,NULL,currentalign,HTMLTEXT_ATT_NORMAL,string,HTMLTEXT_FLAG_NOCR);
		}
	}
	else
	{
		MMpush(m,NIL); // son
	}
	return 0;
}

int TextContent(mmachine m,
	HTML *currentHTML,
	TagStruct *currenttag,
	int givensize,
	int givenred,
	int givengreen,
	int givenblue,
	char *givenface,
	char *givenurl,
	int givenalign,
	int givenattributes)
{
	HTMLTEXT		*currentHTMLTEXT;
	HTMLBITMAP	*currentHTMLBITMAP;
	Param				*param;
	int					fontcolor;
	int					currentsize=givensize;
	char				*currentface=givenface;
	char        *currenturl=givenurl;
	char				*string;
	char				*string2;
	int					length;
	int         currentred=givenred;
	int         currentgreen=givengreen;
	int         currentblue=givenblue;
	int					givenborder=0;
	int					givenwidth=0;
	int					givenheight=0;
	
	if (currenttag != NULL)
	{
//		MMechostr(MSKFOO,"DEBUG TEXT TAGNAME %s\n",currenttag->name);
		if (!stricmp(currenttag->name,"b") || !stricmp(currenttag->name,"strong"))
		{
			string = GetText(currenttag);
			TextContent(m,currentHTML,currenttag->sontag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes|HTMLTEXT_ATT_BOLD);

			string2 = GetText2(currenttag->endtag);
      MMpush(m,NIL);		
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes|HTMLTEXT_ATT_BOLD,string,HTMLTEXT_FLAG_NOCR);
		}
		else if (!stricmp(currenttag->name,"u"))
		{
			string = GetText(currenttag);
			TextContent(m,currentHTML,currenttag->sontag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes|HTMLTEXT_ATT_UNDERLINED);

			string2 = GetText2(currenttag->endtag);
      MMpush(m,NIL);			
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes|HTMLTEXT_ATT_UNDERLINED,string,HTMLTEXT_FLAG_NOCR);
		}
		else if (!stricmp(currenttag->name,"s") || !stricmp(currenttag->name,"strike"))
		{
			string = GetText(currenttag);
			TextContent(m,currentHTML,currenttag->sontag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes|HTMLTEXT_ATT_STRIKED);

			string2 = GetText2(currenttag->endtag);
      MMpush(m,NIL);			
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes|HTMLTEXT_ATT_STRIKED,string,HTMLTEXT_FLAG_NOCR);
		}
		else if (!stricmp(currenttag->name,"i") || !stricmp(currenttag->name,"em"))
		{
			string = GetText(currenttag);
			TextContent(m,currentHTML,currenttag->sontag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes|HTMLTEXT_ATT_ITALIC);

			string2 = GetText2(currenttag->endtag);
      MMpush(m,NIL);			
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes|HTMLTEXT_ATT_ITALIC,string,HTMLTEXT_FLAG_NOCR);
		}
		else if (!stricmp(currenttag->name,"font") || !stricmp(currenttag->name,"big") || !stricmp(currenttag->name,"small"))
		{
			param = TagGetParam(currenttag,"color");
			if (param != NULL)
				if (sscanf(param->value,"#%x",&fontcolor) == 1)
				{
					currentred		= (0x00ff0000 & fontcolor)>>16;
					currentgreen	= (0x0000ff00 & fontcolor)>>8;
					currentblue		= (0x000000ff & fontcolor);
				}

			param = TagGetParam(currenttag,"size");
			if (param != NULL)
				if (sscanf(param->value,"%i",&currentsize) != 1)
					currentsize = givensize;
			param = TagGetParam(currenttag,"face");
			if (param != NULL)
			{
				length = strlen(param->value);
				currentface = (char*) malloc((length+1)*sizeof(char));
				memcpy(currentface,param->value,(length+1));
			}

			if (!stricmp(currenttag->name,"big"))
				if (currentsize<7) currentsize++;

			if (!stricmp(currenttag->name,"small"))
				if (currentsize>1) currentsize--;

			string = GetText(currenttag);
			TextContent(m,currentHTML,currenttag->sontag,currentsize,currentred,currentgreen,currentblue,currentface,givenurl,givenalign,givenattributes);

			string2 = GetText2(currenttag->endtag);
      MMpush(m,NIL);
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes,string2,HTMLTEXT_FLAG_NOCR);


			currentHTMLTEXT = new HTMLTEXT(m,currentsize,currentred,currentgreen,currentblue,currentface,givenurl,givenalign,
																		givenattributes,string,HTMLTEXT_FLAG_NOCR);
		}

		else if (!stricmp(currenttag->name,"img"))
		{
			param = TagGetParam(currenttag,"src");
			if (param != NULL)
			{
				length = strlen(param->value);
				givenurl = (char*) malloc((length+1)*sizeof(char));
				memcpy(givenurl,param->value,(length+1));
				currentHTML->AddURL(givenurl,HTML_URLTYPE_HTMLBITMAP);

			}
			param = TagGetParam(currenttag,"border");
			if (param != NULL)
			{
				if (sscanf(param->value,"%i",&givenborder) != 1)
					givenborder = 1;
			}

			param = TagGetParam(currenttag,"width");
			if (param != NULL)
			{
				if (sscanf(param->value,"%i",&givenwidth) != 1)
					givenwidth = HTMLBITMAP_DEFAULT_WIDTH;
			}
			else
				givenwidth = HTMLBITMAP_DEFAULT_WIDTH;

			param = TagGetParam(currenttag,"height");
			if (param != NULL)
			{
				if (sscanf(param->value,"%i",&givenheight) != 1)
					givenheight = HTMLBITMAP_DEFAULT_WIDTH;
			}
			else
				givenheight = HTMLBITMAP_DEFAULT_WIDTH;

			param = TagGetParam(currenttag,"align");
			if (param != NULL)
			{
				if (!stricmp(param->value,"left"))
					givenalign = HTMLTEXT_ALIGN_LEFT;
				else if (!stricmp(param->value,"center"))
					givenalign = HTMLTEXT_ALIGN_CENTER;
				else if (!stricmp(param->value,"right"))
					givenalign = HTMLTEXT_ALIGN_RIGHT;
				else
					givenalign = HTMLTEXT_ALIGN_LEFT;
			}
			MMpush(m,NIL); // son
			string2 = GetText2(currenttag);
	    MMpush(m,NIL); // no son
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,
																		givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLBITMAP = new HTMLBITMAP(m,givenalign,givenwidth,givenheight,givenborder,givenurl);
		}

		else if (!stricmp(currenttag->name,"a"))
		{
			param = TagGetParam(currenttag,"href");
			if (param != NULL)
			{
				currenturl = param->value;
			}
			else 
				currenturl = givenurl;

			string = GetText(currenttag);
			TextContent(m,currentHTML,currenttag->sontag,givensize,currentHTML->linkcolorred,currentHTML->linkcolorgreen,currentHTML->linkcolorblue,givenface,currenturl,givenalign,givenattributes|HTMLTEXT_ATT_UNDERLINED);
			string2 = GetText2(currenttag->endtag);
	    MMpush(m,NIL); // no son
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,givensize,currentHTML->linkcolorred,currentHTML->linkcolorgreen,currentHTML->linkcolorblue,givenface,currenturl,givenalign,givenattributes|HTMLTEXT_ATT_UNDERLINED,string,HTMLTEXT_FLAG_NOCR);
		}
		else if (!stricmp(currenttag->name,"br"))
		{
/*
			MMpush(m,NIL); // son
			string2 = GetText2(currenttag);
	    MMpush(m,NIL); // no son
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string2,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,NULL,HTMLTEXT_FLAG_CR);
*/
//			MMpush(m,NIL); // son
			string2 = GetText2(currenttag);
	     MMpush(m,NIL); // no son
	      MMpush(m,NIL); // no son
			  BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			 currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string2,HTMLTEXT_FLAG_NOCR);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,NULL,HTMLTEXT_FLAG_CR);

//			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,NULL,HTMLTEXT_FLAG_CR);

		}
		else
		{
			string = GetText(currenttag);
			TextContent(m,currentHTML,currenttag->sontag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			if(currenttag->endtag == NULL)
				string2 = GetText2(currenttag);
			else
				string2 = GetText2(currenttag->endtag);
	    MMpush(m,NIL); // no son			
			BodyContent(m,currentHTML,currenttag->brothertag,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes);
			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string,HTMLTEXT_FLAG_NOCR);

			currentHTMLTEXT = new HTMLTEXT(m,givensize,givenred,givengreen,givenblue,givenface,givenurl,givenalign,givenattributes,string,HTMLTEXT_FLAG_NOCR);
		}
	}
	else
	{
		MMpush(m,NIL); // son
	}
	return 0;
}

int TableContent(mmachine m,
	HTML *currentHTML,
	TagStruct *currenttag,
	int givenalign,
	int givenred,
	int givengreen,
	int givenblue,
	int	givenwidth,
	int givencols,
	int	givenbordersize,
	int	givenframe,
	int	givenrules,
	int	givencellspacing,
	int	givencellpadding)
{
	HTMLROW			*currentHTMLROW;
	char				*string;
	Param				*param;
	int					width=0;
	int					bgcolor=0;
	int					currentalign=HTMLTEXT_ALIGN_LEFT;

	if (currenttag != NULL)
	{
//		MMechostr(MSKFOO,"DEBUG TABLE TAGNAME %s\n",currenttag->name);
		param = TagGetParam(currenttag,"align");
		if (param != NULL)
		{
			if (!stricmp(param->value,"left"))
				currentalign = HTMLTEXT_ALIGN_LEFT;
			else if (!stricmp(param->value,"center"))
				currentalign = HTMLTEXT_ALIGN_CENTER;
			else if (!stricmp(param->value,"right"))
				currentalign = HTMLTEXT_ALIGN_RIGHT;
			else
				currentalign = HTMLTEXT_ALIGN_LEFT;
		}
		
		if (!stricmp(currenttag->name,"tr"))
		{
//		MMechostr(MSKFOO,"DEBUG TR : tagname %s\n",currenttag->name);
			param = TagGetParam(currenttag,"width");
			if (param != NULL)
				if (strchr(param->value,'%')!=NULL)
				{
					if (sscanf(param->value,"%i%%",&width) == 1)
						givenwidth = -width; // negative value because width express in %
					else
						givenwidth = 0; // indicates full width
				}
				else
				{
					if (sscanf(param->value,"%i",&width) == 1)
						givenwidth = width;
					else
						givenwidth = 0; // indicates full width
				}
			else
				givenwidth = -100; // 100%
			param = TagGetParam(currenttag,"bgcolor");
			if (param != NULL)
				if (sscanf(param->value,"#%x",&bgcolor) == 1)
				{
					givenred		= (0x00ff0000 & bgcolor)>>16;
					givengreen	= (0x0000ff00 & bgcolor)>>8;
					givenblue		= (0x000000ff & bgcolor);
				}
			string = GetText(currenttag);
			CellContent(m,currentHTML,currenttag->sontag,currentalign,givenred,givengreen,givenblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);
			TableContent(m,currentHTML,currenttag->brothertag,givenalign,givenred,givengreen,givenblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);
			currentHTMLROW = new HTMLROW(m,currentalign,givenred,givengreen,givenblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);			
		}	
		else if (strchr(currenttag->nexttag->name,'/') != NULL)
		{
			TableContent(m,currentHTML,currenttag->brothertag,givenalign,givenred,givengreen,givenblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);
		}
		else
		{
			TextContent(m,currentHTML,currenttag,3,0,0,0,NULL,NULL,HTMLTEXT_ALIGN_LEFT,HTMLTEXT_ATT_NORMAL);
		}
	}
	else
	{
		MMpush(m,NIL);
	}
	return 0;
}

int CellContent(mmachine m,
	HTML *currentHTML,
	TagStruct *currenttag,
	int givenalign,
	int givenred,
	int givengreen,
	int givenblue,
	int	givenwidth,
	int givencols,
	int	givenbordersize,
	int	givenframe,
	int	givenrules,
	int	givencellspacing,
	int	givencellpadding)
{
	HTMLCELL		*currentHTMLCELL;
	Param				*param;
	int					width=0;
	int					cellspacing=0;
	int					cellpadding=0;
	int					bgcolor=0;
	int					currentred=-1;
	int					currentgreen=-1;
	int					currentblue=-1;
	int					currentalign=givenalign;

	if (currenttag != NULL)
	{
//		MMechostr(MSKFOO,"DEBUG TABLE TAGNAME %s\n",currenttag->name);
		param = TagGetParam(currenttag,"align");
		if (param != NULL)
		{
			if (!stricmp(param->value,"left"))
				currentalign = HTMLTEXT_ALIGN_LEFT;
			else if (!stricmp(param->value,"center"))
				currentalign = HTMLTEXT_ALIGN_CENTER;
			else if (!stricmp(param->value,"right"))
				currentalign = HTMLTEXT_ALIGN_RIGHT;
			else
				currentalign = HTMLTEXT_ALIGN_LEFT;
		}
		param = TagGetParam(currenttag,"cellspacing");
		if (param != NULL)
		{
			if (sscanf(param->value,"%i",&cellspacing) == 1)
				givencellspacing = cellspacing;
		}
		param = TagGetParam(currenttag,"cellpadding");
		if (param != NULL)
		{
			if (sscanf(param->value,"%i",&cellpadding) == 1)
				givencellpadding = cellpadding;
		}
		param = TagGetParam(currenttag,"bgcolor");
		if (param != NULL)
			if (sscanf(param->value,"#%x",&bgcolor) == 1)
			{
				currentred		= (0x00ff0000 & bgcolor)>>16;
				currentgreen	= (0x0000ff00 & bgcolor)>>8;
				currentblue		= (0x000000ff & bgcolor);
			}
	
		if (!stricmp(currenttag->name,"td"))

		{
//		MMechostr(MSKFOO,"DEBUG TD : tagname %s\n",currenttag->name);
			param = TagGetParam(currenttag,"width");
			if (param != NULL)
				if (strchr(param->value,'%')!=NULL)
				{
					if (sscanf(param->value,"%i%%",&width) == 1)
						givenwidth = -width; // negative value because width express in %
					else
						givenwidth = 0; // indicates full width
				}
				else
				{
					if (sscanf(param->value,"%i",&width) == 1)
						givenwidth = width;
					else
						givenwidth = -100; // indicates full width
				}

			BodyContent(m,currentHTML,currenttag->sontag,3,0,0,0,NULL,NULL,currentalign,HTMLTEXT_ATT_NORMAL);
			CellContent(m,currentHTML,currenttag->brothertag,givenalign,givenred,givengreen,givenblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);
			currentHTMLCELL = new HTMLCELL(m,currentalign,currentred,currentgreen,currentblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);			
		}		
		else if (!stricmp(currenttag->name,"th"))
		{
//		MMechostr(MSKFOO,"DEBUG TH : tagname %s\n",currenttag->name);
			param = TagGetParam(currenttag,"width");
			if (param != NULL)
				if (strchr(param->value,'%')!=NULL)
				{
					if (sscanf(param->value,"%i%%",&width) == 1)
						givenwidth = -width; // negative value because width express in %
					else
						givenwidth = 0; // indicates full width
				}
				else
				{
					if (sscanf(param->value,"%i",&width) == 1)
						givenwidth = width;
					else
						givenwidth = -100; // indicates full width
				}
			BodyContent(m,currentHTML,currenttag->sontag,3,0,0,0,NULL,NULL,currentalign,HTMLTEXT_ATT_NORMAL);
			CellContent(m,currentHTML,currenttag->brothertag,givenalign,givenred,givengreen,givenblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);
			currentHTMLCELL = new HTMLCELL(m,currentalign,currentred,currentgreen,currentblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);			
		}		
		else if (strchr(currenttag->nexttag->name,'/') != NULL)
		{
			CellContent(m,currentHTML,currenttag->brothertag,givenalign,givenred,givengreen,givenblue,givenwidth,givencols,givenbordersize,givenframe,givenrules,givencellspacing,givencellpadding);
		}
		else
		{
//		MMechostr(MSKFOO,"DEBUG CELLCONTENT ERROR : tagname %s\n",currenttag->name);
			MMpush(m,NIL);
		}
	}
	else
	{
		MMpush(m,NIL);
	}
	return 0;
}

/* ************************************* */

TagStruct *FindTag(TagStruct *currenttag,char *tagname)
{
	TagStruct *myreturn = NULL;

	while(currenttag != NULL)
	{
		if (!stricmp(tagname,currenttag->name))
			return currenttag;
		if (currenttag->sontag != NULL)
			myreturn = FindTag(currenttag->sontag,tagname);
		if (myreturn != NULL)
			return myreturn;
		currenttag = currenttag->brothertag;
	}

	return NULL;

}

char *FindStringInTag(TagStruct *currenttag)
{
	char	*begin;
	char	*end;
	int		length;
	char	*string;

	if (currenttag == NULL)
		return NULL;

	if (currenttag->endtag != NULL)
	{
		begin = currenttag->end;
		end = currenttag->endtag->begin;

		length = end - begin;

		string = (char*) malloc((length+1)*sizeof(char));
		memcpy(string,begin,length);
		string[length]='\0';

		return string;
	}
	else
		return NULL;
}

Param *TagGetParam(TagStruct *currenttag,char *paramname)
{
	int i = 0;

	if (currenttag == NULL)
		return NULL;

	while (currenttag->paramlist[i] != NULL)
	{
//		MMechostr(MSKFOO,"TagGetParam : reading paramlist...\n");
		if(!stricmp(currenttag->paramlist[i]->name,paramname))
		{
//			MMechostr(MSKFOO,"TagGetParam : end of paramlist reading...\n");
			return currenttag->paramlist[i];
		}
		i++;
	}

	return NULL;
}


char *FindURL(TagStruct *currenttag,int *URLtype)
{
	Param		*currentparam = NULL;

	if (currenttag == NULL)
		return NULL;

	if(!stricmp(currenttag->name,"a"))
	{
		currentparam = TagGetParam(currenttag,"href");
		*URLtype = HTML_URLTYPE_HTML;
	}
	else if(!stricmp(currenttag->name,"img"))
	{
		currentparam = TagGetParam(currenttag,"src");
		*URLtype = HTML_URLTYPE_HTMLBITMAP;
	}
	else if(!stricmp(currenttag->name,"body"))
	{
		currentparam = TagGetParam(currenttag,"background");
		*URLtype = HTML_URLTYPE_HTMLBITMAP;
	}
	else if(!stricmp(currenttag->name,"blockquote"))
	{
		currentparam = TagGetParam(currenttag,"cite");
		*URLtype = HTML_URLTYPE_HTMLTEXT;
	}
/*
	else if(!stricmp(currenttag->name,"frame")
	else if(!stricmp(currenttag->name,"iframe")
*/
	if (currentparam != NULL)
		return currentparam->value;
	else
		return NULL;
}

int SearchURLs(TagStruct *currenttag,HTML *currentHTML)
{
	int				URLtype;
	char			*URLname;

	while(currenttag != NULL)
	{
		URLname = FindURL(currenttag,&URLtype);
		currentHTML->AddURL(URLname,URLtype);
		if (currenttag->sontag != NULL)
			SearchURLs(currenttag->sontag,currentHTML);
		currenttag = currenttag->brothertag;
	}

	return 0;
}

int DisplayTags(TagStruct *currenttag)
{
	Param		*currentparam;
	int			i=0;

	while(currenttag != NULL)
	{
		MMechostr(MSKFOO,"TAG '%s' '%08x'\n",currenttag->name,currenttag);
		i=0;
		currentparam = currenttag->paramlist[i++];
		while(currentparam != NULL)
		{
			MMechostr(MSKFOO,"PARAM '%s' ",currentparam->name);
			MMechostr(MSKFOO," VALUE '%s'\n",currentparam->value);
			currentparam = currenttag->paramlist[i++];
		}
		currenttag = currenttag->nexttag;
	}
	
	return 0;
}

int DisplayTreeTags(TagStruct *currenttag)
{

	while(currenttag->nexttag != NULL)
	{
		MMechostr(MSKFOO," TAG '%s'",currenttag->name);
		if (currenttag->brothertag != NULL)
			MMechostr(MSKFOO," BROTHER '%s'",currenttag->brothertag->name);
		else
			MMechostr(MSKFOO," BROTHER NULL ");
			
		if (currenttag->sontag != NULL) 
			MMechostr(MSKFOO," SON TAG '%s'",currenttag->sontag->name);
		else
			MMechostr(MSKFOO," SON TAG NULL ");
			
		if (currenttag->endtag != NULL) 
			MMechostr(MSKFOO," END TAG '%s'\n",currenttag->endtag->name);
		else
			MMechostr(MSKFOO," END TAG NULL\n");
			
		currenttag = currenttag->nexttag;
	}
	return 0;
}

/*
 * DisplayParams :
 *	In						:	currenttag		(TagStruct *)	the current tag to display.
 *	Out						:	DisplayParams	(int)					status (0 == successful).
 *	Side Effects	:	displays the current tag params in the SCOL console.
 */

int DisplayParams(TagStruct *currenttag)
{
	Param		*currentparam;
	int			i=0;

	i=0;
	currentparam = currenttag->paramlist[i++];
	while(currentparam != NULL)
	{
		MMechostr(MSKFOO,"    PARAM '%s' ",currentparam->name);
		MMechostr(MSKFOO," VALUE '%s'\n",currentparam->value);
		currentparam = currenttag->paramlist[i++];
	}
	return 0;
}


/*
 * DisplayTreeTags2 :
 *	In						:	currenttag				(TagStruct *)	the current tag to display.
 *									i									(int)					the current level.
 *	Out						:	DisplayTreeTags2	(int)					status (0 == successful).
 *	Side Effects	:	displays the current tag name in the SCOL console.
 */

int DisplayTreeTags2(TagStruct *currenttag,int i)
{
	while(currenttag != NULL)
	{
		MMechostr(MSKFOO,"%i ",i);
		MMechostr(MSKFOO,"TAG '%s' '%08x' '%08x' '%08x'\n",currenttag->name,currenttag->sontag,currenttag->brothertag,currenttag->nexttag);
		DisplayParams(currenttag);
		if (currenttag->sontag != NULL)
			DisplayTreeTags2(currenttag->sontag,i+1);
		currenttag = currenttag->brothertag;
	}
	return 0;
}

char *FilterText(char *string)
{
	int		length;
	int		i,j;
	char	*tempstring;
	char	*outstring;
	int isempty = 1;

	if (string == NULL)
		return 0;

	length = strlen(string);

	tempstring = (char*) malloc((length+1)*sizeof(char));
	j = 0;

	for(i=0;i<length;i++)
	{
		if ((string[i] == '\n') ||
				(string[i] == '\t') ||
				(string[i] == ' ')	||
				(string[i] == '\r'))
		{
			if ((j>0) && (tempstring[j-1]!=' '))
			{
				tempstring[j]=' ';
				j++;
			}
			tempstring[j] = ' ';
			string[i] = ' ';
		}
		else
		{
			isempty = 0;
			tempstring[j] = string[i];
			j++;
		}
	}
	tempstring[j] = '\0';
	length = strlen(tempstring);

	if (isempty)
		outstring = NULL;
	else
	{
		outstring = (char*) malloc((length+1)*sizeof(char));
		memcpy(outstring,tempstring,length);
		outstring[length]='\0';
	}

	free(tempstring);
 	return outstring;
}

char *GetText(TagStruct *currenttag)
{
	char	*begin;
	char	*end;
	int		length;
	char	*string;
	char	*filteredstring;

	if (currenttag == NULL)
		return NULL;

	if (currenttag->sontag != NULL)
	{
		begin = currenttag->end;
		end = currenttag->sontag->begin;
	}
	else if (currenttag->endtag != NULL)
	{
		begin = currenttag->end;
		end = currenttag->endtag->begin;
	}
	else if (currenttag->brothertag != NULL)
	{
		begin = currenttag->end;
		end = currenttag->brothertag->begin;
	}
	else if (currenttag->nexttag != NULL)
	{
		begin = currenttag->end;
		end = currenttag->nexttag->begin;
	}
	else
		return NULL;

	length = end - begin;

	string = (char*) malloc((length+1)*sizeof(char));
	if (string == NULL)
	{
		MMechostr(MSKDEBUG,"HTMLTEXT::GetText : could not allocate memory for string (length : '%i')\n",length);
		return NULL;
	}
	memcpy(string,begin,length);
	string[length]='\0';

	filteredstring = FilterText(string);
	free(string);

	return filteredstring;
}

char *GetText2(TagStruct *currenttag)
{
	char	*begin;
	char	*end;
	int		length;
	char	*string;
	char	*filteredstring;

	if (currenttag == NULL)
		return NULL;

	if (currenttag->brothertag == NULL)
	{
		if (currenttag->nexttag == NULL)
			return NULL;
		else
		{
			begin = currenttag->end;
			end = currenttag->nexttag->begin;
		}
	}
	else
	{
		begin = currenttag->end;
		end = currenttag->brothertag->begin;
	}

	length = end - begin;

	string = (char*) malloc((length+1)*sizeof(char));
	if (string == NULL)
	{
		MMechostr(MSKDEBUG,"HTMLTEXT::GetText2 : could not allocate memory for string (length : '%i')\n",length);
		return NULL;
	}
	memcpy(string,begin,length);
	string[length]='\0';

	filteredstring = FilterText(string);
	free(string);

	return filteredstring;
}

#endif