//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307	 USA
//-----------------------------------------------------------------------------
// package	libh/html
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "html_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	html_asis							
//									
// purpose	Output a string so that it displays as is in HTML.
//		Any HTML in the string is encoded so that the HTML
//		code simply appears to the user of the browser.
//		Also control characters will appear as equivalents
//		in a backslash or carat encoded way.
//									
// arguments	1 (char *) pointer to character string			
//		2 (size_t) length of character string			
//
// note		Give the length as ~0 (bit inverse of 0) to cause the
//		length to be determined by strlen() for a string that
//		is null terminated.
//									
// returns	(int) 0 : nothing meaningful
//-----------------------------------------------------------------------------
int
html_asis (
    char *  str
    ,
    size_t  len
    )
    __PROTO_END__
{
    //-- Determine the length if not given.
    if ( len == ~0 ) len = strlen( str );

    //-- Loop through each character in the string.
    while ( len -- ) {
	const char * p;
	unsigned int c;

	//-- Get one character as unsigned (0..255).
	c = (unsigned int) ( * (unsigned char *) str );

	switch ( c ) {

	//-- Handle certain cases special.
	case '\a':	p = "\\a";	break;
	case '\b':	p = "\\b";	break;
	case '\f':	p = "\\f";	break;
	case '\n':	p = "\\n";	break;
	case '\r':	p = "\\r";	break;
	case '\t':	p = "\\t";	break;
	case '\v':	p = "\\v";	break;

	case '"':	p = "&quot;";	break;
	case '&':	p = "&amp;";	break;
	case '<':	p = "&lt;";	break;
	case '>':	p = "&gt;";	break;

	default:
	    p = NULL;
	    //-- Handle ^@ and ^A .. ^Z here.
	    if ( c < 26 )	printf( "^%c", c + 64 );

	    //-- Handle the rest of the controls as octal.
	    else if ( c < 32 )	printf( "\\%03o" , c );

	    //-- Let the browser try to render high characters.
	    else if ( c > 126 )	printf( "&#%03d;" , c );

	    //-- Everything else is just printed as is.
	    else		putchar( c );
	}

	//-- If there was a special string, print it.
	if ( p ) fputs( p , stdout );

	//-- next...
	++ str;
    }

    return 0;
}

