/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program 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.

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



#include	"memory_debug.h"
#include	"long_char.h"

L_CHAR * 
wct_conv(WCT * wct,L_CHAR * str)
{
L_CHAR mask;
L_CHAR ch;
WRITABLE_CODE_TABLE * t;
L_CHAR ret,* ret_str;
int size;
char buf[20],* bp;
int ptr;
	size = l_strlen(str)+10;
	ret_str = d_alloc(size*sizeof(L_CHAR));
	ptr = 0;
	for ( ; *str ; str ++ ) {
		ch = *str;
		t = get_wct(wct,ch);
		if ( t == 0 )
			goto undef;
		for ( ; t->tbl == 0 ; )
			t = t->alias;
		ret = t->tbl[ch&(~t->mask)];
		if ( ret ) {
			ret_str[ptr++] = ret;
			continue;
		}
	undef:
		sprintf(buf,"&cx%x;",*str);
		size += strlen(buf+1);
		ret_str = d_re_alloc(ret_str,size*sizeof(L_CHAR));
		for ( bp = buf ; *bp ; bp ++ )
			ret_str[ptr++] = ((L_CHAR)(*bp))&0x0ff;
	}
	ret_str[ptr] = 0;
	return ret_str;
}
