/****************************************************************************/
/*	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.																	*/
/****************************************************************************/

#include <stdlib.h>
#include <blc_stdlib.h>

/****************************************************************************/
/*					User API functions										*/
/****************************************************************************/

/** @addtogroup stdlib */
/**	@{ */

/**
*	Converts a 32-bit unsigned integer to ASCII in radix 2^N.
*
*	The number will be converted without leading zeroes or spaces and the
*	result is terminated by a 0 byte.
*
*	@param	buff	Pointer to the buffer where the result is stored.
*					At worst case the buffer needs to be 33 bytes long.
*	@param	numb	The unsigned integer to convert
*	@param	base	Conversion base, one of 2, 4, 8, 16 or 32.
*	@param	uppc	For bases 16 or 32 if this flag is true, then digits
*					over 9 will use uppercase letters. If the flag is false,
*					then lowercase will be used.
*
*	@return			The length of the final string, not counting the
*					terminating 0. If the radix is not a power of 2
*					between 2 and 32 inclusive, then no conversion takes
*					place and 0 is returned.
*
*	@note	This function is not a standard C library function.
*/

int		ultostr2n( char *buff, unsigned long numb, int base, int uppc )
{
	return( _blc_itoa2n( buff, numb, base, uppc, 0 ) );
}

/** @} */

/****************************************************************************/
/*					Internal interface										*/
/****************************************************************************/

/*
*	Converts a 32-bit unsigned integer to ASCII in radix 2^N.
*
*	The number will be converted with or without leading zeroes and the
*	result is terminated by a NUL byte.
*
*	numb	The unsigned integer to convert
*
*	buff	Pointer to the buffer where the result is stored.
*			At worst case the buffer needs to be 11 bytes long.
*
*	base	Conversion base, one of 2, 4, 8, 16 or 32.
*
*	uppc	For bases 16 or 32 if this flag is true, then digits
*			over 9 will use uppercase letters. If the flag is false,
*			then lowercase will be used.
*
*	lead	Flag indicating whether leading zeroes should be preserved
*			or not.
*
*	Returns the length of the final string, not counting the terminating 0.
*	If the radix is not a power of 2 between 2 and 32 inclusive, then no
*	conversion takes place and 0 is returned.
*/

int		_blc_itoa2n( char *buff, unsigned long numb,
					 int base, int uppc, int lead )
{
char			*p = buff;
unsigned int	l, r, n, d;

	if ( lead ) p--;

	if ( uppc )

		uppc = 'A' - 10;
	else
		uppc = 'a' - 10;

	switch ( base ) {

		case  2:	r = 1;	n = 32;	break;
		case  4:	r = 2;	n = 16;	break;
		case  8:	r = 3;	n = 10;	break;
		case 16:	r = 4;	n =  8;	break;
		case 32:	r = 5;	n =  6;	break;
		default:	return( 0 );
	}

	if ( ! numb && ! lead ) {

		*buff++ = '0';
		*buff   = '\0';
		return( 1 );
	}

	switch ( base ) {

		case 8:
		case 32:

			if ( ( d = numb >> 30 ) || p != buff ) *buff++ = d + '0';
			numb <<= 2;
			break;
	}

	for ( l = 32 - r ; n-- ; numb <<= r ) {

		d = numb >> l;
		if ( d || p != buff ) *buff++ = d + ( d > 9 ? uppc : '0' );
	}

	if ( lead ) p++;
	*buff = '\0';
	return( buff - p );
}

