/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#ifndef _WCHAR_H
#define	_WCHAR_H

#include <stddef.h>

int		blc_mbtowc_any_16( void *wc, const char *mb, size_t ml );
int		blc_mbtowc_rfc_16( void *wc, const char *mb, size_t ml );
int		blc_mbtowc_any_32( void *wc, const char *mb, size_t ml );
int		blc_mbtowc_rfc_32( void *wc, const char *mb, size_t ml );
int		blc_wctomb_any( char *mb, unsigned int wc );
int		blc_wctomb_rfc( char *mb, unsigned int wc );

/** @addtogroup wchar */
/**	@{ */

/**
*	Maximum UTF-8 sequence length.
*
*	The maximum number of bytes resulting in the UTF-8 expansion of
*	a wchar_t object. If wchar_t is 16 bits wide, then it is 3. If wchat_t
*	is 32 bits and strict RFC conformance is not specified, then it is 6.
*	For 32-bit wchar_t and strict RFC conformance, it is 4.
*/

#define	MB_CUR_MAX	_mb_cur_max()

/**
*	End-of-file marker for wchar_t streams.
*/

#define	WEOF	((wchar_t) -1)

/** @} */

static __inline__ int _mb_cur_max( void )
{
#if __WCHAR_MAX__> 65535U
	#ifdef BLC_RFC3629_CONFORMANCE
		return 4;
	#else
		return 6;
	#endif
#else
	return 3;
#endif
}

static __inline__ int mbtowc( wchar_t *wc, const char *mb, size_t ml )
{
#if __WCHAR_MAX__> 65535U
	#ifdef BLC_RFC3629_CONFORMANCE
		return(blc_mbtowc_rfc_32(wc,mb,ml));
	#else
		return(blc_mbtowc_any_32(wc,mb,ml));
	#endif
#else
	#ifdef BLC_RFC3629_CONFORMANCE
		return(blc_mbtowc_rfc_16(wc,mb,ml));
	#else
		return(blc_mbtowc_any_16(wc,mb,ml));
	#endif
#endif
}

static __inline__ int mbtowci( wchar_t **pp, const char *mb, size_t ml )
{
	return(mbtowc(pp?(*pp)++:NULL,mb,ml));
}

static __inline__ int	wctomb( char *mb, wchar_t wc )
{
#ifdef BLC_RFC3629_CONFORMANCE
	return(blc_wctomb_rfc(mb,(unsigned)wc));
#else
	return(blc_wctomb_any(mb,(unsigned)wc));
#endif
}

static __inline__ int	wcitomb( char *mb, wchar_t **pp )
{
	return(wctomb(mb,*(*pp)++));
}

#endif

