home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / NeoIntroTCL3.0 folder / TCL / NeoDemo / Source / CNeoScrapStream.cp < prev   
Encoding:
Text File  |  1994-08-07  |  4.3 KB  |  177 lines  |  [TEXT/BROW]

  1. /****
  2.  * CNeoScrapStream.cp
  3.  *
  4.  *    C++ implementation for NeoAccess abstract stream class.
  5.  *
  6.  *    Copyright © Neologic Systems 1992-1994. All rights reserved
  7.  *
  8.  ****/
  9.  
  10. #include "NeoTypes.h"
  11. #include "CNeoScrapStream.h"
  12. #include CNeoPersistH
  13.  
  14. /* ****************************************************************** */
  15.                         /** Instance Methods **/
  16. /* ****************************************************************** */
  17. CNeoScrapStream::CNeoScrapStream(const OSType aType)
  18. {
  19.     setType(aType);
  20.     fOffset = 0;
  21.     fData = NewHandle(0);
  22.     NeoFailNil(fData);
  23. }
  24.  
  25. CNeoScrapStream::~CNeoScrapStream(void)
  26. {
  27.     flush();
  28.     if (fData) {
  29.         DisposHandle(fData);
  30.         fData = nil;
  31.     }
  32. }
  33.  
  34. /*
  35.  * Flush any buffered data.
  36.  */
  37. #pragma segment NeoWrite
  38. void CNeoScrapStream::flush(void)
  39. {
  40.     if (fOffset) {
  41.         NeoAssert(fData);
  42.         HLock(fData);
  43.         PutScrap(fLength, fType, *fData);
  44.         HUnlock(fData);
  45.         fData = nil;
  46.         fLength = 0;
  47.         fOffset = 0;
  48.     }
  49.     else if (fData) {
  50.         DisposHandle(fData);
  51.         fData = nil;
  52.         fLength = 0;
  53.     }
  54. }
  55.  
  56. /* ****************************************************************** */
  57.                         /** I/O Methods **/
  58. /* ****************************************************************** */
  59. /*
  60.  * Position the stream to the given mark then allocate a memory block of the
  61.  * given length then read that number of bytes into the block.
  62.  */
  63. #pragma segment NeoStream
  64. NeoBlob CNeoScrapStream::readBlob(const NeoMark aMark, long &aLength, const NeoTag aTag)
  65. {
  66.     long        offset;
  67.     NeoBlob        blob;
  68.  
  69.     FailNIL(blob = (NeoBlob)NewHandle(0));
  70.  
  71.     aLength = GetScrap((Handle)blob, aTag, &offset);
  72.     if (aLength < 0) {                          /* negative: error or no data in scrap */
  73.         DisposHandle((Handle)blob);
  74.         blob = nil;
  75.     }
  76.     else
  77.         CNeoPersist::FCacheUsed += aLength;
  78.  
  79.     return blob;
  80. }
  81.  
  82. /*
  83.  * Read a chunk of data of length aLength into the buffer referred to by
  84.  * aBuffer. This method assumes that the file is already positioned to
  85.  * the proper location in the file.
  86.  */
  87. #pragma segment NeoRead
  88. void CNeoScrapStream::readChunk(void *aBuffer, const long aLength, const NeoTag /* aTag */)
  89. {
  90.     if (fOffset < fLength) {
  91.         NeoAssert(fOffset + aLength <= fLength);            // This shouldn't really happen, should it?
  92.  
  93.         NeoBlockMove((void *)((*(char **)fData) + fOffset), aBuffer, aLength);
  94.         fOffset += aLength;
  95.     }
  96. }
  97.  
  98. /*
  99.  * The process of reading an object from a stream sometimes needs to begin
  100.  * by first configuring or otherwise preparing the stream. The readObject
  101.  * method is overridden by those streams to perform these preamble tasks.
  102.  */
  103. #pragma segment NeoStream
  104. void CNeoScrapStream::readObject(CNeoPersist *aObject, const NeoTag aTag)
  105. {
  106.     Boolean        dispose    = (Boolean)(fData == nil);
  107.     long        length;
  108.     long        offset;
  109.  
  110.     if (!fData) {
  111.         NeoFailNil(fData = NewHandle(0));
  112.         fOffset = 0;
  113.         fLength = 0;
  114.     }
  115.  
  116.     length = GetScrap(fData, fType, &offset);
  117.     if (length < 0) {            /* negative: error or no data in scrap */
  118.         if (dispose) {
  119.             DisposHandle(fData);
  120.             fData = nil;
  121.             fOffset = 0;
  122.             fLength = 0;
  123.         }
  124.     }
  125.     else
  126.         fLength = length;
  127. }
  128.  
  129. /*
  130.  * Position the stream to the given mark then write that number of
  131.  * bytes to the stream.
  132.  */
  133. #pragma segment NeoStream
  134. void CNeoScrapStream::writeBlob(NeoBlob aBlob, const NeoMark aMark, const long aLength, const NeoTag aTag)
  135. {
  136.     if (aBlob) {                                        /* blob exists */
  137.         HLockHi((Handle)aBlob);
  138.         PutScrap(aLength, aTag, NeoBlobGetPtr(aBlob));
  139.         HUnlock((Handle)aBlob);
  140.     }
  141. }
  142.  
  143. /*
  144.  * Write to the current location in the file a chunk of data of length
  145.  * aLength taken from the buffer referred to by aBuffer. This method
  146.  * assumes that the file is open and positioned to the proper location
  147.  * in the file.
  148.  */
  149. #pragma segment NeoWrite
  150. void CNeoScrapStream::writeChunk(const void *aBuffer, const long aLength, const NeoTag /* aTag */)
  151. {
  152.     if (fData)
  153.         SetHandleSize(fData, fOffset + aLength);
  154.     else {
  155.         NeoFailNil(fData = NewHandle(aLength));
  156.         fOffset = 0;
  157.     }
  158.     fLength += aLength;
  159.  
  160.     NeoBlockMove(aBuffer, ((*(char **)fData) + fOffset), aLength);
  161.     fOffset = fLength;
  162. }
  163.  
  164. /*
  165.  * The process of writing an object to a stream sometimes involves first
  166.  * configuring or otherwise preparing the stream. The writeObject method
  167.  * overridden by those streams to perform these preamble tasks.
  168.  */
  169. #pragma segment NeoStream
  170. void CNeoScrapStream::writeObject(CNeoPersist *aObject, const NeoTag aTag)
  171. {
  172.     ZeroScrap();                        // Clear the previous contents of the scrap
  173.     fLength = 0;
  174.     fOffset = 0;
  175. }
  176.  
  177.