///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_DebugFileStream.cpp
// Author : Andy Tate (PC version)
//			modified for PS2 by Mike Healey
// Date   : 14-Jun-2005
//
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Include files...
////////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

// This file's header...
#include "FileAccess/Asura_DebugFileStream.h"

// Asura header-files...
#include "Debugging/Asura_Assert.h"

// PS2 includes
#include "Core/Asura_PS2_OS.h"

#include <sifdev.h>


////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::Open
// Author		: Andy Tate (PC version)
//				: modified for PS2 by Mike Healey
// Date         : 9-Jun-2005
//
// Description  : Opens the specified file for either reading or writing only.
//
// Parameters   : The type of open to perform.
//
// Return value : "true" if successful, "false" if failed to open.
////////////////////////////////////////////////////////////////////////////////

bool Asura_DebugFileStream::Open (const OpenType eOpenType)
{
	const char*	szFilename	= m_xFilename.GetString ();

	// Is there already a file open?
	if (m_bIsOpen)
	{
		// Yes - report an error...
		Asura_Assertf	(!m_bIsOpen, ("\"%s\" has already been opened.", szFilename));
		return			(false);
	}
	else
	{
		// No - do we want to read the file?
		switch (eOpenType)
		{
			case READ_DATA:
			{
				m_iFileHandle = sceOpen(szFilename, SCE_RDONLY);
				break;
			}
			case WRITE_DATA:
			{
				m_iFileHandle = sceOpen(szFilename, SCE_WRONLY | SCE_CREAT);
				break;
			}
			default:
			{
				Asura_Assertf	(false, ("Unexpected \"Ausra_FileStream\" open type specified for \"%s\".", szFilename));
				break;
			}
		}		// End of switch.

		// Set the flags...
		m_eOpenType	= eOpenType;
		m_bIsOpen	= m_iFileHandle >= 0;

		// Return whether we were successful...
		return	(m_bIsOpen);
	}
}

/// End of function 'Asura_DebugFileStream::Open'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::Open
// Author		: Andy Tate (PC version)
//				: modified for PS2 by Mike Healey
// Date         : 9-Jun-2005
//
// Description  : Opens the specified file for either reading or writing only.
//
// Parameters   : The name of the file to open.
//
// Return value : "true" if successful, "false" if failed to open.
////////////////////////////////////////////////////////////////////////////////

bool Asura_DebugFileStream::Open (const char* szFilename,
							 const OpenType eOpenType,
							 const bool bUseFullPath)
{
	// Is there already a file open?
	if (m_bIsOpen)
	{
		// Yes - report an error...
		Asura_Assertf	(!m_bIsOpen, ("Cannot open \"%s\" - \"%s\" is already open.", szFilename, m_xFilename.GetString()));
		return false;
	}
	else
	{
		// No - do we need to use the full path?
		if (bUseFullPath)
		{
			// Yes - do so...
#ifdef ASURA_PS2_CDEMUL
            // (MH - 18.11.2005) assume we want to read/write files on host0 in this case
			m_xFilename	= Asura_PS2_OS::GetFullPathOnPC(szFilename);
#else
			m_xFilename	= Asura_PS2_OS::GetFullPath(szFilename);
#endif
		}
		else
		{
			// No - simple copy the filename...
			m_xFilename	= szFilename;
		}

		// Open this file...
		return	(Open(eOpenType));
	}
}

/// End of function 'Asura_DebugFileStream::Open'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::ReadData
// Author		: Andy Tate (PC version)
//				: modified for PS2 by Mike Healey
// Date         : 9-Jun-2005
//
// Description  : Reads the specified amount of data from the stream into the
//				  buffer given.
//
// Parameters   : The number of bytes to be read and a pointer to the address
//				  to read the data into.
//
// Return value : The number of bytes successfully read.
////////////////////////////////////////////////////////////////////////////////

u_int Asura_DebugFileStream::ReadData (const u_int uNumberOfBytes,
								  void* pData)
{
	// Are we able to read?
	if (IsReading())
	{
		int	iNumBytesRead;

		// Yes - read the data...
		iNumBytesRead = sceRead(m_iFileHandle, pData, uNumberOfBytes);

		// Yes - return the number of bytes read...
		return	(static_cast<u_int>(iNumBytesRead));
	}
	else
	{
		// No - cannot read anything...
		return	(0);
	}
}

/// End of function 'Asura_DebugFileStream::ReadData'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::SkipData
// Author		: Andy Tate (PC version)
//				: modified for PS2 by Mike Healey
// Date         : 9-Jun-2005
//
// Description  : Reads the specified amount of data from the stream into the
//				  buffer given.
//
// Parameters   : A pointer to the address to read the data into and the number
//				  of bytes to be read.
//
// Return value : The number of bytes successfully read.
////////////////////////////////////////////////////////////////////////////////

u_int Asura_DebugFileStream::SkipData (const u_int uNumberOfBytes)
{
	// Is this file open?
	if (IsOpen())
	{
		u_int	uNumBytesSkipped	= 0;

		// Yes - return the number of bytes skipped...
		return	(uNumBytesSkipped);
	}
	else
	{
		// No - cannot read anything...
		return	(0);
	}
}

/// End of function 'Asura_DebugFileStream::SkipData'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::WriteData
// Author		: Andy Tate (PC version)
//				: modified for PS2 by Mike Healey
// Date         : 9-Jun-2005
//
// Description  : Writes the specified amount of data from the stream into the
//				  buffer given.
//
// Parameters   : The number of bytes to be written and a pointer to the address
//				  to write the data into.
//
// Return value : The number of bytes successfully write.
////////////////////////////////////////////////////////////////////////////////

u_int Asura_DebugFileStream::WriteData (const u_int uNumberOfBytes,
								   const void* pData)
{
	// Are we able to write?
	if (IsWriting())
	{
		int	iNumBytesWritten;

		// Yes - write the data...
		iNumBytesWritten = sceWrite(m_iFileHandle, pData, uNumberOfBytes);

		// Return the number of bytes written...
		return	(static_cast<u_int>(iNumBytesWritten));
	}
	else
	{
		// No - cannot write anything...
		return	(0);
	}
}

/// End of function 'Asura_DebugFileStream::WriteData'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::Close
// Author		: Andy Tate (PC version)
//				: modified for PS2 by Mike Healey
// Date         : 10-Jun-2005
//
// Description  : Closes the currently open file.
//
// Return value : "true" if closed successfully, "false" if not.
////////////////////////////////////////////////////////////////////////////////

bool Asura_DebugFileStream::Close (void)
{
	// Is the file open?
	if (m_bIsOpen)
	{
		// Yes - simply close the file...
		sceClose(m_iFileHandle);

		// Reset the flags...
		m_iFileHandle	= -1;
		m_bIsOpen		= false;

		// Return success...
		return	(true);
	}
	else
	{
		// No - can't close it then...
		return	(false);
	}
}

/// End of function 'Asura_DebugFileStream::Close'. ///


/// End of file. ///
