
///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_DebugFileStream.cpp
// Author : Andy Tate
// Date   : 9-Jun-2005
//
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

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

#include "Core/Asura.h"

#ifndef ASURA_PSP_SONY_SUBMISSION	// only expecting to use this for screenshots.

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

// Asura header-files...
#include "Debugging/Asura_Assert.h"
#include "FileAccess/Asura_PSP_FileIO.h"	// for GetFullPath()


////////////////////////////////////////////////////////////////////////////////
// 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_xFileHandle	= sceIoOpen (szFilename, SCE_O_RDONLY, 0);
				break;
			}

			case WRITE_DATA:
			{
				m_xFileHandle	= sceIoOpen (szFilename, SCE_O_CREAT | SCE_O_WRONLY, 0);
				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	= (0 <= m_xFileHandle);

		// 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?
		if (bUseFullPath)
		{
			// ARC - 05 January 2007 10:48:25 - Looks like GetFullPath() doesn't
			// return a string now so build the string in a temporary buffer first
			Asura_Char szTemporaryBuffer[256];
            // (MH - 26.07.2007) if we're writing data to this file and it's a UMD build make sure we use the memory stick.
			bool bForceMemstickIfUMD = (eOpenType == WRITE_DATA);
			Asura_PSP_FileIO::GetFullPath(szFilename, sizeof(szTemporaryBuffer), szTemporaryBuffer, bForceMemstickIfUMD);

			// Store the filename...
			m_xFilename	= szTemporaryBuffer;
		}
		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
// 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())
	{
		SceSSize	xNumBytesRead	= sceIoRead (m_xFileHandle, pData, uNumberOfBytes);

		// Yes - return the number of bytes read...
		return	(xNumBytesRead);
	}
	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 (IsOpen())
	{
		SceOff	xNumBytesSkipped	= sceIoLseek (m_xFileHandle, uNumberOfBytes, SCE_SEEK_CUR);

		// Yes - return the number of bytes skipped...
		return	(xNumBytesSkipped);
	}
	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())
	{
		SceSSize	xNumBytesWritten	= sceIoWrite (m_xFileHandle, pData, uNumberOfBytes);

		// Yes - return the number of bytes written...
		return	(xNumBytesWritten);
	}
	else
	{
		// No - cannot write anything...
		return	(0);
	}
}

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



////////////////////////////////////////////////////////////////////////////////
// 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 - simply close the file...
		sceIoClose	(m_xFileHandle);

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

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

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


#endif // #ifndef ASURA_PSP_SONY_SUBMISSION	

/// End of file. ///
