/* 
Copyright (c) 1991-2000 UserLand Software, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the 
following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

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. 
*/ 


#include "iacinternal.h"



Boolean IACpushbinaryparam (Handle val, OSType binaryfieldtype, OSType keyword) {

	return (IACpushbinaryitem (IACglobals.event, val, binaryfieldtype, keyword));
	} /*IACpushbinaryparam*/


Boolean IACreturnbinary (Handle x, OSType binaryfieldtype) {
	
	return (IACpushbinaryitem (IACglobals.reply, x, binaryfieldtype, keyDirectObject));
	} /*IACreturnbinary*/
	
	
Boolean IACgetbinaryparam (OSType keyword, Handle *hbinary, OSType *binaryfieldtype) {

	if (!IACgetbinaryitem (IACglobals.event, keyword, hbinary, binaryfieldtype)) {
	
		IACparamerror (IACglobals.errorcode, "\pbinary", keyword);
		
		return (false);
		}

	IACglobals.nextparamoptional = false; /*must be reset for each param*/
	
	return (true);
	} /*IACgetbinaryparam*/


Boolean IACgetbinaryitem (AEDescList *list, long n, Handle *val, OSType *binaryfieldtype) {
	
	OSErr ec;
	AEDesc desc;
	DescType key;
	
	if ((*list).descriptorType != typeAEList) {
	
		ec = AEGetKeyDesc (list, n, typeWildCard, &desc);
		
		if (ec != errAEDescNotFound)
			goto done;
		}

	ec = AEGetNthDesc (list, n, typeWildCard, &key, &desc);
	
	done:
	
	IACglobals.errorcode = ec;
	
	if (ec == noErr) {
	
		*binaryfieldtype = desc.descriptorType;
		
		*val = desc.dataHandle;	
		
		return (true);
		}
	else {
		*binaryfieldtype = 0L;
		
		*val = NULL;
		
		return (false);
		}
	} /*IACgetbinaryitem*/


Boolean IACpushbinaryitem (AEDescList *list, Handle val, OSType binaryfieldtype, long n) {
	
	/*
		dmb 4.1: added IACglobals.dontdisposenextparam field
	*/
		
	OSErr ec;
	AEDesc desc;
	
	desc.descriptorType = binaryfieldtype;
	
	desc.dataHandle = val;
	
	if ((*list).descriptorType != typeAEList)
		
		ec = AEPutKeyDesc (list, n, &desc);
	else
		ec = AEPutDesc (list, n, &desc);
	
	IACglobals.errorcode = ec;
	
	if (IACglobals.dontdisposenextparam)
		IACglobals.dontdisposenextparam = false;
	else
		AEDisposeDesc (&desc);
	
	return (ec == noErr);
	} /*IACpushbinaryitem*/