/*	XGArgStream.cpp
 *
 *		This reads data from an argument stream, typically from a resource
 *	loaded in memory
 */

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

  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 <XDataUtil.h>
#include <XView.h>

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

/*$	XGArgStream::XGArgStream
 *
 *		Construct me. This is normally used internally by YAAF to define
 *	the input stream data and length.
 */

XGArgStream::XGArgStream(const unsigned char *data, long len)
{
	fLength = len;
	fData = data;
}

/************************************************************************/
/*																		*/
/*	Read Routines														*/
/*																		*/
/************************************************************************/

/*$	XGArgStream::ReadBoolean
 *
 *		Read a boolean object. Returns true if any of the next two bytes
 *	are non-zero.
 */

bool XGArgStream::ReadBoolean()
{
	bool b;
	
	if (fLength < 2) return false;
	
	b = false;
	if (*fData++) b = true;
	if (*fData++) b = true;
	
	fLength -= 2;
	return b;
}

/*$	XGArgStream::ReadInteger
 *
 *		Read a 32-bit integer object.
 */

long XGArgStream::ReadInteger()
{
	long l;
	
	if (fLength < sizeof(long)) return 0;
	
	l = *((long *)fData);
	fData += sizeof(long);
	fLength -= sizeof(long);
	
#if OPT_NATIVEORDER != 1
	l = SwapLong(l);
#endif

	return l;
}

/*$	XGArgStream::ReadString
 *
 *		Read a (pascal format) string and convert it to C.
 */

void XGArgStream::ReadString(char *str)
{
	unsigned char len;
	unsigned char x;
	
	if (fLength < 1) return;
	len = *fData++;
	fLength--;
	
	for (x = 0; x != len; x++) {
		*str++ = (char)(*fData++);
		fLength--;
	}
	*str = 0;
	
	if (!(len & 1)) {
		fLength--;
		fData++;
	}
}
