/*
 * FISG - Helper functions for XML/HTML output modules
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2004 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#include "out_xhtmlgen.h"
#include "th_string.h"
#include <stdio.h>

typedef struct {
	int	entChar;
	char	*entStr;
} t_xmlentity;


t_xmlentity	xmlEntities[] = {
	{ '<',	"&lt;" },
	{ '>',	"&gt;" },
	{ '&',	"&amp;" },

	{ '¡',	"&iexcl;" },
	{ '¢',	"&cent;" },
	{ '£',	"&pound;" },
	{ '¤',	"&curren;" },
	{ '¥',	"&yen;" },
	{ '¦',	"&brvbar;" },
	{ '§',	"&sect;" },
	{ '¨',	"&uml;" },
	{ '©',	"&copy;" },
	{ 'ª',	"&ordf;" },
	{ '«',	"&laquo;" },
	{ '¬',	"&not;" },
	{ '­',	"&shy;" },
	{ '®',	"&reg;" },
	{ '¯',	"&macr;" },
	{ '°',	"&deg;" },
	{ '±',	"&plusmn;" },
	{ '²',	"&sup2;" },
	{ '³',	"&sup3;" },
	{ '´',	"&acute;" },
	{ 'µ',	"&micro;" },
	{ '¶',	"&para;" },
	{ '·',	"&middot;" },
	{ '¸',	"&cedil;" },
	{ '¹',	"&sup1;" },
	{ 'º',	"&ordm;" },
	{ '»',	"&raquo;" },
	{ '¼',	"&frac14;" },
	{ '½',	"&frac12;" },
	{ '¾',	"&frac34;" },
	{ '¿',	"&iquest;" },
	{ 'À',	"&Agrave;" },
	{ 'Á',	"&Aacute;" },
	{ 'Â',	"&Acirc;" },
	{ 'Ã',	"&Atilde;" },
	{ 'Ä',	"&Auml;" },
	{ 'Å',	"&Aring;" },
	{ 'Æ',	"&AElig;" },
	{ 'Ç',	"&Ccedil;" },
	{ 'È',	"&Egrave;" },
	{ 'É',	"&Eacute;" },
	{ 'Ê',	"&Ecirc;" },
	{ 'Ë',	"&Euml;" },
	{ 'Ì',	"&Igrave;" },
	{ 'Í',	"&Iacute;" },
	{ 'Î',	"&Icirc;" },
	{ 'Ï',	"&Iuml;" },
	{ 'Ð',	"&ETH;" },
	{ 'Ñ',	"&Ntilde;" },
	{ 'Ò',	"&Ograve;" },
	{ 'Ó',	"&Oacute;" },
	{ 'Ô',	"&Ocirc;" },
	{ 'Õ',	"&Otilde;" },
	{ 'Ö',	"&Ouml;" },
	{ '×',	"&times;" },
	{ 'Ø',	"&Oslash;" },
	{ 'Ù',	"&Ugrave;" },
	{ 'Ú',	"&Uacute;" },
	{ 'Û',	"&Ucirc;" },
	{ 'Ü',	"&Uuml;" },
	{ 'Ý',	"&Yacute;" },
	{ 'Þ',	"&THORN;" },
	{ 'ß',	"&szlig;" },
	{ 'à',	"&agrave;" },
	{ 'á',	"&aacute;" },
	{ 'â',	"&acirc;" },
	{ 'ã',	"&atilde;" },
	{ 'ä',	"&auml;" },
	{ 'å',	"&aring;" },
	{ 'æ',	"&aelig;" },
	{ 'ç',	"&ccedil;" },
	{ 'è',	"&egrave;" },
	{ 'é',	"&eacute;" },
	{ 'ê',	"&ecirc;" },
	{ 'ë',	"&euml;" },
	{ 'ì',	"&igrave;" },
	{ 'í',	"&iacute;" },
	{ 'î',	"&icirc;" },
	{ 'ï',	"&iuml;" },
	{ 'ð',	"&eth;" },
	{ 'ñ',	"&ntilde;" },
	{ 'ò',	"&ograve;" },
	{ 'ó',	"&oacute;" },
	{ 'ô',	"&ocirc;" },
	{ 'õ',	"&otilde;" },
	{ 'ö',	"&ouml;" },
	{ '÷',	"&divide;" },
	{ 'ø',	"&oslash;" },
	{ 'ù',	"&ugrave;" },
	{ 'ú',	"&uacute;" },
	{ 'û',	"&ucirc;" },
	{ 'ü',	"&uuml;" },
	{ 'ý',	"&yacute;" },
	{ 'þ',	"&thorn;" },
	{ 'ÿ',	"&yuml;" },
};

const t_uint	nXMLEntities = (sizeof(xmlEntities) / sizeof(t_xmlentity));


int xml_fprintf_entitize(FILE *outFile, char *fmt)
{
 BOOL isFound;
 int c;
 t_uint i;
 
 while ((c = *fmt))
 	{
 	/* Check character for entities */
	for (isFound = FALSE, i = 0; (i < nXMLEntities) && !isFound; i++)
		if (xmlEntities[i].entChar == c)
			{
			isFound = TRUE;
			break;
			}

	if (isFound)
		{
		if (fputs(xmlEntities[i].entStr, outFile) == EOF)
			return EOF;
		} else
		{
 		if (fputc(c, outFile) == EOF)
 			return EOF;
 		}
	
	fmt++;
 	}

 return 0;
}


int xml_fprintf_urlencode(FILE *outFile, char *fmt)
{
 int c;
 
 while ((c = *fmt))
 	{
 	if (th_isalnum(c) || th_ispunct(c))
 		{
 		if (fputc(c, outFile) == EOF)
 			return EOF;
 		} else {
 		fprintf(outFile, "%%%.2x", c & 0xff);
 		}

	fmt++;
 	}

 return 0;
}


t_xhtmlconfig	xhtml_cfg;

int output_xhtml_init(t_config *cfg)
{
 th_config_add_str(cfg, "xhtml_image_path",	NULL, &xhtml_cfg.dataPath, ".");
 th_config_add_str(cfg, "xhtml_css_path",	NULL, &xhtml_cfg.cssPath, "style.css");
 
 return 0;
}



syntax highlighted by Code2HTML, v. 0.9.1