home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Applications / ResAnomaly 1.2 / ResAnomaly Source / LTempHandleStream.cp < prev    next >
Encoding:
Text File  |  1995-08-22  |  3.3 KB  |  153 lines  |  [TEXT/MPCC]

  1. // ===========================================================================
  2. //    LHandleStream.cp                ©1993 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    A Stream whose bytes are in a Handle block in memory
  6. // * Note: ckt modified to use temp. memory!  Don't know why there isn't
  7. // * such a variant included with PP.  This should be a dedicated
  8. // * LTemporaryStream class, really.
  9.  
  10. #ifdef PowerPlant_PCH
  11. #include PowerPlant_PCH
  12. #endif
  13.  
  14. #include <LHandleStream.h>
  15.  
  16.  
  17. // ---------------------------------------------------------------------------
  18. //        • LHandleStream
  19. // ---------------------------------------------------------------------------
  20. //    Default Constructor
  21.  
  22. LHandleStream::LHandleStream()
  23. {
  24.     OSErr    theErr;
  25.     
  26.     mDataH = ::TempNewHandle(0, &theErr);            // Start with a zero length Handle
  27.     // * purposely ignoring theErr
  28.     
  29.     ThrowIfMemFail_(mDataH);
  30. }
  31.  
  32.  
  33. // ---------------------------------------------------------------------------
  34. //        • LHandleStream(Handle)
  35. // ---------------------------------------------------------------------------
  36. //    Construct from an existing Handle
  37. //
  38. //    The LHandleStream object assumes control of the Handle
  39.  
  40. LHandleStream::LHandleStream(
  41.     Handle    inHandle)
  42. {
  43.     mDataH = inHandle;
  44.     LStream::SetLength(GetHandleSize(inHandle));
  45. }
  46.  
  47.  
  48. // ---------------------------------------------------------------------------
  49. //        • ~LHandleStream
  50. // ---------------------------------------------------------------------------
  51. //    Destructor
  52.  
  53. LHandleStream::~LHandleStream()
  54. {
  55.     if (mDataH != nil) {
  56.         ::DisposeHandle(mDataH);
  57.     }
  58. }
  59.  
  60.  
  61. void
  62. LHandleStream::SetLength(
  63.     Int32    inLength)
  64. {
  65.     ::SetHandleSize(mDataH, inLength);
  66.     ThrowIfMemError_();
  67.     LStream::SetLength(inLength);
  68. }
  69.  
  70.  
  71. Int32
  72. LHandleStream::WriteData(
  73.     const void    *inBuffer,
  74.     Int32        inByteCount)
  75. {
  76.     Int32    bytesWritten = inByteCount;
  77.     Int32    endOfWrite = GetMarker() + inByteCount;
  78.     if (endOfWrite > GetLength()) {
  79.                                     // Need to grow Handle
  80.         Try_ {
  81.             SetLength(endOfWrite);
  82.         }
  83.         
  84.         Catch_(inErr) {                // Grow failed. Write only what fits.
  85.             bytesWritten = GetLength() - GetMarker();
  86.         } EndCatch_
  87.     }
  88.  
  89.                                     // Copy bytes into Handle
  90.     ::BlockMoveData(inBuffer, *mDataH + GetMarker(), bytesWritten);
  91.     SetMarker(bytesWritten, streamFrom_Marker);
  92.     return bytesWritten;
  93. }
  94.  
  95.  
  96. Int32
  97. LHandleStream::ReadData(
  98.     void    *outBuffer,
  99.     Int32    inByteCount)
  100. {
  101.                                     // Upper bound is number of bytes from
  102.                                     //   marker to end
  103.     Int32    bytesRead = inByteCount;
  104.     if ((GetMarker() + bytesRead) > GetLength()) {
  105.         bytesRead = GetLength() - GetMarker();
  106.     }
  107.                                     // Copy bytes from Handle into buffer
  108.     ::BlockMoveData(*mDataH + GetMarker(), outBuffer, bytesRead);
  109.     SetMarker(bytesRead, streamFrom_Marker);
  110.  
  111.     return bytesRead;
  112. }
  113.  
  114.  
  115. void
  116. LHandleStream::SetDataHandle(
  117.     Handle    inHandle)
  118. {
  119.     if (mDataH != nil) {
  120.         ::DisposeHandle(mDataH);
  121.     }
  122.         
  123.     mDataH = inHandle;
  124.     
  125.     Int32    newLength = 0;
  126.     if (inHandle != nil) {
  127.         newLength = GetHandleSize(inHandle);
  128.     }
  129.     LStream::SetLength(newLength);
  130.         
  131.     SetMarker(0, streamFrom_Start);
  132. }
  133.  
  134.  
  135. Handle
  136. LHandleStream::GetDataHandle()
  137. {
  138.     return mDataH;
  139. }
  140.  
  141.  
  142. Handle
  143. LHandleStream::DetachDataHandle()
  144. {
  145.     Handle    dataHandle = mDataH;    // Save current data Handle
  146.     
  147.     LStream::SetLength(0);
  148.     mDataH = ::NewHandle(0);        // Reset to zero-sized Handle
  149.     ThrowIfMemFail_(mDataH);
  150.     
  151.     return dataHandle;
  152. }
  153.