/*

cls_2: Curl Like Syntax
Yauheni A. Akhotnikau (C) 2001-2003
eao197@yahoo.com
-------------------------------------------------

Permission is granted to anyone to use this software for any purpose on any
computer system, and to redistribute it freely, subject to the following
restrictions:

1. This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2. The origin of this software must not be misrepresented, either by
   explicit claim or by omission.

3. Altered versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

-------------------------------------------------

*/
/*!
	\file
	\brief	Внутрение функции проверки и преобразования
		строковых представлений чисел в различных форматах.
*/

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

namespace cls_2
{

bool
check_hex_digits( const char * p ) {
	static char digits[] = "0123456789abcdefABCDEF";
	while( *p ) {
		if( !strchr( digits, *p ) )
			return false;
		++p;
	}

	return true;
}

bool
check_oct_digits( const char * p ) {
	static char digits[] = "01234567";
	while( *p ) {
		if( !strchr( digits, *p ) )
			return false;
		++p;
	}

	return true;
}

bool
check_bin_digits( const char * p ) {
	static char digits[] = "01";
	while( *p ) {
		if( !strchr( digits, *p ) )
			return false;
		++p;
	}

	return true;
}

bool
check_dec_digits( const char * p ) {
	static char digits[] = "0123456789";
	while( *p ) {
		if( !strchr( digits, *p ) )
			return false;
		++p;
	}

	return true;
}

bool
check_double_digits( const char * p ) {
	static char digits[] = "0123456789.eE-+";
	while( *p ) {
		if( !strchr( digits, *p ) )
			return false;
		++p;
	}

	return true;
}

static void
parse_binary(
	const char * s,
	long & result ) {
	result = 0;
	while( *s ) {
		result <<= 1;
		if( '1' == *(s++) )
			result |= 1;
	}
}

bool
parse_digits(
	const char * pcsz,
	long & result,
	char base_mark )
{
	bool is_negative = false;
	if( '-' == *pcsz ) {
		is_negative = true;
		++pcsz;
	}

	if( base_mark == *pcsz )
	{
		if( 3 <= strlen( pcsz ) )
		{
			// This is hexdecimal, octal or binary value
			if( is_negative )
				// Negative value cannot be specified
				// in hexadecimal, octal ot binary with
				// minus sign
				return false;

			if( 'X' == toupper( pcsz[ 1 ] ) )
			{
				// This is hexdecimal value
				if( check_hex_digits( pcsz + 2 ) &&
					1 == sscanf( pcsz + 2, "%lx", &result ) )
					return true;
			}
			else if( 'O' == toupper( pcsz[ 1 ] ) )
			{
				// This is octal value
				if( check_oct_digits( pcsz + 2 ) &&
					1 == sscanf( pcsz + 2, "%lo", &result ) )
					return true;
			}
			else if( 'B' == toupper( pcsz[ 1 ] ) )
			{
				// This is binary value
				if( check_bin_digits( pcsz + 2 ) )
				{
					parse_binary( pcsz + 2, result );
					return true;
				}
			}
		}
		else if( 1 == strlen( pcsz ) )
		{
			result = 0;
			return true;
		}
	}
	else
		if( check_dec_digits( pcsz ) &&
			1 == sscanf( pcsz, "%ld", &result ) )
		{
			if( is_negative )
				result = -result;

			return true;
		}

	return false;
}

static void
parse_binary(
	const char * s,
	unsigned long & result )
{
	result = 0;
	while( *s )
	{
		result <<= 1;
		if( '1' == *(s++) )
			result |= 1;
	}
}

bool
parse_digits(
	const char * pcsz,
	unsigned long & result,
	char base_mark )
{
	if( base_mark == *pcsz )
	{
		if( 3 <= strlen( pcsz ) )
		{
			// This is hexdecimal, octal or binary value
			if( 'X' == toupper( pcsz[ 1 ] ) )
			{
				// This is hexdecimal value
				if( check_hex_digits( pcsz + 2 ) &&
					1 == sscanf( pcsz + 2, "%lx", &result ) )
					return true;
			}
			else if( 'O' == toupper( pcsz[ 1 ] ) )
			{
				// This is octal value
				if( check_oct_digits( pcsz + 2 ) &&
					1 == sscanf( pcsz + 2, "%lo", &result ) )
					return true;
			}
			else if( 'B' == toupper( pcsz[ 1 ] ) )
			{
				// This is binary value
				if( check_bin_digits( pcsz + 2 ) )
				{
					parse_binary( pcsz + 2, result );
					return true;
				}
			}
		}
		else if( 1 == strlen( pcsz ) )
		{
			result = 0;
			return true;
		}
	}
	else
		if( check_dec_digits( pcsz ) &&
			1 == sscanf( pcsz, "%lu", &result ) )
			return true;

	return false;
}

/*
	Try parse double value.
	Representations:
		[-][d*][.d*][[eE][-][d*]]

	Returns true on success
*/
bool
parse_digits( const char * pcsz, double & result ) {
	bool is_negative = false;
	if( '-' == *pcsz ) {
		is_negative = true;
		++pcsz;
	}

	if( check_double_digits( pcsz ) &&
		1 == sscanf( pcsz, "%lg", &result ) ) {
		if( is_negative )
			result = -result;

		return true;
	}

	return false;
}

} /* namespace cls_2 */

