Revision 337 (by dpavlin, 2004/06/10 19:22:40) new trunk for webpac v2
/*
	openisis - an open implementation of the CDS/ISIS database
	Version 0.8.x (patchlevel see file Version)
	Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org

	This library 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

	see README for more information
EOH */

/*
	$Id: lcs.c,v 1.8 2003/04/08 00:20:52 kripke Exp $
	charset support
*/

#include <string.h> /* memset */

#include "luti.h"
#include "lio.h"
#include "lcs.h"


/* ************************************************************
	private types
*/
/* ************************************************************
	private data
*/
/* ************************************************************
	private functions
*/
/* ************************************************************
	package functions
*/
unsigned char lcs_latin1_uc[256] = {
	  0,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
	' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
	' ','!','"','#','$','%','&','\'','(',')','*','+',',','-','.','/',
	'0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?',
	'@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
	'P','Q','R','S','T','U','V','W','X','Y','Z','[','\\',']','^','_',
	'`','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
	'P','Q','R','S','T','U','V','W','X','Y','Z','{','|','}','~',' ',
	' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
	' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
	' ',161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
	176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
	'A','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I',
	'T','N','O','O','O','O','O',215,'O','U','U','U','U','Y','T','S',
	'A','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I',
	'T','N','O','O','O','O','O',247,'O','U','U','U','U','Y','T','Y'
};

unsigned char lcs_latin1_ct[256] = {
/* NUL   STX   SOT   ETX   EOT   ENQ   ACK   BEL */
  LCS_F,LCS_C,LCS_C,LCS_C,LCS_C,LCS_C,LCS_C,LCS_C,
/*  BS    HT    LF    VT    FF    CR    SO    SI */
  LCS_C,LCS_C,LCS_F,LCS_C,LCS_R,LCS_F,LCS_C,LCS_C,
/* DLE   DC1   DC2   DC3   DC4   NAK   SYN   ETB */
  LCS_C,LCS_C,LCS_C,LCS_C,LCS_C,LCS_C,LCS_C,LCS_C,
/* CAN    EM   SUB   ESC    FS    GS    RS    US */
  LCS_C,LCS_C,LCS_C,LCS_C,LCS_C,LCS_R,LCS_F,LCS_C,
/*blank   !     "     #     $     %     &     '  */
  LCS_S,LCS_P,LCS_P,LCS_P,LCS_P,LCS_P,LCS_P,LCS_P,
/*  (     )     *     +     ,     -     .     /  */
  LCS_P,LCS_P,LCS_P,LCS_P,LCS_T,LCS_P,LCS_P,LCS_P,
/*  0     1     2     3     4     5     6     7  */
  LCS_D,LCS_D,LCS_D,LCS_D,LCS_D,LCS_D,LCS_D,LCS_D,
/*  8     9     :     ;     <     =     >     ?  */
  LCS_D,LCS_D,LCS_T,LCS_T,LCS_P,LCS_T,LCS_P,LCS_P,
/*  @     A     B     C     D     E     F     G  */
  LCS_P,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,
  LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A, /* H-O */
  LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A, /* P-W */
/*  X     Y     Z     [     \     ]     ^     _  */
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S_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,
  LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_Y,
  LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A,LCS_A
};


/* ************************************************************
	package functions
*/
int lcs_mktab ( LcsTab *dst, char *n, int l, int bits )
{
	int ret = 0;
	unsigned char *c = dst->c;
	unsigned short *u = dst->u;
	int line = 1;
	int i = 0;
	memset( dst, 0, sizeof(*dst) );
	for (;; i++ ) {
		unsigned short in = 0;
		for ( ; l && (*n < '0' || '9' < *n); n++, l-- ) /* skip nondigs */
			if ( '\n' == *n )
				line++;
		if ( ! l )
			break;
		for ( ; l && '0' <= *n && *n <= '9'; n++, l--)
			in = 10*in + *n - '0';
		if ( bits < 0 ) {
			if ( i > 127 ) {
				log_msg( LOG_WARN, "too many unicode values in input" );
				return 0;
			}
			if ( in <= 0xffff )
				u[i] = in;
			else
				log_msg( ERR_INVAL, "unicode %d too big %dth value line %d",
					in, i, line );
			continue;
		}
		if ( in > 0xff )
			log_msg( ERR_INVAL, "byte %d too big %dth value line %d",
				in, i, line );
		else if ( bits )
			c[in] |= bits;
		else if ( i > 255 ) {
			log_msg( LOG_WARN, "too many values in input" );
			return 0;
		} else
			c[i] = (unsigned char)in;
	}
	return ret;
}	/* lcs_mktab */


int lcs_mkrecod ( unsigned char *dst, unsigned short *from,
	unsigned short *to )
{
	int i = 0, j;
	for ( i=0; i<128; i++ )
		dst[i] = (unsigned char)i; /* identity */
	for ( ; i<256; i++ ) {
		unsigned short s = from[ i - 128 ];
		dst[i] = ' ';
		if ( to )
			for ( j=128; j--; ) {
				if ( s == to[j] ) {
					dst[i] = (unsigned char)128+j;
					break;
				}
			}
		else if ( 256 > s ) /* map to latin 1 */
			dst[i] = (unsigned char)s;
	}
	return 0;
}	/* lcs_mkrecod */


/* ************************************************************
	public functions
*/