/**********************************************************************

	Title :                 File module
	Filename :              file.c
	Author :                Thomas Szirtes
	Revision :              1.0

	Notes :                 Allows C-style byte access to GD files

	Copyright :             (C) SEGA Europe 1998

  **********************************************************************/

/*
 *		Includes
 */

#include	"shinobi.h"
#include	<stdio.h>


/*
 *		Definitions
 */

/* assuming mode 1 */
#define		SECTOR_SIZE		2048


/*
 *		Module Data
 */
/* sector buffer */
static Uint8	sector_buffer	[SECTOR_SIZE*4];
static int		into_buf		=	SECTOR_SIZE;


/*--------------------------------------------------------------------------
 
  GDFS	t_fopen(char *fname, char *attributes)

  Parameters:
		fname		pointer to filename string
		attributes	pointer to string of attributes (ignored)

  Returns:
		file handle

  Notes:
		Attempts to open a GDROM file

  --------------------------------------------------------------------------*/
GDFS	t_fopen(char *fname, char *attributes)
{
	GDFS	gdfs;
	gdfs	=  gdFsOpen((Sint8 *)fname, NULL);
	into_buf	=	SECTOR_SIZE;
	return gdfs;
}



/*--------------------------------------------------------------------------
 
  void	t_fclose( GDFS stream )

  Parameters:
		stream		current file handle

  Returns:

  Notes:
		Closes an open file

  --------------------------------------------------------------------------*/
void	t_fclose( GDFS stream )
{
	gdFsClose( stream );
}



/*--------------------------------------------------------------------------
 
  Uint32	t_fread( Uint8 *buffer, Uint32 size, Uint32 count, GDFS stream )

  Parameters:
		buffer		pointer to destination memory area
		size		size of items to load
		count		number of items to load
		stream		current file handle

  Returns:
		number of bytes read

  Notes:
		Reads in number of data elements from a file stream

  --------------------------------------------------------------------------*/
Uint32	t_fread( Uint8 *buffer, Uint32 size, Uint32 count, GDFS stream )
{
	Uint32	num_bytes;
	Uint32	bytes_left;

	num_bytes	=	size * count;
	bytes_left	=	num_bytes;

	if (into_buf < SECTOR_SIZE )
	{
		/* We still have some bytes already buffered up */
		while (bytes_left > 0 && into_buf < SECTOR_SIZE )
		{
			*buffer		=	sector_buffer[into_buf];
			buffer		++;
			into_buf	++;
			bytes_left	--;
		}
	}

	if (bytes_left <= 0)	return num_bytes;

	/* load sectors quickly first */
	while (bytes_left > SECTOR_SIZE)
	{
		if ( gdFsRead(stream, 1, buffer) != GDFS_ERR_OK ) 
			return num_bytes - bytes_left;
		
		buffer				+=	SECTOR_SIZE;
		bytes_left			-=	SECTOR_SIZE;
	}

	/* multiple of sector size? */
	if (bytes_left <= 0)	return num_bytes;

	/* buffer up spare sector */
	if ( gdFsRead(stream, 1, &sector_buffer) != GDFS_ERR_OK )
		return	num_bytes - bytes_left;

	into_buf	=		0;

	while(bytes_left > 0)
	{
		*buffer		=	sector_buffer[into_buf];
		buffer		++;
		bytes_left	--;
		into_buf	++;
	}

	return	num_bytes - bytes_left;

}


