home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWFiles / Sources / FWAccBuf.cpp next >
Encoding:
Text File  |  1995-11-08  |  5.2 KB  |  180 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:        FWAccBuf.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWACCBUF_H
  13. #include "FWAccBuf.h"
  14. #endif
  15.  
  16. #ifndef FWPRIDEB_H
  17. #include "FWPriDeb.h"
  18. #endif
  19.  
  20. #if FW_LIB_EXPORT_PRAGMAS
  21. #pragma lib_export on
  22. #endif
  23.  
  24. //========================================================================================
  25. //    CLASS FW_CPrivFileAccessBuffer
  26. //========================================================================================
  27.  
  28. //----------------------------------------------------------------------------------------
  29. // FW_CPrivFileAccessBuffer::FW_CPrivFileAccessBuffer
  30. //----------------------------------------------------------------------------------------
  31.  
  32. FW_CPrivFileAccessBuffer::FW_CPrivFileAccessBuffer(long capacity) :
  33.     fType(kInvalid),
  34.     fBuffer(0),
  35.     fCapacity(capacity),
  36.     fInitialPosition(0),
  37.     fValidBytes(0),
  38.     fBytesWritten(0)
  39. {
  40. }
  41.  
  42. //----------------------------------------------------------------------------------------
  43. // FW_CPrivFileAccessBuffer::FW_CPrivFileAccessBuffer
  44. //----------------------------------------------------------------------------------------
  45.  
  46. FW_CPrivFileAccessBuffer::FW_CPrivFileAccessBuffer(const FW_CPrivFileAccessBuffer& otherBuffer) :
  47.     fType(kInvalid),
  48.     fBuffer(0),
  49.     fCapacity(otherBuffer.fCapacity),
  50.     fInitialPosition(0),
  51.     fValidBytes(0),
  52.     fBytesWritten(0)
  53. {
  54. }
  55.  
  56. //----------------------------------------------------------------------------------------
  57. // FW_CPrivFileAccessBuffer::~FW_CPrivFileAccessBuffer
  58. //----------------------------------------------------------------------------------------
  59.  
  60. FW_CPrivFileAccessBuffer::~FW_CPrivFileAccessBuffer()
  61. {
  62.     delete [] fBuffer;
  63. }
  64.  
  65. //----------------------------------------------------------------------------------------
  66. // FW_CPrivFileAccessBuffer::Initialize
  67. //----------------------------------------------------------------------------------------
  68.  
  69. void FW_CPrivFileAccessBuffer::Initialize(int type,
  70.                                          long filePosition,
  71.                                          long validBytes)
  72. {
  73.     fType = type;
  74.     if (fBuffer == 0)
  75.         fBuffer = new char[fCapacity];
  76.     fInitialPosition = filePosition;
  77.     fValidBytes = validBytes;
  78.     fBytesWritten = 0;
  79. }
  80.  
  81. //----------------------------------------------------------------------------------------
  82. // FW_CPrivFileAccessBuffer::operator=
  83. //----------------------------------------------------------------------------------------
  84.  
  85. FW_CPrivFileAccessBuffer& FW_CPrivFileAccessBuffer::operator=(const FW_CPrivFileAccessBuffer& otherBuffer)
  86. {
  87.     if (this != &otherBuffer)
  88.     {
  89.         if (fCapacity != otherBuffer.fCapacity)
  90.         {
  91.             delete [] fBuffer;
  92.             
  93.             fBuffer = NULL;
  94.             fCapacity = otherBuffer.fCapacity;
  95.             fType = kInvalid;
  96.         }
  97.     }
  98.     
  99.     return (*this);
  100. }
  101.  
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_CPrivFileAccessBuffer::ReadPeek
  105. //----------------------------------------------------------------------------------------
  106.  
  107. void* FW_CPrivFileAccessBuffer::ReadPeek(long currentPosition,
  108.                                         long& availableReadBytes)
  109. {
  110.     void * source = 0;
  111.     
  112.     availableReadBytes = 0;
  113.     
  114.     if (fType == kReadPeek)
  115.     {
  116.         long offsetInBuffer = currentPosition - fInitialPosition;
  117.         if ((offsetInBuffer >= 0) && (offsetInBuffer < fValidBytes))
  118.         {
  119.             source = fBuffer + offsetInBuffer;
  120.             availableReadBytes = fValidBytes - offsetInBuffer;
  121.         }
  122.     }
  123.  
  124.     return source;
  125. }
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // FW_CPrivFileAccessBuffer::ReadPeekAdvance
  129. //----------------------------------------------------------------------------------------
  130.  
  131. void FW_CPrivFileAccessBuffer::ReadPeekAdvance(long currentPosition,
  132.                                               long bytesRead)
  133. {
  134.     FW_ASSERT(fType == kReadPeek);
  135.     FW_ASSERT(fInitialPosition <= currentPosition);
  136.     FW_ASSERT(bytesRead >= 0);
  137.     FW_ASSERT((currentPosition - fInitialPosition + bytesRead) <= fValidBytes);
  138. }
  139.  
  140.  
  141. //----------------------------------------------------------------------------------------
  142. // FW_CPrivFileAccessBuffer::WritePeek
  143. //----------------------------------------------------------------------------------------
  144.  
  145. void* FW_CPrivFileAccessBuffer::WritePeek(long currentPosition,
  146.                                          long& availableWriteBytes)
  147. {
  148.     void * destination = 0;
  149.     
  150.     availableWriteBytes = 0;
  151.     
  152.     if (fType == kWritePeek)
  153.     {
  154.         long offsetInBuffer = currentPosition - fInitialPosition;
  155.         if ((fBytesWritten == offsetInBuffer) && (fBytesWritten < fValidBytes))
  156.         {
  157.             destination = fBuffer + fBytesWritten;
  158.             availableWriteBytes = fValidBytes - fBytesWritten;
  159.         }
  160.     }
  161.  
  162.     return destination;
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // FW_CPrivFileAccessBuffer::WritePeekAdvance
  167. //----------------------------------------------------------------------------------------
  168.  
  169. void FW_CPrivFileAccessBuffer::WritePeekAdvance(long currentPosition,
  170.                                                long bytesWritten)
  171. {
  172.     FW_ASSERT(fType == kWritePeek);
  173.     FW_ASSERT(fBytesWritten == (currentPosition - fInitialPosition));
  174.     FW_ASSERT(bytesWritten >= 0);
  175.     FW_ASSERT((fBytesWritten + bytesWritten) <= fValidBytes);
  176.     
  177.     fBytesWritten += bytesWritten;
  178. }
  179.  
  180.