/**********************************************************************
 
	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	<stdlib.h>
#include	"memory_debug.h"
#include	"utils.h"
#include	"long_char.h"


L_CHAR *
l_string(CODE_METHOD * cm,char * str)
{
L_CHAR * ret,*dest;
int i;
int len;
void * work;
unsigned char * src;


	if ( str == 0 )
		return 0; 

	len = strlen(str)+1;
	ret = d_alloc(sizeof(L_CHAR)*(len+1),101);
	work = (*cm->open)();
	dest = ret;
	src = (unsigned char*)str;


	len = 0;
	for ( ; *src ; src ++ ) {
		if ( (*cm->to_internal)(dest,work,*src) ) {
			dest ++;
			len ++;
		}
	}
	(*cm->close)(0,work);
	*dest = 0;
	ret = d_re_alloc(ret,(len+1)*sizeof(L_CHAR));
	set_buffer(ret);


	return ret;
}
