// ------------------------------------------------------------------------
/*
	\author: Jaap Suter

	Copyright (C) Electronic Arts Canada Inc. 2002.  All rights reserved.
*/
// ------------------------------------------------------------------------

/*! \file */

#ifndef REVEAL_UTILITY_H
#define REVEAL_UTILITY_H

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

#ifdef __EA_GCCXML
#define assert      // allows the cpp-xml parser to parse this header without issue
#endif

#if defined  _MSC_VER
// unreferenced local function has been removed - happens when clients
// do not #define REVEAL when this file is included.
#pragma warning( disable : 4505 )
#endif

#if defined __GNUC__
#define _snprintf snprintf
#endif

namespace Reveal {
	namespace Util {

// -------------------------------------------------------------------------------
/*!
	\brief Check for position inside rectangle;
    \return True if the given coordinates are in the
			given rectangle (top left inclusive, bottom right exclusive), false otherwise;
*/
// -------------------------------------------------------------------------------
inline bool PointInRect( int x, int y, int rectX, int rectY, int rectWidth, int rectHeight )
{
    return (x >= rectX) && (x < (rectX + rectWidth ))
        && (y >= rectY) && (y < (rectY + rectHeight));
}

// -------------------------------------------------------------------------------
/*!
	\brief Converts an integer to its string representation;
*/
// -------------------------------------------------------------------------------
inline size_t ToString( int n, char* buffer, size_t size )
{
	assert( buffer );
	assert( size > 7 ); // Remove warning disable once size is used again in release builds too;
	_snprintf( buffer, size, "%i", n );
	buffer[size - 1] = '\0';
	return strlen( buffer );
}

// -------------------------------------------------------------------------------
/*!
	\brief Converts an unsigned integer to its string representation;
	\brief Fix cast;
*/
// -------------------------------------------------------------------------------
inline size_t ToString( unsigned int n, char* buffer, size_t size )
{
	return ToString( static_cast< int >( n ), buffer, size );
}

// -------------------------------------------------------------------------------
/*!
	\brief Converts a float to its string representation;
*/
// -------------------------------------------------------------------------------
inline size_t ToString( float f, char* buffer, size_t size )
{
    assert( buffer );
	assert( size > 15 );

	_snprintf( buffer, size, "%.3Lf",(long double)f );
	buffer[size - 1] = '\0';
	return strlen( buffer );
}

// -------------------------------------------------------------------------------
/*!
	\brief Converts a string to its floating point representation;
*/
// -------------------------------------------------------------------------------
inline float StringToFloat( const char* string )
{
	float n = 0;
	float f = 0;
	bool doingFraction = false;
	size_t numDigitsInFraction = 0;

	float negative = 1.0f;
	if ( *string == '-' )
	{
		++string;
		negative = -1.0f;
	}

	while ( *string != '\0' )
	{
		if ( *string == '.' )
		{
			doingFraction = true;
		}
		else if ( !isdigit( *string ) )
		{
			break;
		}
		else
		if ( doingFraction )
		{
			++numDigitsInFraction;
			f *= 10.0f;
			f += *string - '0';
		}
		else
		{
			n *= 10.0f;
			n += *string - '0';
		}

		++string;
	}

	while ( numDigitsInFraction-- ) f /= 10.0f;

	return (n + f) * negative;
}

	} // Namespace Util;
} // Namespace Reveal;

#if defined __GNUC__
#undef _snprintf 
#endif

#endif
