home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / BArray.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  5.6 KB  |  234 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        BArray.cpp
  3.  
  4.     Contains:    ByteArray implementation
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.          <3>     3/15/96    CC        Woops! Forgot to include TempObj.h.
  13.          <2>     3/15/96    CC        1331422: Added ODAEGetByteArray function.
  14. */
  15.  
  16.  
  17. #ifndef _ODTYPES_
  18. #include <ODTypes.h>
  19. #endif
  20.  
  21. #ifndef _BARRAY_
  22. #include <BArray.h>
  23. #endif
  24.  
  25. #ifndef _EXCEPT_
  26. #include <Except.h>
  27. #endif
  28.  
  29. #ifndef _ODMEMORY_
  30. #include <ODMemory.h>
  31. #endif
  32.  
  33. #ifndef SOM_Module_OpenDoc_Errors_defined
  34. #include <ErrorDef.xh>
  35. #endif
  36.  
  37. #ifndef __APPLEEVENTS__
  38. #include <AppleEvents.h>
  39. #endif
  40.  
  41. #ifndef _TEMPOBJ_
  42. #include <TempObj.h>
  43. #endif
  44.  
  45. //------------------------------------------------------------------------------
  46. // CreateByteArray
  47. //------------------------------------------------------------------------------
  48.  
  49. ODByteArray* CreateByteArray(void* buffer, ODULong size)
  50. {
  51.     ODByteArray*    ba = (ODByteArray*) ODNewPtrClear(sizeof(ODByteArray));
  52.     if (ba != kODNULL) {
  53.         ba->_buffer = (octet*) ODNewPtrClear(size);
  54.         if (ba->_buffer) {
  55.             ODBlockMove(buffer, ba->_buffer, size);
  56.             ba->_length = size;
  57.             ba->_maximum = size;
  58.         }
  59.         else
  60.             THROW(kODErrOutOfMemory);
  61.     }
  62.     else
  63.         THROW(kODErrOutOfMemory);
  64.     
  65.     return ba;
  66. }
  67.  
  68. //------------------------------------------------------------------------------
  69. // CreateByteArrayStruct
  70. //------------------------------------------------------------------------------
  71.  
  72. ODByteArray CreateByteArrayStruct(void* buffer, ODULong size)
  73. {
  74.     ODByteArray    ba = CreateEmptyByteArrayStruct(size);
  75.  
  76.     if ( size > 0 )
  77.     {
  78.         ODBlockMove(buffer, ba._buffer, size);
  79.         ba._length = size;
  80.     }
  81.     
  82.     return ba;
  83. }
  84.  
  85. //------------------------------------------------------------------------------
  86. // CreateEmptyByteArray
  87. //------------------------------------------------------------------------------
  88.  
  89. ODByteArray* CreateEmptyByteArray(ODULong maximum)
  90. {
  91.     ODByteArray*    ba = (ODByteArray*) ODNewPtrClear(sizeof(ODByteArray));
  92.     ODPtr            buffer = ODNewPtrClear(maximum);
  93.  
  94.     if ((ba != kODNULL) && (buffer != kODNULL)) {
  95.         ba->_buffer = (octet *) buffer;
  96.         ba->_length = 0;
  97.         ba->_maximum = maximum;
  98.     }
  99.     else {
  100.         ODDisposePtr(ba);
  101.         ODDisposePtr(buffer);
  102.         THROW(kODErrOutOfMemory);
  103.     }
  104.     return ba;
  105. }
  106.  
  107. //------------------------------------------------------------------------------
  108. // CreateEmptyByteArrayStruct
  109. //------------------------------------------------------------------------------
  110.  
  111. ODByteArray CreateEmptyByteArrayStruct(ODULong maximum)
  112. {
  113.     ODByteArray    ba;
  114.  
  115.     if ( maximum > 0 )
  116.     {
  117.         ba._buffer = (octet*) ODNewPtrClear(maximum);
  118.         if ( ba._buffer == kODNULL )
  119.             THROW(kODErrOutOfMemory);
  120.     }
  121.     else
  122.         ba._buffer = kODNULL;
  123.  
  124.     ba._length = 0;
  125.     ba._maximum = maximum;
  126.  
  127.     return ba;
  128. }
  129.  
  130. //------------------------------------------------------------------------------
  131. // UseByteArray
  132. //------------------------------------------------------------------------------
  133.  
  134. void UseByteArray(ODByteArray* ba, void* buffer, ODULong size)
  135. {
  136.     ba->_buffer = (octet *) buffer;
  137.     ba->_length = size;
  138.     ba->_maximum = size;
  139. }
  140.  
  141. //------------------------------------------------------------------------------
  142. // CopyByteArray
  143. //------------------------------------------------------------------------------
  144.  
  145. ODByteArray* CopyByteArray(ODByteArray* fromBA)
  146. {
  147.     ODByteArray*    ba = (ODByteArray*) ODNewPtrClear(sizeof(ODByteArray));
  148.     ODPtr            buffer = ODNewPtrClear(fromBA->_maximum);
  149.  
  150.     if ((ba != kODNULL) && (buffer != kODNULL)) {
  151.         ba->_buffer = (octet*) buffer;
  152.         ODBlockMove(fromBA->_buffer, ba->_buffer, fromBA->_length);
  153.         ba->_length = fromBA->_length;
  154.         ba->_maximum = fromBA->_maximum;
  155.     }
  156.     else {
  157.         ODDisposePtr(ba);
  158.         ODDisposePtr(buffer);
  159.         THROW(kODErrOutOfMemory);
  160.     }
  161.     return ba;    
  162. }
  163.  
  164. //------------------------------------------------------------------------------
  165. // CopyByteArrayStruct
  166. //------------------------------------------------------------------------------
  167.  
  168. ODByteArray CopyByteArrayStruct(ODByteArray* fromBA)
  169. {
  170.     ODByteArray    ba = CreateEmptyByteArrayStruct(fromBA->_maximum);
  171.  
  172.     if ( fromBA->_length > 0 )
  173.     {
  174.         ODBlockMove(fromBA->_buffer, ba._buffer, fromBA->_length);
  175.         ba._length = fromBA->_length;
  176.     }
  177.  
  178.     return ba;    
  179. }
  180.  
  181. //------------------------------------------------------------------------------
  182. // DisposeByteArray
  183. //------------------------------------------------------------------------------
  184.  
  185. void DisposeByteArray(ODByteArray* array)
  186. {
  187.     if (array->_buffer != kODNULL)
  188.         ODDisposePtr(array->_buffer);
  189.     ODDisposePtr(array);
  190. }
  191.  
  192. //------------------------------------------------------------------------------
  193. // AreByteArraysEqual
  194. //------------------------------------------------------------------------------
  195.  
  196. ODBoolean AreByteArraysEqual(ODByteArray* ba1, ODByteArray* ba2)
  197. {
  198.     return (((ba1->_length == ba2->_length) &&
  199.             !memcmp(ba1->_buffer, ba2->_buffer, ba1->_length)) ? kODTrue : kODFalse);
  200. }
  201.  
  202. //------------------------------------------------------------------------------
  203. // ODAEGetByteArray
  204. //------------------------------------------------------------------------------
  205.  
  206. ODByteArray* ODAEGetByteArray(const AppleEvent *theAppleEvent,
  207.                                 AEKeyword theAEKeyword,
  208.                                 DescType desiredType)
  209. {
  210.     DescType    gotType;
  211.     Size        actualSize;
  212.     Size        maximumSize;
  213.     OSErr        error;
  214.  
  215.     error = AESizeOfParam(theAppleEvent, theAEKeyword, &gotType, &actualSize);
  216.     THROW_IF_ERROR(error);
  217.     if ( gotType != desiredType )
  218.         THROW(errAECorruptData);
  219.  
  220.     maximumSize = actualSize;
  221.     TempODByteArray ba(CreateEmptyByteArray(maximumSize));
  222.  
  223.     error = AEGetParamPtr(theAppleEvent, theAEKeyword, desiredType,
  224.                                     &gotType, ba->_buffer, maximumSize,
  225.                                     &actualSize);
  226.     THROW_IF_ERROR(error);
  227.     if ( actualSize != maximumSize )
  228.         THROW(errAECorruptData);
  229.  
  230.     ba->_length = actualSize;
  231.  
  232.     return ba.DontDelete();
  233. }
  234.