#ifndef PandaWave_RESOURCES
#include "RevengeResources.h"
#endif

#include <XFile.h>
#include <assert.h>

#include "vengeance_misc.h"

static char *script_beg = 0;
static char *script_pos = 0;
static int script_lin = 1;
static int script_len = 0;
static char *script_end = 0;
static bool script_eof = FALSE;

char temp_token[1024];


float incremental_round( float value, float increment )
{
	assert( increment );
	
	float float_value = value / increment;
	long trunc_value = float_value;
	float rounded;

	if( float_value > 0.0 )
	{
		if( float_value - (float)trunc_value < 0.5 )
			rounded = trunc_value * increment;
		else
			rounded = (trunc_value + 1) * increment;
	}
	else
	{
		if( float_value - (float)trunc_value > -0.5 )
			rounded = trunc_value * increment;
		else
			rounded = (trunc_value - 1) * increment;
	}
	
	return rounded;
}


bool load_script( XGFileSpecifier *fs )
{
	FILE *fp;
	
	fp = fs->OpenFile( "r" );
	if( !fp )
		return FALSE;
	
	fseek( fp, 0, SEEK_END );
	script_len = ftell( fp );
	fseek( fp, 0, SEEK_SET );
	
	if( script_len == 0 )
	{
		fclose( fp );
		return FALSE;
	}
	
	script_beg = (char*)malloc( script_len );
	if( !script_beg )
	{
		fclose( fp );
		return FALSE;
	}
	
	memset( script_beg, 0x00, script_len );
	
	script_pos = script_beg;
	script_end = script_beg + script_len;
	
	fread( script_beg, 1, script_len, fp );
	fclose( fp );
	
	script_eof = FALSE;
	
	return TRUE;
}

void free_script( void )
{
	if( script_beg )
		free( script_beg );

	script_beg = 0;
	script_pos = 0;
	script_lin = 0;
	script_len = 0;
	script_end = 0;
	script_eof = FALSE;
}

bool parse_token( char *token, bool cross_lines, long *line )
{
	char *token_start = token;
	
	if( script_eof || !script_beg )
		return FALSE;
		
	// skip whitespace
	// oooooo, look at the fun platform differences
	while( (*script_pos < 33 || *script_pos == '/') && script_pos != script_end )
	{
		// skip comments
		if( *script_pos == '/' && *(script_pos - 1) == '/' )
		{
#if OPT_MACOS
			while( script_pos != script_end )
			{
				if( *script_pos == 0x0d )
					break;
				
				script_pos++;
			}
#endif

#if OPT_WINOS
// phook - why am I only seeing 0x0a right now?
#if 0
			while( script_pos != script_end )
			{
				if( *(script_pos - 1) == 0x0d && *script_pos == 0x0a )
					break;
				
				script_pos++;
			}
#else
			while( script_pos != script_end )
			{
				if( *script_pos == 0x0a )
					break;
				
				script_pos++;
			}
#endif
#endif

#if OPT_XWIN
			while( script_pos != script_end )
			{
				if( *script_pos == 0x0a )
					break;
				
				script_pos++;
			}
#endif
		}
		
		// check for line increments
#if OPT_MACOS
		if( *script_pos == 0x0d )
		{
			script_lin++;
			if( !cross_lines )
			{
				script_pos++;
				return FALSE;
			}
		}
#endif

#if OPT_WINOS
// phook - why am I only seeing 0x0a right now?
#if 0
		if( *(script_pos - 1) == 0x0d && *script_pos == 0x0a )
		{
			script_lin++;
			if( !cross_lines )
			{
				script_pos++;
				return FALSE;
			}
		}
#else
		if( *script_pos == 0x0a )
		{
			script_lin++;
			if( !cross_lines )
			{
				script_pos++;
				return FALSE;
			}
		}
#endif
#endif

#if OPT_XWIN
		if( *script_pos == 0x0a )
		{
			script_lin++;
			if( !cross_lines )
			{
				script_pos++;
				return FALSE;
			}
		}
#endif

		++script_pos;
	}
		
	if( script_pos == script_end )
	{
		script_eof = TRUE;
		return FALSE;
	}

	if( line )
		*line = script_lin;
		
	// copy a non-quoted token	
	if( *script_pos != '\"' )
	{
		// copy till next whitespace.
		while( *script_pos > 32 && *script_pos < 127 && script_pos != script_end )
		{
			if( token - token_start < MAX_TOKEN_LENGTH )
			{
				*token = *script_pos;
				token++;
				script_pos++;
			}
			else
				return FALSE;
		}
	}
	// copy the quoted token
	else
	{
		// copy till next quote.
		script_pos++;

		while( *script_pos != '\"' && script_pos != script_end )
		{
			// even quoted tokens shouldn't cross any lines
#if OPT_MACOS
			if( *script_pos == 0x0d )
				return FALSE;
#endif

#if OPT_WINOS
// phook - why am I only seeing 0x0a right now?
#if 0
			if( *(script_pos - 1) == 0x0d && *script_pos == 0x0a )
				return FALSE;
#else
			if( *script_pos == 0x0a )
				return FALSE;
#endif
#endif

#if OPT_XWIN
			if( *script_pos == 0x0a )
				return FALSE;
#endif
		
			if( token - token_start < MAX_TOKEN_LENGTH )
			{
				*token = *script_pos;
				token++;
				script_pos++;
			}
			else
				return FALSE;
		}
		
		// make sure we had an ending quote
		if( *script_pos != '\"' )
			return FALSE;
		
		// skip ending quote
		if( script_pos != script_end )
			script_pos++;
	}
	
	if( script_pos == script_end )
		script_eof = TRUE;
	
	*token = 0; // terminate;
	return TRUE;
}

bool parse_float( float *f )
{
	if( !parse_token( temp_token ) )
		return FALSE;
		
	*f = (float)atof( temp_token );
	
	return TRUE;
}

bool parse_int( int *i )
{
	if( !parse_token( temp_token ) )
		return FALSE;
		
	*i = atoi( temp_token );
	
	return TRUE;
}

bool seek_to_token( const char *token_to_find, bool case_sensitive )
{
/*
	int(const char *, const char *)comp;
	
	if( case_sensitive )
		comp = strcmp;
	else
		comp = stricmp;
*/
		
	while( parse_token( temp_token ) )
	{
		if( case_sensitive )
			if( !strcmp( temp_token, token_to_find ) )
				return TRUE;
		else
			if( !_stricmp( temp_token, token_to_find ) )
				return TRUE;
	}
	
	return FALSE;
}
