/*	XGFileHandle.cpp
 *
 *		This provides the basic file I/O behaviors, but reads and writes
 *	data to a temporary handle. This also provides the basic handle
 *	semantics, so you can also extract data using the XHandle utilities.
 */

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

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

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

#include <XConfig.h>
#include <XError.h>
#include <XStream.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGFileHandle::XGFileHandle
 *
 *		Construct me. This creates an empty file handle, one with no
 *	data.
 */

XGFileHandle::XGFileHandle()
{
	fPos = 0;
}

/*$	XGFileHandle::~XGFileHandle
 *
 *		Destruct me. This releases the allocated memory and loses the
 *	information that had been saved.
 */

XGFileHandle::~XGFileHandle()
{
}

/************************************************************************/
/*																		*/
/*	File I/O Routines													*/
/*																		*/
/************************************************************************/

/*$	XGFileHandle::Read
 *
 *		Read data. This reads data from the current file position from
 *	the memory image, and returns the number of bytes read.
 */

long XGFileHandle::Read(long len, void *data)
{
	long e;
	
	e = GetSize() - fPos;
	if (len > e) len = e;
	GetMemory(fPos,len,data);
	fPos += len;
	return len;
}

/*$	XGFileHandle::Write
 *
 *		Write data. This writes the data at the current file position
 *	into the handle, growing the handle if needed.
 */

long XGFileHandle::Write(long len, void *data)
{
	long e;
	
	e = fPos + len;
	if (e > GetSize()) SetSize(e);
	SetMemory(fPos,len,data);
	fPos += len;
	return len;
}

/*$	XGFileHandle::Flush
 *
 *		Flush the data that was just written. This does nothing here.
 */

void XGFileHandle::Flush()
{
}

/*$	XGFileHandle::GetFilePos
 *
 *		Get the current file position in the handle where data is being
 *	read/written.
 */

long XGFileHandle::GetFilePos()
{
	return fPos;
}

/*$	XGFileHandle::SetFilePos
 *
 *		Set the file position in the handle.
 */

void XGFileHandle::SetFilePos(long pos)
{
	Assert(pos >= 0);
	Assert(pos <= GetSize());
	
	fPos = pos;
}

/*$	XGFileHandle::GetEOF
 *
 *		Get the end of file position. This gets the size of the handle
 *	that has been generated thus far.
 */

long XGFileHandle::GetEOF()
{
	return GetSize();
}

/*$	XGFileHandle::SetEOF
 *
 *		Set the end of file position. This grows or shrinks the handle
 *	as needed.
 */

void XGFileHandle::SetEOF(long pos)
{
	SetSize(pos);
}
