/*	XGWritableStream.cpp
 *
 *		Lets you write to an XGArgStream.  Useful for building
 *	dialogs/views on the fly (some views can only be built
 *	via an arguement stream).
 */

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

  XGWritableStream.cpp is Copyright 1998-1999 Sassafras Software Inc.
  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>			// must be first to optimize compile on MSVC++
#include <string.h>

#include <XWritableStream.h>


/************************************************************************/
/*																		*/
/*	Construction														*/
/*																		*/
/************************************************************************/

/*$	XGWritableStream::XGWritableStream
 *
 *		Constructs a writable XGArgStream object. This is useful for
 *	creating an XGArgStream which can be explicitly initialized before
 *	being passed to an XGView object.
 *
 *		The #data# parameter should point to a buffer that is "large
 *	enough" to store the data being initialized. The #size# parameter
 *	is the size of that buffer.
 */

XGWritableStream::XGWritableStream (unsigned char *data, long size) : XGArgStream(data, 0)
{
	fSize = size;
	fStart = data;
	Rewind();
}

/************************************************************************/
/*																		*/
/*	Status																*/
/*																		*/
/************************************************************************/

/*$	XGWritableStream::LEngth
 *
 *		Returns the number of bytes that have been written so far to the
 *	XGWritableStream object.
 */

long XGWritableStream::Length (void)
{
	return(fSize - fRemains);
}

/*$	XGWritableStream::Rewind
 *
 *		This resets this writable stream object, clearing all the data
 *	that was written to thsi thing.
 */

void XGWritableStream::Rewind (void)
{
	fRemains = fSize;
	fPosition = fStart;
	SetBuffer(fStart);
}

/************************************************************************/
/*																		*/
/*	Initialization														*/
/*																		*/
/************************************************************************/

/*$	XGWritableStream::WriteBoolean
 *
 *		This writes a boolean value to the writable stream.
 */

void XGWritableStream::WriteBoolean (bool b)
{	
	if (fRemains < 2)
		return;
	
	*fPosition++ = 0;
	*fPosition++ = b;
	fRemains -= 2;
	SetLength(fSize - fRemains);
}

/*$	XGWritableStream::WriteInteger
 *
 *		This writes a 32-bit integer value to the writable stream object.
 */

void XGWritableStream::WriteInteger (long l)
{
	if (fRemains < 4)
		return;
	
	*fPosition++ = (l >> 24);
	*fPosition++ = (l >> 16);
	*fPosition++ = (l >> 8);
	*fPosition++ = l;
	fRemains -= 4;
	SetLength(fSize - fRemains);
}

/*$	XGWritableStream::WriteString
 *
 *		This writes a C string to the writable stream object.
 */
 
void XGWritableStream::WriteString (char *s)
{
	WriteString(s, strlen(s));
}

/*$	XGWritableStream::WriteString
 *
 *		This writes a Pascal string to the writable stream object.
 */
 
void XGWritableStream::WriteString (unsigned char *s)
{
	WriteString(&s[1], *s);
}

/*$	XGWritableStream::WriteString
 *
 *		This writes an arbitrary text stream to the writable
 *	stream object. The #s# parameter points to the start of the
 *	string, and the #len# parameter gives the length in bytes.
 */
 
void XGWritableStream::WriteString (void *s, long len)
{
	int plen;
	
	plen = (len + 2) & ~0x01;
	if (fRemains < plen)
		return;
	fRemains -= plen;
	*fPosition = len;
	memcpy(&fPosition[1], s, len);
	fPosition += plen;
	SetLength(fSize - fRemains);
}

