/**************************************************************
$Workfile:   ui_parser.c  $
***************************************************************
**
** Host Code, Copyright (C) 2000-2002
** Ikanos Communication, Fremont, CA
** All rights reserved.
**
** The information and contents of this file are the
** proprietary information of Ikanos Communication and may not
** be disclosed or used without the formal written approval of
** Ikanos Communication
**
**  
***************************************************************/

/*******  Include Files         ***************************************/


/*#define VIT_DEBUG */


#include "uiUtil.h"

#ifdef VIT_DEBUG
#include <stdio.h>
#include <stdlib.h>

typedef	  	unsigned		char 	   	U8;
typedef	  	unsigned 	short	   	U16; 
typedef	  	unsigned 	long	   	U32;
typedef		signed 		char   		S8;
typedef		signed		short			S16;
typedef		signed		long			S32;
#endif /* VIT_DEBUG */


#define BYTE unsigned char
#define TRUE  (1)
#define u8 U8
#define u16 U16
#define uint16 U16
#define u32 U32
#define s16 S16
#define s32 S32
#define NULL 0x0



#define   uip_isdigit(c)  (((c) >= '0') && ((c) <= '9'))
#define   uip_isupper(c)  (((c) >= 'A') && ((c) <= 'Z'))
#define   uip_isalnum(c)  (uip_isalpha(c) || uip_isdigit(c))
#define   uip_isalpha(c)  (uip_islower(c) || uip_isupper(c))

#define   uip_islower(c)  (((c) >= 'a') && ((c) <= 'z'))
#define   uip_toupper(c)  (uip_islower(c) ? ((c) - 'a' + 'A'): (c))


/*******  Local Data            ***************************************/
static BYTE *t_uipBufPtr;


/* *****************************************************************************
* Function      : Skip Space
* Parameters    : None
* Return        : Nothing
* Description   : 
*****************************************************************************
*/

static void uip_ParseSkipBlanks( void )
{
	while( *t_uipBufPtr == ' ' ) { t_uipBufPtr++ ; }
}
 

/*
*****************************************************************************
* Function      : Skip Pre-Hex (0x or 0X)
* Parameters    : None
* Return        : Nothing
* Description   : 
*****************************************************************************
*/
static void uip_ParseSkipPreHex( void )
{
	while( (*t_uipBufPtr == '0') || (*t_uipBufPtr == 'x')  ||
						 (*t_uipBufPtr == 'X') ) 
    { 
		t_uipBufPtr++; 
	}
}
 



/*
*****************************************************************************
* Function      : Convert Hex to Int
* Parameters    : None
* Return        : s16
* Description   : 
*****************************************************************************
*/

u16 uip_Hex2Int( u8 c )
{
 s16 value;

	if( ('0' <= c) && (c <= '9') ) 
	{ 
		value = c - '0';
	}
	else if( ('A' <= c) && (c <= 'F') ) 
	{ 
		value = c - 'A' + 10; 
	}
	else if( ('a' <= c) && (c <= 'f') )	
	{ 
		value = c - 'a' + 10; 
	}
	else
	{ 
		value = -1; 
	}

	return (value);
}


/*
*****************************************************************************
* Function      : Get user request in Hex format
* Parameters    : None
* Return        : u32
* Description   : 
*****************************************************************************
*/
u32 uip_ParseGetHex( void )
{
 	s32 result = 0;
 	s16 c;

	uip_ParseSkipBlanks();
	uip_ParseSkipPreHex();

	while( (c=uip_Hex2Int( *t_uipBufPtr )) >= 0 )
	{
		result = ( result << 4 ) + c;
		t_uipBufPtr++;
	}

	return result ;
}

/*
*****************************************************************************
* Function      : Get user request in char format
* Parameters    : None
* Return        : s16
* Description   : 
*****************************************************************************
*/
static s16 uip_ParseGetChar( void )
{
 s16 c = 0;

	if ( (c = *t_uipBufPtr) != 0 )
	{
		t_uipBufPtr++;
	}

	return(c);
}

static s16 uip_ParseToUpper( void )
{
 	s16 c = 0;

	if ( (c = *t_uipBufPtr) != 0 )
	{
		t_uipBufPtr++;
	}

	return(c);
}

/* test if the string is a Decimal number */
int uip_isDecNum(char *s)
{
   int c=0;

	if (*s =='\0') return 1;

   while (*s!='\0')
	{

	  axdslPrintf("uip_isDecNum(): digit: %c\n",*s);
	  if ((c =uip_isdigit(*s)) == 0 )
	  {
	  			axdslPrintf("uip_isDecNum(): Notdigit: %c\n",*s);
				 return 1;
	  }
	  else
			s++;
	}
	return 0;
}

/* test if the string is a hexadecimal number */
int uip_isHexNum(char *s)
{
   int c=0;

	/* Need to check for preHex value: ie 0x or 0X */

   while (*s!='\0')
	{
	  printf("digit: %c\n",*s);
	  if ((c =uip_isdigit(*s)) == 0 )
	  {
				 return 1;
	  }
	  else
			s++;
	}
	return 0;
}




/*
*****************************************************************************
* Function      : Get User Request in Long format
* Parameters    : None
* Return        : s32
* Description   : 
*****************************************************************************
*/
static s32 uip_ParseGetLong( void )
{
 	char cNeg = 0 ;
 	s32 lResult = 0;
 	int c;

	uip_ParseSkipBlanks();

	if( *t_uipBufPtr == '-' )	
	{ 
		cNeg = TRUE; 
		t_uipBufPtr++; 
	}

	while( (c=*t_uipBufPtr) >= '0' && ( c <= '9' ) )
	{
		lResult = (lResult * 10L) + (c - '0');
		t_uipBufPtr++;
	}

	if( cNeg )	
	{ 
		lResult = -lResult; 
	}

	return (lResult) ;
}



u16 uip_getToken(BYTE *uip_str, BYTE *tStr )
{
 	s16 c = 0;
	u16 ndx=0;
	int retVal=UIP_VALID_TOKEN;

	if (uip_str != NULL) 
		t_uipBufPtr = uip_str;

	if (strlen(t_uipBufPtr) <= 0)
	{ 
		tStr[ndx]= '\0';
		retVal= UIP_NOMORE_TOKEN;
		return (retVal);
	}	  
	

	axdslPrintf("t_uipBufPtr %s\n", t_uipBufPtr); 

	uip_ParseSkipBlanks ();
	if (*t_uipBufPtr=='?')
	{
			  retVal=UIP_HELP_TOKEN;
			  return (retVal);
	}

	while ( (c= *t_uipBufPtr) != 0) 
	{
 		if ( (uip_isalnum(*t_uipBufPtr)))
		{  /* alphaNum */
			tStr[ndx++]= *t_uipBufPtr;
			t_uipBufPtr++;
			continue;
		}

		/* The only allowed delimiters are , and SPACE */
	  	if ( (*t_uipBufPtr == ' ') || ( *t_uipBufPtr == ',') )
		{
			/*printf("Break on Token:  %c\n", *t_uipBufPtr); */
			t_uipBufPtr++;
		  	break;
		}
		else
		{
			/*printf("Invalid DELIMITER:  %c\n", *t_uipBufPtr); */
			retVal =UIP_INV_DELIM;	
			break;

		}
	 
	}
	tStr[ndx]= '\0';

	/*printf("Token:  %s\n", tStr); */

	/* reassign uip_str and send it back */
	/*uip_str = t_uipBufPtr; */
	return(retVal);
}

