Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
Source/SVEditAEUtils.c
/* |
File: SVEditAEUtils.c |
Contains: |
Written by: Original version by Jon Lansdell and Nigel Humphreys. |
3.1 updates by Greg Sutton. |
Copyright: Copyright ©1995-1999 by Apple Computer, Inc., All Rights Reserved. |
You may incorporate this Apple sample source code into your program(s) without |
restriction. This Apple sample source code has been provided "AS IS" and the |
responsibility for its operation is yours. You are not permitted to redistribute |
this Apple sample source code as "Apple sample source code" after having made |
changes. If you're going to re-distribute the source, we require that you make |
it clear in the source that the code was descended from Apple sample source |
code, but that you've made changes. |
Change History (most recent first): |
7/19/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1 |
*/ |
#pragma segment Main |
#include "SVEditAEUtils.h" |
#include "SVEditUtils.h" |
#include "SVEditWindow.h" |
#include "SVAERecording.h" |
#include <AERegistry.h> |
#include <AEObjects.h> |
// ----------------------------------------------------------------------- |
// Utility Routines for getting data from AEDesc's |
// ----------------------------------------------------------------------- |
void GetRawDataFromDescriptor(const AEDesc *theDesc, |
Ptr destPtr, |
Size destMaxSize, |
Size *actSize) |
{ |
Size copySize; |
if (theDesc->dataHandle) |
{ |
HLock((Handle)theDesc->dataHandle); |
*actSize = GetHandleSize((Handle)theDesc->dataHandle); |
copySize = LesserOf(*actSize, destMaxSize); |
BlockMove(*theDesc->dataHandle, destPtr, copySize); |
HUnlock((Handle)theDesc->dataHandle); |
} |
else |
*actSize = 0; |
} /*GetRawDataFromDescriptor*/ |
OSErr GetPStringFromDescriptor(const AEDesc *aDesc, StringPtr resultStr) |
{ |
Size stringSize; |
AEDesc resultDesc = {typeNull, NULL}; |
OSErr err; |
err = AECoerceDesc(aDesc, typeChar, &resultDesc); |
if (noErr != err) goto done; |
resultStr[0] = 0; |
GetRawDataFromDescriptor(&resultDesc, (Ptr)&resultStr[1], 255, &stringSize); |
if (stringSize <= 255) |
resultStr[0] = stringSize; |
else |
err = errAECoercionFail; |
done: |
if (resultDesc.dataHandle) |
AEDisposeDesc(&resultDesc); |
return(err); |
} |
OSErr PutPStringToDescriptor(AEDesc* aDesc, StringPtr pStr) |
{ |
OSErr err; |
if (! aDesc || ! pStr) |
return(errAETypeError); |
err = AECreateDesc(typeChar, (Ptr)&pStr[1], pStr[0], aDesc); |
return(err); |
} |
OSErr GetIntegerFromDescriptor(const AEDesc *sourceDesc, short *result) |
{ |
OSErr myErr; |
OSErr ignoreErr; |
Size intSize; |
AEDesc resultDesc; |
*result = 0; |
myErr = AECoerceDesc(sourceDesc,typeShortInteger,&resultDesc); |
if (myErr==noErr) |
{ |
GetRawDataFromDescriptor(&resultDesc, |
(Ptr)result, |
2, |
&intSize); |
if (intSize>2) |
myErr = errAECoercionFail; |
} |
if (resultDesc.dataHandle) |
ignoreErr = AEDisposeDesc(&resultDesc); |
return(myErr); |
} |
OSErr GetBooleanFromDescriptor(const AEDesc *sourceDesc, |
Boolean *result) |
{ |
OSErr myErr; |
OSErr ignoreErr; |
Size boolSize; |
AEDesc resultDesc; |
*result = false; |
myErr = AECoerceDesc(sourceDesc,typeBoolean,&resultDesc); |
if (myErr==noErr) |
{ |
GetRawDataFromDescriptor(&resultDesc, |
(Ptr)result, |
sizeof(Boolean), |
&boolSize); |
if (boolSize>sizeof(Boolean)) |
myErr = errAECoercionFail; |
} |
if (resultDesc.dataHandle) |
ignoreErr = AEDisposeDesc(&resultDesc); |
return(myErr); |
} |
OSErr GetLongIntFromDescriptor(const AEDesc *sourceDesc, |
long *result) |
{ |
OSErr myErr; |
OSErr ignoreErr; |
Size intSize; |
AEDesc resultDesc; |
*result = 0; |
myErr = AECoerceDesc(sourceDesc,typeLongInteger,&resultDesc); |
if (myErr==noErr) |
{ |
GetRawDataFromDescriptor(&resultDesc, |
(Ptr)result, |
4, |
&intSize); |
if (intSize>4) |
myErr = errAECoercionFail; |
} |
if (resultDesc.dataHandle) |
ignoreErr = AEDisposeDesc(&resultDesc); |
return(myErr); |
} /*GetLongIntFromDescriptor*/ |
OSErr GetRectFromDescriptor(const AEDesc *sourceDesc, Rect *result) |
{ |
OSErr myErr; |
OSErr ignoreErr; |
Size rectSize; |
AEDesc resultDesc; |
SetRect(result,0,0,0,0); |
myErr = AECoerceDesc(sourceDesc,typeQDRectangle,&resultDesc); |
if (myErr==noErr) |
{ |
GetRawDataFromDescriptor(&resultDesc, |
(Ptr)result, |
sizeof(Rect), |
&rectSize); |
if (rectSize<sizeof(Rect)) |
myErr = errAECoercionFail; |
} |
if (resultDesc.dataHandle) |
ignoreErr = AEDisposeDesc(&resultDesc); |
return(myErr); |
} /*GetRectFromDescriptor*/ |
OSErr GetPointFromDescriptor(const AEDesc *sourceDesc, |
Point *result) |
{ |
OSErr myErr; |
OSErr ignoreErr; |
Size ptSize; |
AEDesc resultDesc; |
SetPt(result,0,0); |
myErr = AECoerceDesc(sourceDesc,typeQDPoint,&resultDesc); |
if (myErr==noErr) |
{ |
GetRawDataFromDescriptor(&resultDesc, |
(Ptr)result, |
sizeof(Point), |
&ptSize); |
if (ptSize<sizeof(Point)) |
myErr = errAECoercionFail; |
} |
if (resultDesc.dataHandle) |
ignoreErr = AEDisposeDesc(&resultDesc); |
return(myErr); |
} /*GetPointFromDescriptor*/ |
// ---------------------------------------------------------------------- |
// Name: GotRequiredParams |
// Function: Checks all parameters defined as 'required' have been read |
// ---------------------------------------------------------------------- |
OSErr GotRequiredParams(const AppleEvent *theAppleEvent) |
{ |
DescType returnedType; |
Size actualSize; |
OSErr err; |
// look for the keyMissedKeywordAttr, just to see if it's there |
err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, |
&returnedType, NULL, 0, &actualSize); |
switch (err) |
{ |
case errAEDescNotFound: // attribute not there means we |
err = noErr; // got all required parameters. |
break; |
case noErr: // attribute there means missed |
err = errAEParamMissed; // at least one parameter. |
break; |
// default: pass on unexpected error in looking for the attribute |
} |
return(err); |
} // GotReqiredParams |
// This routine takes a result descriptor, a reply descriptor and an error. |
// If there is a result to add to the reply it makes sure the reply isn't |
// NULL itself then adds the result to the reply depending on the error |
// and the type of result. |
OSErr AddResultToReply(AEDesc* result, AEDesc* reply, OSErr error) |
{ |
OSErr err; |
// Check that the reply is not NULL and there is a result to put in it |
if (typeNull == reply->descriptorType || typeNull == result->descriptorType) |
return(error); |
if (noErr == error) |
err = AEPutParamDesc(reply, keyDirectObject, result); |
else |
{ |
switch (result->descriptorType) |
{ |
case typeInteger: |
err = AEPutParamDesc(reply, keyErrorNumber, result); |
break; |
case typeChar: |
err = AEPutParamDesc(reply, keyErrorString, result); |
break; |
default: |
err = errAETypeError; |
} |
if (noErr == err) |
err = error; // Don't loose that error |
} |
return(err); |
} |
// ----------------------------------------------------------------------- |
// Name: MakeSelfAddress |
// Purpose: Builds an AEAddressDesc for the current process |
// ----------------------------------------------------------------------- |
OSErr MakeSelfAddress(AEAddressDesc *selfAddress) |
{ |
ProcessSerialNumber procSerNum; |
procSerNum.highLongOfPSN = 0; |
procSerNum.lowLongOfPSN = kCurrentProcess; |
return(AECreateDesc(typeProcessSerialNumber, (Ptr)&procSerNum, sizeof(procSerNum), selfAddress)); |
} // MakeSelfAddress |
OSErr GetEnumeratedFromDescriptor(const AEDesc *sourceDesc, DescType *result) |
{ |
OSErr myErr; |
OSErr ignoreErr; |
Size enumSize; |
AEDesc resultDesc; |
*result = typeNull; |
myErr = AECoerceDesc(sourceDesc,typeEnumerated,&resultDesc); |
if (myErr==noErr) |
{ |
GetRawDataFromDescriptor(&resultDesc, (Ptr)result, sizeof(DescType), &enumSize); |
if (enumSize<sizeof(DescType)) |
myErr = errAECoercionFail; |
} |
if (resultDesc.dataHandle) |
ignoreErr = AEDisposeDesc(&resultDesc); |
return(myErr); |
} // GetEnumeratedFromDescriptor |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-07-22