home *** CD-ROM | disk | FTP | other *** search
- /*
- File: BArray.cpp
-
- Contains: ByteArray implementation
-
- Owned by: Vincent Lo
-
- Copyright: © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <3> 3/15/96 CC Woops! Forgot to include TempObj.h.
- <2> 3/15/96 CC 1331422: Added ODAEGetByteArray function.
- */
-
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef _BARRAY_
- #include <BArray.h>
- #endif
-
- #ifndef _EXCEPT_
- #include <Except.h>
- #endif
-
- #ifndef _ODMEMORY_
- #include <ODMemory.h>
- #endif
-
- #ifndef SOM_Module_OpenDoc_Errors_defined
- #include <ErrorDef.xh>
- #endif
-
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
-
- #ifndef _TEMPOBJ_
- #include <TempObj.h>
- #endif
-
- //------------------------------------------------------------------------------
- // CreateByteArray
- //------------------------------------------------------------------------------
-
- ODByteArray* CreateByteArray(void* buffer, ODULong size)
- {
- ODByteArray* ba = (ODByteArray*) ODNewPtrClear(sizeof(ODByteArray));
- if (ba != kODNULL) {
- ba->_buffer = (octet*) ODNewPtrClear(size);
- if (ba->_buffer) {
- ODBlockMove(buffer, ba->_buffer, size);
- ba->_length = size;
- ba->_maximum = size;
- }
- else
- THROW(kODErrOutOfMemory);
- }
- else
- THROW(kODErrOutOfMemory);
-
- return ba;
- }
-
- //------------------------------------------------------------------------------
- // CreateByteArrayStruct
- //------------------------------------------------------------------------------
-
- ODByteArray CreateByteArrayStruct(void* buffer, ODULong size)
- {
- ODByteArray ba = CreateEmptyByteArrayStruct(size);
-
- if ( size > 0 )
- {
- ODBlockMove(buffer, ba._buffer, size);
- ba._length = size;
- }
-
- return ba;
- }
-
- //------------------------------------------------------------------------------
- // CreateEmptyByteArray
- //------------------------------------------------------------------------------
-
- ODByteArray* CreateEmptyByteArray(ODULong maximum)
- {
- ODByteArray* ba = (ODByteArray*) ODNewPtrClear(sizeof(ODByteArray));
- ODPtr buffer = ODNewPtrClear(maximum);
-
- if ((ba != kODNULL) && (buffer != kODNULL)) {
- ba->_buffer = (octet *) buffer;
- ba->_length = 0;
- ba->_maximum = maximum;
- }
- else {
- ODDisposePtr(ba);
- ODDisposePtr(buffer);
- THROW(kODErrOutOfMemory);
- }
- return ba;
- }
-
- //------------------------------------------------------------------------------
- // CreateEmptyByteArrayStruct
- //------------------------------------------------------------------------------
-
- ODByteArray CreateEmptyByteArrayStruct(ODULong maximum)
- {
- ODByteArray ba;
-
- if ( maximum > 0 )
- {
- ba._buffer = (octet*) ODNewPtrClear(maximum);
- if ( ba._buffer == kODNULL )
- THROW(kODErrOutOfMemory);
- }
- else
- ba._buffer = kODNULL;
-
- ba._length = 0;
- ba._maximum = maximum;
-
- return ba;
- }
-
- //------------------------------------------------------------------------------
- // UseByteArray
- //------------------------------------------------------------------------------
-
- void UseByteArray(ODByteArray* ba, void* buffer, ODULong size)
- {
- ba->_buffer = (octet *) buffer;
- ba->_length = size;
- ba->_maximum = size;
- }
-
- //------------------------------------------------------------------------------
- // CopyByteArray
- //------------------------------------------------------------------------------
-
- ODByteArray* CopyByteArray(ODByteArray* fromBA)
- {
- ODByteArray* ba = (ODByteArray*) ODNewPtrClear(sizeof(ODByteArray));
- ODPtr buffer = ODNewPtrClear(fromBA->_maximum);
-
- if ((ba != kODNULL) && (buffer != kODNULL)) {
- ba->_buffer = (octet*) buffer;
- ODBlockMove(fromBA->_buffer, ba->_buffer, fromBA->_length);
- ba->_length = fromBA->_length;
- ba->_maximum = fromBA->_maximum;
- }
- else {
- ODDisposePtr(ba);
- ODDisposePtr(buffer);
- THROW(kODErrOutOfMemory);
- }
- return ba;
- }
-
- //------------------------------------------------------------------------------
- // CopyByteArrayStruct
- //------------------------------------------------------------------------------
-
- ODByteArray CopyByteArrayStruct(ODByteArray* fromBA)
- {
- ODByteArray ba = CreateEmptyByteArrayStruct(fromBA->_maximum);
-
- if ( fromBA->_length > 0 )
- {
- ODBlockMove(fromBA->_buffer, ba._buffer, fromBA->_length);
- ba._length = fromBA->_length;
- }
-
- return ba;
- }
-
- //------------------------------------------------------------------------------
- // DisposeByteArray
- //------------------------------------------------------------------------------
-
- void DisposeByteArray(ODByteArray* array)
- {
- if (array->_buffer != kODNULL)
- ODDisposePtr(array->_buffer);
- ODDisposePtr(array);
- }
-
- //------------------------------------------------------------------------------
- // AreByteArraysEqual
- //------------------------------------------------------------------------------
-
- ODBoolean AreByteArraysEqual(ODByteArray* ba1, ODByteArray* ba2)
- {
- return (((ba1->_length == ba2->_length) &&
- !memcmp(ba1->_buffer, ba2->_buffer, ba1->_length)) ? kODTrue : kODFalse);
- }
-
- //------------------------------------------------------------------------------
- // ODAEGetByteArray
- //------------------------------------------------------------------------------
-
- ODByteArray* ODAEGetByteArray(const AppleEvent *theAppleEvent,
- AEKeyword theAEKeyword,
- DescType desiredType)
- {
- DescType gotType;
- Size actualSize;
- Size maximumSize;
- OSErr error;
-
- error = AESizeOfParam(theAppleEvent, theAEKeyword, &gotType, &actualSize);
- THROW_IF_ERROR(error);
- if ( gotType != desiredType )
- THROW(errAECorruptData);
-
- maximumSize = actualSize;
- TempODByteArray ba(CreateEmptyByteArray(maximumSize));
-
- error = AEGetParamPtr(theAppleEvent, theAEKeyword, desiredType,
- &gotType, ba->_buffer, maximumSize,
- &actualSize);
- THROW_IF_ERROR(error);
- if ( actualSize != maximumSize )
- THROW(errAECorruptData);
-
- ba->_length = actualSize;
-
- return ba.DontDelete();
- }
-