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

/*  YAAF - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <XDataUtil.h>
#include <XView.h>

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

// @G"Construction/Destruction"

/*	@F"XGArgStream::XGArgStream" @I
 *
 *		Construct me.
 */

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

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

// @G"Parse parameters"

/*	@F"XGArgStream::ReadBoolean" @I
 *
 *		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;
}

/*	@F"XGArgStream::ReadInteger" @I
 *
 *		Read an 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;
}

/*	@F"XGArgStream::ReadString" @I
 *
 *		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++;
	}
}
