
/****** vartools/str2num ******************************************************
*
*  NAME
*		str2num -- convert hex or dec string to long
*
*  SYNOPSIS
*		success = str2num(string, number)
*
*		BOOL str2num(STRPTR, LONG *)
*
*  FUNCTION
*		This function converts decimal or hexdecimal string to long integer.
*		Hex string must start with '$' or '0x'.
*
*  INPUTS
*		string	-	string to convert
*		number	-	pointer to long, where result will be stored
*
*  RESULT
*		0 for success, non-zero for failure
*
*	NOTE
*		This function uses SAS/C specific library functions (stcd_l and stch_l).
*
******************************************************************************/

#include <exec/types.h>
#include <string.h>

BOOL str2num(STRPTR s, LONG *num)
{
	int (* conv)(const char *, long *);
	LONG tmp;

	if (*s == '$')
		s++, conv = stch_l;
	else
		if (s[0] == '0' && s[1] == 'x')
			s += 2, conv = stch_l;
		else
			conv = stcd_l;

	if ((conv(s, &tmp) != strlen(s)) || *s == 0)
		return TRUE; /* error */

	*num = tmp;

	return NULL;  /* ok */
}
