/*
**	GadTools layout toolkit
**
**	Copyright © 1993-1996 by Olaf `Olsen' Barthel
**		Freely distributable.
**
**	:ts=4
*/

#ifndef _GTLAYOUT_GLOBAL_H
#include "gtlayout_global.h"
#endif

#ifdef DO_FRACTION_KIND
/****** gtlayout.library/LT_Fixed2String ******************************************
*
*   NAME
*	LT_Fixed2String -- Convert a fixed-point number into an alphanumeric
*	                   string.
*
*   SYNOPSIS
*	LT_Fixed2String(Fixed,String);
*	                  D0    D1
*
*	VOID LT_Fixed2String(FIXED,STRPTR);
*
*   FUNCTION
*	This routine implements a service similar to ftoa(). It converts
*	a fixed point number into an ASCII representation.
*
*   INPUTS
*	Fixed - Fixed point numeric value
*
*	String - Buffer to receive the converted value. This buffer
*	    must be at least 12 bytes large.
*
*   NOTES
*	The decimal point will be taken from the current locale.
*	If locale.library is not installed in your system, the
*	`.' character will be used instead.
*
*	The arguments are passed in D0 and D1, this is *not* a
*	typo.
*
*   BUGS
*	Works reliably, but the entire FIXED datatype operations
*	are broken due to design faults.
*
*   SEE ALSO
*	gtlayout.library/LT_String2Fixed
*
******************************************************************************
*
*/

VOID LIBENT
LT_Fixed2String(_REG(d0) FIXED fixed,_REG(d1) STRPTR buffer)
{
	SPrintf(buffer,"%ld%lc%ld",(LONG)(fixed / FIXED_UNITY),(LONG)(LTP_Locale ? LTP_Locale->loc_DecimalPoint[0] : '.'),(LONG)(fixed % FIXED_UNITY));
}
#endif


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


#ifdef DO_FRACTION_KIND

/****** gtlayout.library/LT_String2Fixed ******************************************
*
*   NAME
*	LT_String2Fixed -- Convert an ASCII string into a fixed-point numeric
*	                   value.
*
*   SYNOPSIS
*	Fixed = LT_String2Fixed(String)
*	  D0                      A0
*
*	FIXED LT_String2Fixed(STRPTR);
*
*   FUNCTION
*	This routine complements LT_Fixed2String, it implements a
*	service similar to atof() in converting an ASCII string
*	into a fixed-point number.
*
*   INPUTS
*	String - Pointer to null-terminated string.
*
*   RESULT
*	Fixed - Fixed-point number; will be 0.0 if non-numeric
*	    characters are found in the input string.
*
*   NOTES
*	The decimal point to look for will be taken from the
*	current locale settings. If locale.library is not
*	installed, the `.' character will be used instead.
*
*   BUGS
*	Works reliably, but the entire FIXED datatype operations
*	are broken due to design faults.
*
******************************************************************************
*
*/

FIXED LIBENT
LT_String2Fixed(_REG(a0) STRPTR buffer)
{
	UBYTE	localBuffer[20];
	LONG	decimalPoint;
	LONG	i,left,right;

	if(LTP_Locale)
		decimalPoint = LTP_Locale->loc_DecimalPoint[0];
	else
		decimalPoint = '.';

	strcpy(localBuffer,buffer);

	i = 0;

	while(buffer[i])
	{
		if(localBuffer[i] == decimalPoint)
		{
			localBuffer[i] = 0;

			if(i)
				left = LTP_Atol(localBuffer);
			else
				left = 0;

			if(localBuffer[i + 1])
			{
				UBYTE rest[4];

				CopyMem(&localBuffer[i + 1],rest,3);

				rest[3] = 0;

				right = LTP_Atol(rest);
			}
			else
				right = 0;

			return((FIXED)(left * FIXED_UNITY + right));
		}

		i++;
	}

	left = LTP_Atol(localBuffer);

	return((FIXED)(left * FIXED_UNITY));
}
#endif


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


#ifdef DO_FRACTION_KIND
/****** gtlayout.library/LT_FixedMult ******************************************
*
*   NAME
*	LT_FixedMult -- Multiply a fixed-point number with an integer value.
*
*   SYNOPSIS
*	Fixed = LT_FixedMult(Fixed,Factor)
*	  D0                   D0    D1
*
*	FIXED LT_FixedMult(FIXED,ULONG);
*
*   FUNCTION
*	The layout engine provides only a fixed point multiplication
*	routine. You need to implement other numeric operations on
*	your own.
*
*   INPUTS
*	Fixed - Fixed-point number to be multiplied.
*
*	Factor - Integer value to multiply fixed-point number with.
*
*   RESULT
*	Fixed - Product
*
*   BUGS
*	Works reliably, but the entire FIXED datatype operations
*	are broken due to design faults.
*
******************************************************************************
*
*/

ULONG LIBENT
LT_FixedMult(_REG(d0) FIXED fixed,_REG(d1) ULONG factor)
{
	return((fixed * factor) / FIXED_UNITY);
}
#endif
