
///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_DebugFileStream.cpp
// Author : Andy Tate
// Date   : 9-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 independent header-files...

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


////////////////////////////////////////////////////////////////////////////////
// Constant declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Static variable declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Private functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Protected functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Public functions...
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::Open
// Author       : Andy Tate
// 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_bIsOpen	= Asura_PS3_FileSystem::OpenForReading (szFilename, &m_xHandle, m_bForceHost0);
				break;
			}

			case WRITE_DATA:
			{
				m_bIsOpen	= Asura_PS3_FileSystem::OpenForWriting (szFilename, &m_xHandle, m_bForceHost0);
				break;
			}

			default:
			{
				Asura_Assertf	(false, ("Unexpected \"Ausra_FileStream\" open type specified for \"%s\".", szFilename));
				break;
			}
		}		// End of switch.

		// Set the flags...
		m_eOpenType	= eOpenType;

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

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



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::Open
// Author       : Andy Tate
// 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?
		m_bForceHost0 = bUseFullPath;
		m_xFilename	= szFilename;

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

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



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::ReadData
// Author       : Andy Tate
// 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())
	{
		const u_int	uNumBytesRead	= Asura_PS3_FileSystem::ReadData (m_xHandle, uNumberOfBytes, pData);

		// Yes - return the number of bytes read...
		return	(uNumBytesRead);
	}

	else
	{
		// No - cannot read anything...
		return	(0);
	}
}

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



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::SkipData
// Author       : Andy Tate
// 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 (m_bIsOpen)
	{
		const u_int	uNumBytesSkipped	= Asura_PS3_FileSystem::SkipData (m_xHandle, uNumberOfBytes);

		// Yes - attempt to skip...
		if (0 == uNumBytesSkipped)
		{
			// Success - return the number of bytes skipped...
			return	(uNumberOfBytes);
		}

		else
		{
			// Failure - return no bytes skipped...
			return	(0);
		}
	}

	else
	{
		// No - cannot read anything...
		return	(0);
	}
}

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



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::WriteData
// Author       : Andy Tate
// 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())
	{
		const u_int	uNumBytesWritten	= Asura_PS3_FileSystem::WriteData (m_xHandle, uNumberOfBytes, pData);

		// Yes - return the number of bytes written...
		return	(uNumBytesWritten);
	}

	else
	{
		// No - cannot write anything...
		return	(0);
	}
}

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



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::SetCurrentPosition
// Author       : Andy Tate
// Date         : 28-Jun-2005
//
// Description  : Sets the current byte position within the currently open file.
//
// Parameters   : The offset position within the current file.
//
// Return value : "true" if successfully set, "false" if failed.
////////////////////////////////////////////////////////////////////////////////

bool Asura_DebugFileStream::SetCurrentPosition (u_int uPosition)
{
	// Is there already a file open?
	if (m_bIsOpen)
	{
		// Yes - return the current position...
		return	(Asura_PS3_FileSystem::SetCurrentPosition(m_xHandle, uPosition));
	}

	else
	{
		// No - report an error...
		Asura_Assert	(m_bIsOpen, "Cannot set the current position of a file which is not open.");
		return			(false);
	}
}

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



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::GetCurrentPosition
// Author       : Andy Tate
// Date         : 28-Jun-2005
//
// Description  : Gets the current byte position within the currently open file.
//
// Return value : The offset position within the current file.
////////////////////////////////////////////////////////////////////////////////

u_int Asura_DebugFileStream::GetCurrentPosition (void) const
{
	// Is there already a file open?
	if (m_bIsOpen)
	{
		const u_int	uCurrentPosition	= Asura_PS3_FileSystem::GetCurrentPosition (m_xHandle);

		// Yes - return the current position...
		return	(uCurrentPosition);
	}

	else
	{
		// No - report an error...
		Asura_Assert	(m_bIsOpen, "Cannot get the current position of a file which is not open.");
		return			(false);
	}
}

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



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::GetFileSize
// Author       : Andy Tate
// Date         : 28-Jun-2005
//
// Description  : Gets the size of the currently open file.
//
// Return value : The size of the file.
////////////////////////////////////////////////////////////////////////////////

u_int Asura_DebugFileStream::GetFileSize (void) const
{
	// Is there already a file open?
	if (m_bIsOpen)
	{
		const u_int	uSize	= Asura_PS3_FileSystem::GetFileSize (m_xHandle);

		// Return the size of the file...
		return	(uSize);
	}

	else
	{
		// No - report an error...
		Asura_Assert	(m_bIsOpen, "Cannot get the size of a file which is not open.");
		return			(false);
	}
}

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



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_DebugFileStream::Close
// Author       : Andy Tate
// 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 - reset the flags...
		m_bIsOpen	= false;
		
		// Simply close the file...
		return	(Asura_PS3_FileSystem::Close(&m_xHandle));
	}

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

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


/// End of file. ///
